domainsTable.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {browserHistory} from 'react-router';
  2. import type {Location} from 'history';
  3. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  4. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  5. import type {CursorHandler} from 'sentry/components/pagination';
  6. import Pagination from 'sentry/components/pagination';
  7. import {t} from 'sentry/locale';
  8. import type {Organization} from 'sentry/types';
  9. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  10. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  11. import type {Sort} from 'sentry/utils/discover/fields';
  12. import {RATE_UNIT_TITLE, RateUnit} from 'sentry/utils/discover/fields';
  13. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import useOrganization from 'sentry/utils/useOrganization';
  16. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  17. import type {MetricsResponse} from 'sentry/views/starfish/types';
  18. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  19. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  20. type Row = Pick<
  21. MetricsResponse,
  22. | 'project.id'
  23. | 'span.domain'
  24. | 'spm()'
  25. | 'avg(span.self_time)'
  26. | 'sum(span.self_time)'
  27. | 'time_spent_percentage()'
  28. >;
  29. type Column = GridColumnHeader<
  30. 'span.domain' | 'spm()' | 'avg(span.self_time)' | 'time_spent_percentage()'
  31. >;
  32. const COLUMN_ORDER: Column[] = [
  33. {
  34. key: 'span.domain',
  35. name: t('Domain'),
  36. width: COL_WIDTH_UNDEFINED,
  37. },
  38. {
  39. key: 'spm()',
  40. name: `${t('Requests')} ${RATE_UNIT_TITLE[RateUnit.PER_MINUTE]}`,
  41. width: COL_WIDTH_UNDEFINED,
  42. },
  43. {
  44. key: `avg(span.self_time)`,
  45. name: DataTitles.avg,
  46. width: COL_WIDTH_UNDEFINED,
  47. },
  48. {
  49. key: 'time_spent_percentage()',
  50. name: DataTitles.timeSpent,
  51. width: COL_WIDTH_UNDEFINED,
  52. },
  53. ];
  54. const SORTABLE_FIELDS = [
  55. 'avg(span.self_time)',
  56. 'spm()',
  57. 'time_spent_percentage()',
  58. ] as const;
  59. type ValidSort = Sort & {
  60. field: (typeof SORTABLE_FIELDS)[number];
  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 DomainsTable({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.DOMAINS_CURSOR]: newCursor},
  83. });
  84. };
  85. return (
  86. <VisuallyCompleteWithData
  87. id="DomainsTable"
  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.DOMAINS_SORT,
  109. }),
  110. renderBodyCell: (column, row) =>
  111. renderBodyCell(column, row, meta, location, organization),
  112. }}
  113. location={location}
  114. />
  115. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  116. </VisuallyCompleteWithData>
  117. );
  118. }
  119. function renderBodyCell(
  120. column: Column,
  121. row: Row,
  122. meta: EventsMetaType | undefined,
  123. location: Location,
  124. organization: Organization
  125. ) {
  126. if (!meta?.fields) {
  127. return row[column.key];
  128. }
  129. const renderer = getFieldRenderer(column.key, meta.fields, false);
  130. return renderer(row, {
  131. location,
  132. organization,
  133. unit: meta.units?.[column.key],
  134. });
  135. }