|
| 1 | +import {browserHistory} from 'react-router'; |
| 2 | +import type {Location} from 'history'; |
| 3 | + |
| 4 | +import type {GridColumnHeader} from 'sentry/components/gridEditable'; |
| 5 | +import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable'; |
| 6 | +import Link from 'sentry/components/links/link'; |
| 7 | +import type {CursorHandler} from 'sentry/components/pagination'; |
| 8 | +import Pagination from 'sentry/components/pagination'; |
| 9 | +import {t} from 'sentry/locale'; |
| 10 | +import type {Organization} from 'sentry/types'; |
| 11 | +import type {EventsMetaType} from 'sentry/utils/discover/eventView'; |
| 12 | +import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers'; |
| 13 | +import type {Sort} from 'sentry/utils/discover/fields'; |
| 14 | +import {RATE_UNIT_TITLE, RateUnit} from 'sentry/utils/discover/fields'; |
| 15 | +import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry'; |
| 16 | +import {decodeScalar, decodeSorts} from 'sentry/utils/queryString'; |
| 17 | +import {MutableSearch} from 'sentry/utils/tokenizeSearch'; |
| 18 | +import {useLocation} from 'sentry/utils/useLocation'; |
| 19 | +import useOrganization from 'sentry/utils/useOrganization'; |
| 20 | +import {normalizeUrl} from 'sentry/utils/withDomainRequired'; |
| 21 | +import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell'; |
| 22 | +import {useSpanMetrics} from 'sentry/views/starfish/queries/useSpanMetrics'; |
| 23 | +import type {MetricsResponse} from 'sentry/views/starfish/types'; |
| 24 | +import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters'; |
| 25 | +import {DataTitles} from 'sentry/views/starfish/views/spans/types'; |
| 26 | + |
| 27 | +type Row = Pick< |
| 28 | + MetricsResponse, |
| 29 | + | 'project.id' |
| 30 | + | 'span.description' |
| 31 | + | 'span.group' |
| 32 | + | 'spm()' |
| 33 | + | 'avg(span.self_time)' |
| 34 | + | 'sum(span.self_time)' |
| 35 | + | 'time_spent_percentage()' |
| 36 | +>; |
| 37 | + |
| 38 | +type Column = GridColumnHeader< |
| 39 | + 'span.description' | 'spm()' | 'avg(span.self_time)' | 'time_spent_percentage()' |
| 40 | +>; |
| 41 | + |
| 42 | +const COLUMN_ORDER: Column[] = [ |
| 43 | + { |
| 44 | + key: 'span.description', |
| 45 | + name: t('AI Pipeline name'), |
| 46 | + width: COL_WIDTH_UNDEFINED, |
| 47 | + }, |
| 48 | + { |
| 49 | + key: 'spm()', |
| 50 | + name: `${t('Times')} ${RATE_UNIT_TITLE[RateUnit.PER_MINUTE]}`, |
| 51 | + width: COL_WIDTH_UNDEFINED, |
| 52 | + }, |
| 53 | + { |
| 54 | + key: `avg(span.self_time)`, |
| 55 | + name: DataTitles.avg, |
| 56 | + width: COL_WIDTH_UNDEFINED, |
| 57 | + }, |
| 58 | + { |
| 59 | + key: 'time_spent_percentage()', |
| 60 | + name: DataTitles.timeSpent, |
| 61 | + width: COL_WIDTH_UNDEFINED, |
| 62 | + }, |
| 63 | +]; |
| 64 | + |
| 65 | +const SORTABLE_FIELDS = ['avg(span.self_time)', 'spm()', 'time_spent_percentage()']; |
| 66 | + |
| 67 | +type ValidSort = Sort & { |
| 68 | + field: 'spm()' | 'avg(span.self_time)' | 'time_spent_percentage()'; |
| 69 | +}; |
| 70 | + |
| 71 | +export function isAValidSort(sort: Sort): sort is ValidSort { |
| 72 | + return (SORTABLE_FIELDS as unknown as string[]).includes(sort.field); |
| 73 | +} |
| 74 | + |
| 75 | +export function PipelinesTable() { |
| 76 | + const location = useLocation(); |
| 77 | + const organization = useOrganization(); |
| 78 | + const cursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]); |
| 79 | + const sortField = decodeScalar(location.query?.[QueryParameterNames.SPANS_SORT]); |
| 80 | + |
| 81 | + let sort = decodeSorts(sortField).filter(isAValidSort)[0]; |
| 82 | + if (!sort) { |
| 83 | + sort = {field: 'time_spent_percentage()', kind: 'desc'}; |
| 84 | + } |
| 85 | + const {data, isLoading, meta, pageLinks, error} = useSpanMetrics({ |
| 86 | + search: new MutableSearch('span.op:ai.pipeline.langchain'), |
| 87 | + fields: [ |
| 88 | + 'project.id', |
| 89 | + 'span.group', |
| 90 | + 'span.description', |
| 91 | + 'spm()', |
| 92 | + 'avg(span.self_time)', |
| 93 | + 'sum(span.self_time)', |
| 94 | + 'time_spent_percentage()', |
| 95 | + ], |
| 96 | + sorts: [sort], |
| 97 | + limit: 25, |
| 98 | + cursor, |
| 99 | + }); |
| 100 | + |
| 101 | + const handleCursor: CursorHandler = (newCursor, pathname, query) => { |
| 102 | + browserHistory.push({ |
| 103 | + pathname, |
| 104 | + query: {...query, [QueryParameterNames.SPANS_CURSOR]: newCursor}, |
| 105 | + }); |
| 106 | + }; |
| 107 | + |
| 108 | + return ( |
| 109 | + <VisuallyCompleteWithData |
| 110 | + id="PipelinesTable" |
| 111 | + hasData={data.length > 0} |
| 112 | + isLoading={isLoading} |
| 113 | + > |
| 114 | + <GridEditable |
| 115 | + isLoading={isLoading} |
| 116 | + error={error} |
| 117 | + data={data} |
| 118 | + columnOrder={COLUMN_ORDER} |
| 119 | + columnSortBy={[ |
| 120 | + { |
| 121 | + key: sort.field, |
| 122 | + order: sort.kind, |
| 123 | + }, |
| 124 | + ]} |
| 125 | + grid={{ |
| 126 | + renderHeadCell: column => |
| 127 | + renderHeadCell({ |
| 128 | + column, |
| 129 | + sort, |
| 130 | + location, |
| 131 | + sortParameterName: QueryParameterNames.SPANS_SORT, |
| 132 | + }), |
| 133 | + renderBodyCell: (column, row) => |
| 134 | + renderBodyCell(column, row, meta, location, organization), |
| 135 | + }} |
| 136 | + location={location} |
| 137 | + /> |
| 138 | + <Pagination pageLinks={pageLinks} onCursor={handleCursor} /> |
| 139 | + </VisuallyCompleteWithData> |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +function renderBodyCell( |
| 144 | + column: Column, |
| 145 | + row: Row, |
| 146 | + meta: EventsMetaType | undefined, |
| 147 | + location: Location, |
| 148 | + organization: Organization |
| 149 | +) { |
| 150 | + if (column.key === 'span.description') { |
| 151 | + if (!row['span.description']) { |
| 152 | + return <span>(unknown)</span>; |
| 153 | + } |
| 154 | + if (!row['span.group']) { |
| 155 | + return <span>{row['span.description']}</span>; |
| 156 | + } |
| 157 | + return ( |
| 158 | + <Link |
| 159 | + to={normalizeUrl( |
| 160 | + `/organizations/${organization.slug}/ai-analytics/pipelines/${row['span.group']}` |
| 161 | + )} |
| 162 | + > |
| 163 | + {row['span.description']} |
| 164 | + </Link> |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + if (!meta || !meta?.fields) { |
| 169 | + return row[column.key]; |
| 170 | + } |
| 171 | + |
| 172 | + const renderer = getFieldRenderer(column.key, meta.fields, false); |
| 173 | + |
| 174 | + const rendered = renderer(row, { |
| 175 | + location, |
| 176 | + organization, |
| 177 | + unit: meta.units?.[column.key], |
| 178 | + }); |
| 179 | + |
| 180 | + return rendered; |
| 181 | +} |
0 commit comments