spanOperationTable.tsx 8.8 KB

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