queriesTable.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import type {Location} from 'history';
  2. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  3. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  4. import type {CursorHandler} from 'sentry/components/pagination';
  5. import Pagination from 'sentry/components/pagination';
  6. import {t} from 'sentry/locale';
  7. import type {Organization} from 'sentry/types/organization';
  8. import {trackAnalytics} from 'sentry/utils/analytics';
  9. import {browserHistory} from 'sentry/utils/browserHistory';
  10. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  11. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  12. import type {Sort} from 'sentry/utils/discover/fields';
  13. import {RATE_UNIT_TITLE, RateUnit} from 'sentry/utils/discover/fields';
  14. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  15. import {useLocation} from 'sentry/utils/useLocation';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  18. import {SpanDescriptionCell} from 'sentry/views/insights/common/components/tableCells/spanDescriptionCell';
  19. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  20. import {DataTitles} from 'sentry/views/insights/common/views/spans/types';
  21. import type {SpanMetricsResponse} from 'sentry/views/insights/types';
  22. import {ModuleName} from 'sentry/views/insights/types';
  23. type Row = Pick<
  24. SpanMetricsResponse,
  25. | 'project.id'
  26. | 'span.description'
  27. | 'span.group'
  28. | 'spm()'
  29. | 'avg(span.self_time)'
  30. | 'sum(span.self_time)'
  31. | 'time_spent_percentage()'
  32. >;
  33. type Column = GridColumnHeader<
  34. 'span.description' | 'spm()' | 'avg(span.self_time)' | 'time_spent_percentage()'
  35. >;
  36. const COLUMN_ORDER: Column[] = [
  37. {
  38. key: 'span.description',
  39. name: t('Query Description'),
  40. width: COL_WIDTH_UNDEFINED,
  41. },
  42. {
  43. key: 'spm()',
  44. name: `${t('Queries')} ${RATE_UNIT_TITLE[RateUnit.PER_MINUTE]}`,
  45. width: COL_WIDTH_UNDEFINED,
  46. },
  47. {
  48. key: `avg(span.self_time)`,
  49. name: DataTitles.avg,
  50. width: COL_WIDTH_UNDEFINED,
  51. },
  52. {
  53. key: 'time_spent_percentage()',
  54. name: DataTitles.timeSpent,
  55. width: COL_WIDTH_UNDEFINED,
  56. },
  57. ];
  58. const SORTABLE_FIELDS = ['avg(span.self_time)', 'spm()', 'time_spent_percentage()'];
  59. type ValidSort = Sort & {
  60. field: 'spm()' | 'avg(span.self_time)' | 'time_spent_percentage()';
  61. };
  62. export function isAValidSort(sort: Sort): sort is ValidSort {
  63. return (SORTABLE_FIELDS as unknown as string[]).includes(sort.field);
  64. }
  65. interface Props {
  66. response: {
  67. data: Row[];
  68. isLoading: boolean;
  69. error?: Error | null;
  70. meta?: EventsMetaType;
  71. pageLinks?: string;
  72. };
  73. sort: ValidSort;
  74. }
  75. export function QueriesTable({response, sort}: Props) {
  76. const {data, isLoading, meta, pageLinks} = response;
  77. const location = useLocation();
  78. const organization = useOrganization();
  79. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  80. browserHistory.push({
  81. pathname,
  82. query: {...query, [QueryParameterNames.SPANS_CURSOR]: newCursor},
  83. });
  84. };
  85. return (
  86. <VisuallyCompleteWithData
  87. id="QueriesTable"
  88. hasData={data.length > 0}
  89. isLoading={isLoading}
  90. >
  91. <GridEditable
  92. isLoading={isLoading}
  93. error={response.error}
  94. data={data}
  95. columnOrder={COLUMN_ORDER}
  96. columnSortBy={[
  97. {
  98. key: sort.field,
  99. order: sort.kind,
  100. },
  101. ]}
  102. grid={{
  103. renderHeadCell: column =>
  104. renderHeadCell({
  105. column,
  106. sort,
  107. location,
  108. sortParameterName: QueryParameterNames.SPANS_SORT,
  109. }),
  110. renderBodyCell: (column, row) =>
  111. renderBodyCell(column, row, meta, location, organization),
  112. }}
  113. />
  114. <Pagination
  115. pageLinks={pageLinks}
  116. onCursor={handleCursor}
  117. paginationAnalyticsEvent={(direction: string) => {
  118. trackAnalytics('insight.general.table_paginate', {
  119. organization,
  120. source: ModuleName.DB,
  121. direction,
  122. });
  123. }}
  124. />
  125. </VisuallyCompleteWithData>
  126. );
  127. }
  128. function renderBodyCell(
  129. column: Column,
  130. row: Row,
  131. meta: EventsMetaType | undefined,
  132. location: Location,
  133. organization: Organization
  134. ) {
  135. if (column.key === 'span.description') {
  136. return (
  137. <SpanDescriptionCell
  138. moduleName={ModuleName.DB}
  139. description={row['span.description']}
  140. group={row['span.group']}
  141. projectId={row['project.id']}
  142. />
  143. );
  144. }
  145. if (!meta || !meta?.fields) {
  146. return row[column.key];
  147. }
  148. const renderer = getFieldRenderer(column.key, meta.fields, false);
  149. const rendered = renderer(row, {
  150. location,
  151. organization,
  152. unit: meta.units?.[column.key],
  153. });
  154. return rendered;
  155. }