queryTransactionsTable.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import type {Location} from 'history';
  4. import * as qs from 'query-string';
  5. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  6. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  7. import Link from 'sentry/components/links/link';
  8. import type {CursorHandler} from 'sentry/components/pagination';
  9. import Pagination from 'sentry/components/pagination';
  10. import {t} from 'sentry/locale';
  11. import type {Organization} from 'sentry/types';
  12. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  13. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  14. import type {Sort} from 'sentry/utils/discover/fields';
  15. import {useLocation} from 'sentry/utils/useLocation';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {useDatabaseModuleURL} from 'sentry/views/performance/utils/useModuleURL';
  18. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  19. import {OverflowEllipsisTextContainer} from 'sentry/views/starfish/components/textAlign';
  20. import type {SpanMetricsResponse} from 'sentry/views/starfish/types';
  21. import {SpanMetricsField} from 'sentry/views/starfish/types';
  22. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  23. import {DataTitles, getThroughputTitle} from 'sentry/views/starfish/views/spans/types';
  24. type Row = Pick<
  25. SpanMetricsResponse,
  26. | 'transaction'
  27. | 'transaction.method'
  28. | 'spm()'
  29. | 'avg(span.self_time)'
  30. | 'sum(span.self_time)'
  31. | 'time_spent_percentage()'
  32. >;
  33. type Column = GridColumnHeader<
  34. 'transaction' | 'spm()' | 'avg(span.self_time)' | 'time_spent_percentage()'
  35. >;
  36. const COLUMN_ORDER: Column[] = [
  37. {
  38. key: 'transaction',
  39. name: t('Found In'),
  40. width: COL_WIDTH_UNDEFINED,
  41. },
  42. {
  43. key: 'spm()',
  44. name: getThroughputTitle('db'),
  45. width: COL_WIDTH_UNDEFINED,
  46. },
  47. {
  48. key: `avg(${SpanMetricsField.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 = [
  59. 'avg(span.self_time)',
  60. 'spm()',
  61. 'time_spent_percentage()',
  62. ] as const;
  63. type ValidSort = Sort & {
  64. field: (typeof SORTABLE_FIELDS)[number];
  65. };
  66. export function isAValidSort(sort: Sort): sort is ValidSort {
  67. return (SORTABLE_FIELDS as unknown as string[]).includes(sort.field);
  68. }
  69. interface Props {
  70. data: Row[];
  71. isLoading: boolean;
  72. sort: ValidSort;
  73. span: Pick<SpanMetricsResponse, SpanMetricsField.SPAN_GROUP | SpanMetricsField.SPAN_OP>;
  74. error?: Error | null;
  75. meta?: EventsMetaType;
  76. pageLinks?: string;
  77. }
  78. export function QueryTransactionsTable({
  79. data,
  80. isLoading,
  81. error,
  82. meta,
  83. pageLinks,
  84. sort,
  85. span,
  86. }: Props) {
  87. const moduleURL = useDatabaseModuleURL();
  88. const location = useLocation();
  89. const organization = useOrganization();
  90. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  91. browserHistory.push({
  92. pathname,
  93. query: {...query, [QueryParameterNames.TRANSACTIONS_CURSOR]: newCursor},
  94. });
  95. };
  96. return (
  97. <Fragment>
  98. <GridEditable
  99. aria-label={t('Transactions')}
  100. isLoading={isLoading}
  101. error={error}
  102. data={data}
  103. columnOrder={COLUMN_ORDER}
  104. columnSortBy={[
  105. {
  106. key: sort.field,
  107. order: sort.kind,
  108. },
  109. ]}
  110. grid={{
  111. renderHeadCell: col =>
  112. renderHeadCell({
  113. column: col,
  114. sort,
  115. location,
  116. sortParameterName: QueryParameterNames.TRANSACTIONS_SORT,
  117. }),
  118. renderBodyCell: (column, row) =>
  119. renderBodyCell(moduleURL, column, row, meta, span, location, organization),
  120. }}
  121. location={location}
  122. />
  123. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  124. </Fragment>
  125. );
  126. }
  127. function renderBodyCell(
  128. moduleURL: string,
  129. column: Column,
  130. row: Row,
  131. meta: EventsMetaType | undefined,
  132. span: Pick<SpanMetricsResponse, SpanMetricsField.SPAN_GROUP | SpanMetricsField.SPAN_OP>,
  133. location: Location,
  134. organization: Organization
  135. ) {
  136. if (column.key === 'transaction') {
  137. const label =
  138. row['transaction.method'] && !row.transaction.startsWith(row['transaction.method'])
  139. ? `${row['transaction.method']} ${row.transaction}`
  140. : row.transaction;
  141. const pathname = `${moduleURL}/spans/span/${encodeURIComponent(span[SpanMetricsField.SPAN_GROUP])}`;
  142. const query: {[key: string]: string | undefined} = {
  143. ...location.query,
  144. transaction: row.transaction,
  145. transactionMethod: row['transaction.method'],
  146. };
  147. return (
  148. <OverflowEllipsisTextContainer>
  149. <Link to={`${pathname}?${qs.stringify(query)}`}>{label}</Link>
  150. </OverflowEllipsisTextContainer>
  151. );
  152. }
  153. if (!meta || !meta?.fields) {
  154. return row[column.key];
  155. }
  156. const renderer = getFieldRenderer(column.key, meta.fields, false);
  157. const rendered = renderer(
  158. {...row, 'span.op': span['span.op']},
  159. {
  160. location,
  161. organization,
  162. unit: meta.units?.[column.key],
  163. }
  164. );
  165. return rendered;
  166. }