queriesTable.tsx 4.9 KB

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