averageComparisonChart.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {useMemo} from 'react';
  2. import {t} from 'sentry/locale';
  3. import type {NewQuery} from 'sentry/types/organization';
  4. import {defined} from 'sentry/utils';
  5. import type {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  6. import EventView from 'sentry/utils/discover/eventView';
  7. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  8. import {formatVersion} from 'sentry/utils/formatters';
  9. import {decodeScalar} from 'sentry/utils/queryString';
  10. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  11. import {useLocation} from 'sentry/utils/useLocation';
  12. import usePageFilters from 'sentry/utils/usePageFilters';
  13. import {COLD_START_TYPE} from 'sentry/views/performance/mobile/appStarts/screenSummary/startTypeSelector';
  14. import {YAxis, YAXIS_COLUMNS} from 'sentry/views/performance/mobile/screenload/screens';
  15. import {ScreensBarChart} from 'sentry/views/performance/mobile/screenload/screens/screenBarChart';
  16. import {useTableQuery} from 'sentry/views/performance/mobile/screenload/screens/screensTable';
  17. import useTruncatedReleaseNames from 'sentry/views/performance/mobile/useTruncatedRelease';
  18. import {PRIMARY_RELEASE_COLOR} from 'sentry/views/starfish/colors';
  19. import {useReleaseSelection} from 'sentry/views/starfish/queries/useReleases';
  20. import {SpanMetricsField} from 'sentry/views/starfish/types';
  21. import {appendReleaseFilters} from 'sentry/views/starfish/utils/releaseComparison';
  22. interface Props {
  23. chartHeight?: number;
  24. }
  25. function transformData(data: TableDataRow[] | undefined, appStartType: string) {
  26. if (!defined(data)) {
  27. return [];
  28. }
  29. return [
  30. {
  31. seriesName: t('Average Duration'),
  32. data:
  33. data?.map(row => ({
  34. name: formatVersion(row.release as string),
  35. value:
  36. (row[
  37. YAXIS_COLUMNS[
  38. appStartType === COLD_START_TYPE ? YAxis.COLD_START : YAxis.WARM_START
  39. ]
  40. ] as number) ?? 0,
  41. })) ?? [],
  42. itemStyle: {
  43. color: PRIMARY_RELEASE_COLOR,
  44. },
  45. },
  46. ];
  47. }
  48. export function AverageComparisonChart({chartHeight}: Props) {
  49. const location = useLocation();
  50. const {
  51. primaryRelease,
  52. secondaryRelease,
  53. isLoading: isReleasesLoading,
  54. } = useReleaseSelection();
  55. const {selection} = usePageFilters();
  56. const appStartType =
  57. decodeScalar(location.query[SpanMetricsField.APP_START_TYPE]) ?? COLD_START_TYPE;
  58. const query = new MutableSearch([
  59. 'event.type:transaction',
  60. 'transaction.op:ui.load',
  61. `count_starts(measurements.app_start_${appStartType}):>0`,
  62. ]);
  63. const queryString = appendReleaseFilters(query, primaryRelease, secondaryRelease);
  64. const newQuery: NewQuery = {
  65. name: '',
  66. fields: ['release', `avg(measurements.app_start_${appStartType})`],
  67. dataset: DiscoverDatasets.METRICS,
  68. query: queryString,
  69. version: 2,
  70. projects: selection.projects,
  71. };
  72. const tableEventView = EventView.fromNewQueryWithLocation(newQuery, location);
  73. const {data, isLoading} = useTableQuery({
  74. eventView: tableEventView,
  75. enabled: !isReleasesLoading,
  76. referrer: 'api.starfish.mobile-startup-bar-chart',
  77. });
  78. const transformedData = useMemo(() => {
  79. return transformData(data?.data, appStartType);
  80. }, [data, appStartType]);
  81. const {truncatedPrimaryRelease, truncatedSecondaryRelease} = useTruncatedReleaseNames();
  82. return (
  83. <ScreensBarChart
  84. chartOptions={[
  85. {
  86. title:
  87. appStartType === COLD_START_TYPE
  88. ? t('Average Cold Start')
  89. : t('Average Warm Start'),
  90. yAxis: `avg(measurements.app_start_${appStartType})`,
  91. xAxisLabel: [
  92. ...(primaryRelease ? [formatVersion(primaryRelease)] : []),
  93. ...(secondaryRelease ? [formatVersion(secondaryRelease)] : []),
  94. ],
  95. series: transformedData,
  96. subtitle: primaryRelease
  97. ? t(
  98. '%s v. %s',
  99. truncatedPrimaryRelease,
  100. secondaryRelease ? truncatedSecondaryRelease : ''
  101. )
  102. : '',
  103. },
  104. ]}
  105. chartHeight={chartHeight}
  106. isLoading={isLoading || isReleasesLoading}
  107. chartKey={`averageStart`}
  108. />
  109. );
  110. }