spanOperationTable.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import {Fragment} from 'react';
  2. import * as qs from 'query-string';
  3. import {getInterval} from 'sentry/components/charts/utils';
  4. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  5. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  6. import SortLink from 'sentry/components/gridEditable/sortLink';
  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 {NewQuery} from 'sentry/types/organization';
  12. import {defined} from 'sentry/utils';
  13. import {browserHistory} from 'sentry/utils/browserHistory';
  14. import type {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  15. import type {MetaType} from 'sentry/utils/discover/eventView';
  16. import EventView, {isFieldSortable} from 'sentry/utils/discover/eventView';
  17. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  18. import type {Sort} from 'sentry/utils/discover/fields';
  19. import {fieldAlignment} from 'sentry/utils/discover/fields';
  20. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  21. import {decodeList, decodeScalar, decodeSorts} from 'sentry/utils/queryString';
  22. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  23. import {useLocation} from 'sentry/utils/useLocation';
  24. import useOrganization from 'sentry/utils/useOrganization';
  25. import usePageFilters from 'sentry/utils/usePageFilters';
  26. import {
  27. PRIMARY_RELEASE_ALIAS,
  28. SECONDARY_RELEASE_ALIAS,
  29. } from 'sentry/views/insights/common/components/releaseSelector';
  30. import {PercentChangeCell} from 'sentry/views/insights/common/components/tableCells/percentChangeCell';
  31. import {OverflowEllipsisTextContainer} from 'sentry/views/insights/common/components/textAlign';
  32. import {STARFISH_CHART_INTERVAL_FIDELITY} from 'sentry/views/insights/common/utils/constants';
  33. import {appendReleaseFilters} from 'sentry/views/insights/common/utils/releaseComparison';
  34. import {useModuleURL} from 'sentry/views/insights/common/utils/useModuleURL';
  35. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  36. import {APP_START_SPANS} from 'sentry/views/insights/mobile/appStarts/components/spanOpSelector';
  37. import {
  38. COLD_START_TYPE,
  39. WARM_START_TYPE,
  40. } from 'sentry/views/insights/mobile/appStarts/components/startTypeSelector';
  41. import useCrossPlatformProject from 'sentry/views/insights/mobile/common/queries/useCrossPlatformProject';
  42. import {useTableQuery} from 'sentry/views/insights/mobile/screenload/components/tables/screensTable';
  43. import {MobileCursors} from 'sentry/views/insights/mobile/screenload/constants';
  44. import {SpanMetricsField, type SubregionCode} from 'sentry/views/insights/types';
  45. const {SPAN_SELF_TIME, SPAN_DESCRIPTION, SPAN_GROUP, SPAN_OP, PROJECT_ID} =
  46. SpanMetricsField;
  47. type Props = {
  48. primaryRelease?: string;
  49. secondaryRelease?: string;
  50. transaction?: string;
  51. };
  52. export function SpanOperationTable({
  53. transaction,
  54. primaryRelease,
  55. secondaryRelease,
  56. }: Props) {
  57. const moduleURL = useModuleURL('app_start');
  58. const location = useLocation();
  59. const {selection} = usePageFilters();
  60. const organization = useOrganization();
  61. const {isProjectCrossPlatform, selectedPlatform} = useCrossPlatformProject();
  62. const cursor = decodeScalar(location.query?.[MobileCursors.SPANS_TABLE]);
  63. const spanOp = decodeScalar(location.query[SpanMetricsField.SPAN_OP]) ?? '';
  64. const subregions = decodeList(
  65. location.query[SpanMetricsField.USER_GEO_SUBREGION]
  66. ) as SubregionCode[];
  67. const startType =
  68. decodeScalar(location.query[SpanMetricsField.APP_START_TYPE]) ?? COLD_START_TYPE;
  69. const deviceClass = decodeScalar(location.query[SpanMetricsField.DEVICE_CLASS]) ?? '';
  70. const searchQuery = new MutableSearch([
  71. // Exclude root level spans because they're comprised of nested operations
  72. '!span.description:"Cold Start"',
  73. '!span.description:"Warm Start"',
  74. // Exclude this span because we can get TTID contributing spans instead
  75. '!span.description:"Initial Frame Render"',
  76. 'has:span.description',
  77. 'transaction.op:ui.load',
  78. `transaction:${transaction}`,
  79. `has:ttid`,
  80. `${SpanMetricsField.APP_START_TYPE}:${
  81. startType || `[${COLD_START_TYPE},${WARM_START_TYPE}]`
  82. }`,
  83. `${SpanMetricsField.SPAN_OP}:${spanOp ? spanOp : `[${APP_START_SPANS.join(',')}]`}`,
  84. ...(spanOp ? [`${SpanMetricsField.SPAN_OP}:${spanOp}`] : []),
  85. ...(deviceClass ? [`${SpanMetricsField.DEVICE_CLASS}:${deviceClass}`] : []),
  86. ...(subregions.length > 0
  87. ? [`${SpanMetricsField.USER_GEO_SUBREGION}:[${subregions.join(',')}]`]
  88. : []),
  89. ]);
  90. if (isProjectCrossPlatform) {
  91. searchQuery.addFilterValue('os.name', selectedPlatform);
  92. }
  93. const queryStringPrimary = appendReleaseFilters(
  94. searchQuery,
  95. primaryRelease,
  96. secondaryRelease
  97. );
  98. const sort = decodeSorts(location.query[QueryParameterNames.SPANS_SORT])[0] ?? {
  99. kind: 'desc',
  100. field: `avg_compare(${SPAN_SELF_TIME},release,${primaryRelease},${secondaryRelease})`,
  101. };
  102. const newQuery: NewQuery = {
  103. name: '',
  104. fields: [
  105. PROJECT_ID,
  106. SPAN_OP,
  107. SPAN_GROUP,
  108. SPAN_DESCRIPTION,
  109. `avg_if(${SPAN_SELF_TIME},release,${primaryRelease})`,
  110. `avg_if(${SPAN_SELF_TIME},release,${secondaryRelease})`,
  111. `avg_compare(${SPAN_SELF_TIME},release,${primaryRelease},${secondaryRelease})`,
  112. `sum(${SPAN_SELF_TIME})`,
  113. ],
  114. query: queryStringPrimary,
  115. dataset: DiscoverDatasets.SPANS_METRICS,
  116. version: 2,
  117. projects: selection.projects,
  118. interval: getInterval(selection.datetime, STARFISH_CHART_INTERVAL_FIDELITY),
  119. };
  120. const eventView = EventView.fromNewQueryWithLocation(newQuery, location);
  121. eventView.sorts = [sort];
  122. const {data, isPending, pageLinks} = useTableQuery({
  123. eventView,
  124. enabled: true,
  125. referrer: 'api.starfish.mobile-spartup-span-table',
  126. cursor,
  127. });
  128. const columnNameMap = {
  129. [SPAN_OP]: t('Operation'),
  130. [SPAN_DESCRIPTION]: t('Span Description'),
  131. [`avg_if(${SPAN_SELF_TIME},release,${primaryRelease})`]: t(
  132. 'Avg Duration (%s)',
  133. PRIMARY_RELEASE_ALIAS
  134. ),
  135. [`avg_if(${SPAN_SELF_TIME},release,${secondaryRelease})`]: t(
  136. 'Avg Duration (%s)',
  137. SECONDARY_RELEASE_ALIAS
  138. ),
  139. [`avg_compare(${SPAN_SELF_TIME},release,${primaryRelease},${secondaryRelease})`]:
  140. t('Change'),
  141. };
  142. function renderBodyCell(column, row): React.ReactNode {
  143. if (!data?.meta || !data?.meta.fields) {
  144. return row[column.key];
  145. }
  146. if (column.key === SPAN_DESCRIPTION) {
  147. const label = row[SpanMetricsField.SPAN_DESCRIPTION];
  148. const pathname = `${moduleURL}/spans/`;
  149. const query = {
  150. ...location.query,
  151. transaction,
  152. spanOp: row[SpanMetricsField.SPAN_OP],
  153. spanGroup: row[SpanMetricsField.SPAN_GROUP],
  154. spanDescription: row[SpanMetricsField.SPAN_DESCRIPTION],
  155. appStartType: row[SpanMetricsField.APP_START_TYPE],
  156. };
  157. return (
  158. <OverflowEllipsisTextContainer>
  159. <Link to={`${pathname}?${qs.stringify(query)}`}>{label}</Link>
  160. </OverflowEllipsisTextContainer>
  161. );
  162. }
  163. if (data.meta.fields[column.key] === 'percent_change') {
  164. return (
  165. <PercentChangeCell
  166. deltaValue={defined(row[column.key]) ? parseFloat(row[column.key]) : Infinity}
  167. preferredPolarity="-"
  168. />
  169. );
  170. }
  171. const renderer = getFieldRenderer(column.key, data?.meta.fields, false);
  172. const rendered = renderer(row, {
  173. location,
  174. organization,
  175. unit: data?.meta.units?.[column.key],
  176. });
  177. return rendered;
  178. }
  179. function renderHeadCell(
  180. column: GridColumnHeader,
  181. tableMeta?: MetaType
  182. ): React.ReactNode {
  183. const fieldType = tableMeta?.fields?.[column.key];
  184. const alignment = fieldAlignment(column.key as string, fieldType);
  185. const field = {
  186. field: column.key as string,
  187. width: column.width,
  188. };
  189. function generateSortLink() {
  190. if (!tableMeta) {
  191. return undefined;
  192. }
  193. let newSortDirection: Sort['kind'] = 'desc';
  194. if (sort?.field === column.key) {
  195. if (sort.kind === 'desc') {
  196. newSortDirection = 'asc';
  197. }
  198. }
  199. function getNewSort() {
  200. return `${newSortDirection === 'desc' ? '-' : ''}${column.key}`;
  201. }
  202. return {
  203. ...location,
  204. query: {...location.query, [QueryParameterNames.SPANS_SORT]: getNewSort()},
  205. };
  206. }
  207. const canSort = isFieldSortable(field, tableMeta?.fields, true);
  208. const sortLink = (
  209. <SortLink
  210. align={alignment}
  211. title={column.name}
  212. direction={sort?.field === column.key ? sort.kind : undefined}
  213. canSort={canSort}
  214. generateSortLink={generateSortLink}
  215. />
  216. );
  217. return sortLink;
  218. }
  219. const columnSortBy = eventView.getSorts();
  220. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  221. browserHistory.push({
  222. pathname,
  223. query: {...query, [MobileCursors.SPANS_TABLE]: newCursor},
  224. });
  225. };
  226. return (
  227. <Fragment>
  228. <GridEditable
  229. isLoading={isPending}
  230. data={data?.data as TableDataRow[]}
  231. columnOrder={[
  232. String(SPAN_OP),
  233. String(SPAN_DESCRIPTION),
  234. `avg_if(${SPAN_SELF_TIME},release,${primaryRelease})`,
  235. `avg_if(${SPAN_SELF_TIME},release,${secondaryRelease})`,
  236. `avg_compare(${SPAN_SELF_TIME},release,${primaryRelease},${secondaryRelease})`,
  237. ].map(col => {
  238. return {key: col, name: columnNameMap[col] ?? col, width: COL_WIDTH_UNDEFINED};
  239. })}
  240. columnSortBy={columnSortBy}
  241. grid={{
  242. renderHeadCell: column => renderHeadCell(column, data?.meta),
  243. renderBodyCell,
  244. }}
  245. />
  246. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  247. </Fragment>
  248. );
  249. }