pipelinesTable.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import styled from '@emotion/styled';
  2. import type {Location} from 'history';
  3. import * as qs from 'query-string';
  4. import GridEditable, {
  5. COL_WIDTH_UNDEFINED,
  6. type GridColumnHeader,
  7. } from 'sentry/components/gridEditable';
  8. import Link from 'sentry/components/links/link';
  9. import type {CursorHandler} from 'sentry/components/pagination';
  10. import Pagination from 'sentry/components/pagination';
  11. import SearchBar from 'sentry/components/searchBar';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {IconInfo} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {Organization} from 'sentry/types/organization';
  17. import {browserHistory} from 'sentry/utils/browserHistory';
  18. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  19. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  20. import type {Sort} from 'sentry/utils/discover/fields';
  21. import {RATE_UNIT_TITLE, RateUnit} from 'sentry/utils/discover/fields';
  22. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  23. import {decodeScalar, decodeSorts} from 'sentry/utils/queryString';
  24. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  25. import {useLocation} from 'sentry/utils/useLocation';
  26. import useOrganization from 'sentry/utils/useOrganization';
  27. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  28. import {useSpanMetrics} from 'sentry/views/insights/common/queries/useDiscover';
  29. import {useModuleURL} from 'sentry/views/insights/common/utils/useModuleURL';
  30. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  31. import type {SpanMetricsResponse} from 'sentry/views/insights/types';
  32. type Row = Pick<
  33. SpanMetricsResponse,
  34. | 'project.id'
  35. | 'span.description'
  36. | 'span.group'
  37. | 'spm()'
  38. | 'avg(span.duration)'
  39. | 'sum(span.duration)'
  40. | 'sum(ai.total_tokens.used)'
  41. | 'sum(ai.total_cost)'
  42. >;
  43. type Column = GridColumnHeader<
  44. | 'span.description'
  45. | 'spm()'
  46. | 'avg(span.duration)'
  47. | 'sum(ai.total_tokens.used)'
  48. | 'sum(ai.total_cost)'
  49. >;
  50. const COLUMN_ORDER: Column[] = [
  51. {
  52. key: 'span.description',
  53. name: t('AI Pipeline Name'),
  54. width: COL_WIDTH_UNDEFINED,
  55. },
  56. {
  57. key: 'sum(ai.total_tokens.used)',
  58. name: t('Total tokens used'),
  59. width: 180,
  60. },
  61. {
  62. key: 'sum(ai.total_cost)',
  63. name: t('Total cost'),
  64. width: 180,
  65. },
  66. {
  67. key: `avg(span.duration)`,
  68. name: t('Pipeline Duration'),
  69. width: COL_WIDTH_UNDEFINED,
  70. },
  71. {
  72. key: 'spm()',
  73. name: `${t('Pipeline runs')} ${RATE_UNIT_TITLE[RateUnit.PER_MINUTE]}`,
  74. width: COL_WIDTH_UNDEFINED,
  75. },
  76. ];
  77. const SORTABLE_FIELDS = ['sum(ai.total_tokens.used)', 'avg(span.duration)', 'spm()'];
  78. type ValidSort = Sort & {
  79. field: 'spm()' | 'avg(span.duration)';
  80. };
  81. export function isAValidSort(sort: Sort): sort is ValidSort {
  82. return (SORTABLE_FIELDS as unknown as string[]).includes(sort.field);
  83. }
  84. export function PipelinesTable() {
  85. const location = useLocation();
  86. const moduleURL = useModuleURL('ai');
  87. const organization = useOrganization();
  88. const cursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]);
  89. const sortField = decodeScalar(location.query?.[QueryParameterNames.SPANS_SORT]);
  90. const spanDescription = decodeScalar(location.query?.['span.description'], '');
  91. let sort = decodeSorts(sortField).filter(isAValidSort)[0];
  92. if (!sort) {
  93. sort = {field: 'spm()', kind: 'desc'};
  94. }
  95. const {data, isPending, meta, pageLinks, error} = useSpanMetrics(
  96. {
  97. search: MutableSearch.fromQueryObject({
  98. 'span.category': 'ai.pipeline',
  99. 'span.description': spanDescription ? `*${spanDescription}*` : undefined,
  100. }),
  101. fields: [
  102. 'project.id',
  103. 'span.group',
  104. 'span.description',
  105. 'spm()',
  106. 'avg(span.duration)',
  107. 'sum(span.duration)',
  108. ],
  109. sorts: [sort],
  110. limit: 25,
  111. cursor,
  112. },
  113. 'api.ai-pipelines.view'
  114. );
  115. const {data: tokensUsedData, isPending: tokensUsedLoading} = useSpanMetrics(
  116. {
  117. search: new MutableSearch(
  118. `span.category:ai span.ai.pipeline.group:[${(data as Row[])
  119. ?.map(x => x['span.group'])
  120. ?.filter(x => !!x)
  121. .join(',')}]`
  122. ),
  123. fields: ['span.ai.pipeline.group', 'sum(ai.total_tokens.used)'],
  124. },
  125. 'api.performance.ai-analytics.token-usage-chart'
  126. );
  127. const {
  128. data: tokenCostData,
  129. isPending: tokenCostLoading,
  130. error: tokenCostError,
  131. } = useSpanMetrics(
  132. {
  133. search: new MutableSearch(
  134. `span.category:ai span.ai.pipeline.group:[${(data as Row[])?.map(x => x['span.group']).join(',')}]`
  135. ),
  136. fields: ['span.ai.pipeline.group', 'sum(ai.total_cost)'],
  137. },
  138. 'api.performance.ai-analytics.token-usage-chart'
  139. );
  140. const rows: Row[] = (data as Row[]).map(baseRow => {
  141. const row: Row = {
  142. ...baseRow,
  143. 'sum(ai.total_tokens.used)': 0,
  144. 'sum(ai.total_cost)': 0,
  145. };
  146. if (!tokensUsedLoading) {
  147. const tokenUsedDataPoint = tokensUsedData.find(
  148. tokenRow => tokenRow['span.ai.pipeline.group'] === row['span.group']
  149. );
  150. if (tokenUsedDataPoint) {
  151. row['sum(ai.total_tokens.used)'] =
  152. tokenUsedDataPoint['sum(ai.total_tokens.used)'];
  153. }
  154. }
  155. if (!tokenCostLoading && !tokenCostError) {
  156. const tokenCostDataPoint = tokenCostData.find(
  157. tokenRow => tokenRow['span.ai.pipeline.group'] === row['span.group']
  158. );
  159. if (tokenCostDataPoint) {
  160. row['sum(ai.total_cost)'] = tokenCostDataPoint['sum(ai.total_cost)'];
  161. }
  162. }
  163. return row;
  164. });
  165. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  166. browserHistory.push({
  167. pathname,
  168. query: {...query, [QueryParameterNames.SPANS_CURSOR]: newCursor},
  169. });
  170. };
  171. const handleSearch = (newQuery: string) => {
  172. browserHistory.push({
  173. ...location,
  174. query: {
  175. ...location.query,
  176. 'span.description': newQuery === '' ? undefined : newQuery,
  177. [QueryParameterNames.SPANS_CURSOR]: undefined,
  178. },
  179. });
  180. };
  181. return (
  182. <VisuallyCompleteWithData
  183. id="PipelinesTable"
  184. hasData={rows.length > 0}
  185. isLoading={isPending}
  186. >
  187. <Container>
  188. <SearchBar
  189. placeholder={t('Search for pipeline')}
  190. query={spanDescription}
  191. onSearch={handleSearch}
  192. />
  193. <GridEditable
  194. isLoading={isPending}
  195. error={error}
  196. data={rows}
  197. columnOrder={COLUMN_ORDER}
  198. columnSortBy={[
  199. {
  200. key: sort.field,
  201. order: sort.kind,
  202. },
  203. ]}
  204. grid={{
  205. renderHeadCell: column =>
  206. renderHeadCell({
  207. column,
  208. sort,
  209. location,
  210. sortParameterName: QueryParameterNames.SPANS_SORT,
  211. }),
  212. renderBodyCell: (column, row) =>
  213. renderBodyCell(moduleURL, column, row, meta, location, organization),
  214. }}
  215. />
  216. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  217. </Container>
  218. </VisuallyCompleteWithData>
  219. );
  220. }
  221. function renderBodyCell(
  222. moduleURL: string,
  223. column: Column,
  224. row: Row,
  225. meta: EventsMetaType | undefined,
  226. location: Location,
  227. organization: Organization
  228. ) {
  229. if (column.key === 'span.description') {
  230. if (!row['span.description']) {
  231. return <span>(unknown)</span>;
  232. }
  233. if (!row['span.group']) {
  234. return <span>{row['span.description']}</span>;
  235. }
  236. const queryString = {
  237. ...location.query,
  238. 'span.description': row['span.description'],
  239. };
  240. return (
  241. <Link
  242. to={`${moduleURL}/pipeline-type/${row['span.group']}?${qs.stringify(queryString)}`}
  243. >
  244. {row['span.description']}
  245. </Link>
  246. );
  247. }
  248. if (column.key === 'sum(ai.total_cost)') {
  249. const cost = row[column.key];
  250. if (cost) {
  251. return <span>US ${cost.toFixed(3)}</span>;
  252. }
  253. return (
  254. <span>
  255. Unknown{' '}
  256. <Tooltip
  257. title={t(
  258. "Cost is calculated for some of the most popular models, but some providers aren't yet supported."
  259. )}
  260. isHoverable
  261. >
  262. <IconInfo size="xs" />
  263. </Tooltip>
  264. </span>
  265. );
  266. }
  267. if (!meta || !meta?.fields) {
  268. return row[column.key];
  269. }
  270. const renderer = getFieldRenderer(column.key, meta.fields, false);
  271. const rendered = renderer(row, {
  272. location,
  273. organization,
  274. unit: meta.units?.[column.key],
  275. });
  276. return rendered;
  277. }
  278. const Container = styled('div')`
  279. display: flex;
  280. flex-direction: column;
  281. gap: ${space(1)};
  282. `;