pipelineSpansTable.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import type {Location} from 'history';
  2. import GridEditable, {
  3. COL_WIDTH_UNDEFINED,
  4. type GridColumnHeader,
  5. } from 'sentry/components/gridEditable';
  6. import Link from 'sentry/components/links/link';
  7. import {t} from 'sentry/locale';
  8. import type {Organization} from 'sentry/types';
  9. import EventView, {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 {generateLinkToEventInTraceView} from 'sentry/utils/discover/urls';
  13. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  14. import {decodeScalar, decodeSorts} from 'sentry/utils/queryString';
  15. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  16. import {useLocation} from 'sentry/utils/useLocation';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  19. import {useIndexedSpans} from 'sentry/views/starfish/queries/useIndexedSpans';
  20. import {SpanIndexedField} from 'sentry/views/starfish/types';
  21. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  22. type Column = GridColumnHeader<
  23. | SpanIndexedField.ID
  24. | SpanIndexedField.SPAN_DURATION
  25. | SpanIndexedField.TIMESTAMP
  26. | SpanIndexedField.USER
  27. >;
  28. const COLUMN_ORDER: Column[] = [
  29. {
  30. key: SpanIndexedField.ID,
  31. name: t('ID'),
  32. width: COL_WIDTH_UNDEFINED,
  33. },
  34. {
  35. key: SpanIndexedField.SPAN_DURATION,
  36. name: t('Total duration'),
  37. width: 150,
  38. },
  39. {
  40. key: SpanIndexedField.USER,
  41. name: t('User'),
  42. width: COL_WIDTH_UNDEFINED,
  43. },
  44. {
  45. key: SpanIndexedField.TIMESTAMP,
  46. name: t('Timestamp'),
  47. width: COL_WIDTH_UNDEFINED,
  48. },
  49. ];
  50. const SORTABLE_FIELDS = [
  51. SpanIndexedField.ID,
  52. SpanIndexedField.SPAN_DURATION,
  53. SpanIndexedField.TIMESTAMP,
  54. ];
  55. type ValidSort = Sort & {
  56. field:
  57. | SpanIndexedField.ID
  58. | SpanIndexedField.SPAN_DURATION
  59. | SpanIndexedField.TIMESTAMP;
  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. groupId: string;
  66. }
  67. export function PipelineSpansTable({groupId}: Props) {
  68. const location = useLocation();
  69. const organization = useOrganization();
  70. const sortField = decodeScalar(location.query?.[QueryParameterNames.SPANS_SORT]);
  71. let sort = decodeSorts(sortField).filter(isAValidSort)[0];
  72. if (!sort) {
  73. sort = {field: SpanIndexedField.TIMESTAMP, kind: 'desc'};
  74. }
  75. const {
  76. data: rawData,
  77. meta: rawMeta,
  78. error,
  79. isLoading,
  80. } = useIndexedSpans({
  81. limit: 30,
  82. sorts: [sort],
  83. fields: [
  84. SpanIndexedField.ID,
  85. SpanIndexedField.TRACE,
  86. SpanIndexedField.SPAN_DURATION,
  87. SpanIndexedField.TRANSACTION_ID,
  88. SpanIndexedField.USER,
  89. SpanIndexedField.TIMESTAMP,
  90. SpanIndexedField.PROJECT,
  91. ],
  92. referrer: 'api.ai-pipelines.view',
  93. search: new MutableSearch(`span.category:ai.pipeline span.group:"${groupId}"`),
  94. });
  95. const data = rawData || [];
  96. const meta = rawMeta as EventsMetaType;
  97. return (
  98. <VisuallyCompleteWithData
  99. id="PipelineSpansTable"
  100. hasData={data.length > 0}
  101. isLoading={isLoading}
  102. >
  103. <GridEditable
  104. isLoading={isLoading}
  105. error={error}
  106. data={data}
  107. columnOrder={COLUMN_ORDER}
  108. columnSortBy={[
  109. {
  110. key: sort.field,
  111. order: sort.kind,
  112. },
  113. ]}
  114. grid={{
  115. renderHeadCell: column =>
  116. renderHeadCell({
  117. column,
  118. sort,
  119. location,
  120. sortParameterName: QueryParameterNames.SPANS_SORT,
  121. }),
  122. renderBodyCell: (column, row) =>
  123. renderBodyCell(column, row, meta, location, organization),
  124. }}
  125. location={location}
  126. />
  127. </VisuallyCompleteWithData>
  128. );
  129. }
  130. function renderBodyCell(
  131. column: Column,
  132. row: any,
  133. meta: EventsMetaType | undefined,
  134. location: Location,
  135. organization: Organization
  136. ) {
  137. if (column.key === SpanIndexedField.ID) {
  138. if (!row[SpanIndexedField.ID]) {
  139. return <span>(unknown)</span>;
  140. }
  141. if (!row[SpanIndexedField.TRACE]) {
  142. return <span>{row[SpanIndexedField.ID]}</span>;
  143. }
  144. return (
  145. <Link
  146. to={generateLinkToEventInTraceView({
  147. organization,
  148. eventId: row[SpanIndexedField.TRANSACTION_ID],
  149. projectSlug: row[SpanIndexedField.PROJECT],
  150. traceSlug: row[SpanIndexedField.TRACE],
  151. timestamp: row[SpanIndexedField.TIMESTAMP],
  152. location,
  153. eventView: EventView.fromLocation(location),
  154. spanId: row[SpanIndexedField.ID],
  155. })}
  156. >
  157. {row[SpanIndexedField.ID]}
  158. </Link>
  159. );
  160. }
  161. if (!meta || !meta?.fields) {
  162. return row[column.key];
  163. }
  164. const renderer = getFieldRenderer(column.key, meta.fields, false);
  165. const rendered = renderer(row, {
  166. location,
  167. organization,
  168. unit: meta.units?.[column.key],
  169. });
  170. return rendered;
  171. }