spanOperationTable.tsx 9.4 KB

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