spanOperationTable.tsx 9.1 KB

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