spanOperationTable.tsx 9.1 KB

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