queriesTable.tsx 4.3 KB

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