index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import type {Query} from 'history';
  4. import EventsRequest from 'sentry/components/charts/eventsRequest';
  5. import {HeaderTitleLegend} from 'sentry/components/charts/styles';
  6. import {getInterval, getSeriesSelection} from 'sentry/components/charts/utils';
  7. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import {t, tct} from 'sentry/locale';
  10. import type {OrganizationSummary} from 'sentry/types/organization';
  11. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  12. import {parseFunction} from 'sentry/utils/discover/fields';
  13. import useApi from 'sentry/utils/useApi';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import {useNavigate} from 'sentry/utils/useNavigate';
  16. import useRouter from 'sentry/utils/useRouter';
  17. import type {ViewProps} from '../../../types';
  18. import {
  19. SPAN_OPERATION_BREAKDOWN_FILTER_TO_FIELD,
  20. SpanOperationBreakdownFilter,
  21. } from '../../filter';
  22. import Content from './content';
  23. type Props = ViewProps & {
  24. currentFilter: SpanOperationBreakdownFilter;
  25. organization: OrganizationSummary;
  26. queryExtra: Query;
  27. withoutZerofill: boolean;
  28. queryExtras?: Record<string, string>;
  29. };
  30. const yAxisValues = ['p50', 'p75', 'p95', 'p99', 'p100', 'avg'];
  31. /**
  32. * Fetch and render a stacked area chart that shows duration percentiles over
  33. * the past 7 days
  34. */
  35. function DurationChart({
  36. project,
  37. environment,
  38. organization,
  39. query,
  40. statsPeriod,
  41. queryExtra,
  42. currentFilter,
  43. withoutZerofill,
  44. start: propsStart,
  45. end: propsEnd,
  46. queryExtras,
  47. }: Props) {
  48. const navigate = useNavigate();
  49. const router = useRouter();
  50. const location = useLocation();
  51. const api = useApi();
  52. const theme = useTheme();
  53. function handleLegendSelectChanged(legendChange: {
  54. name: string;
  55. selected: Record<string, boolean>;
  56. type: string;
  57. }) {
  58. const {selected} = legendChange;
  59. const unselected = Object.keys(selected).filter(key => !selected[key]);
  60. const to = {
  61. ...location,
  62. query: {
  63. ...location.query,
  64. unselectedSeries: unselected,
  65. },
  66. };
  67. navigate(to);
  68. }
  69. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  70. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  71. const utc = normalizeDateTimeParams(location.query).utc === 'true';
  72. const period = statsPeriod;
  73. const legend = {right: 10, top: 5, selected: getSeriesSelection(location)};
  74. const datetimeSelection = {start, end, period};
  75. const contentCommonProps = {
  76. theme,
  77. router,
  78. start,
  79. end,
  80. utc,
  81. legend,
  82. queryExtra,
  83. period,
  84. projects: project,
  85. environments: environment,
  86. onLegendSelectChanged: handleLegendSelectChanged,
  87. };
  88. const requestCommonProps = {
  89. api,
  90. start,
  91. end,
  92. project,
  93. environment,
  94. query,
  95. period,
  96. interval: getInterval(datetimeSelection, 'high'),
  97. };
  98. const parameter =
  99. SPAN_OPERATION_BREAKDOWN_FILTER_TO_FIELD[currentFilter] ?? 'transaction.duration';
  100. const header = (
  101. <HeaderTitleLegend>
  102. {currentFilter === SpanOperationBreakdownFilter.NONE
  103. ? t('Duration Breakdown')
  104. : tct('Span Operation Breakdown - [operationName]', {
  105. operationName: currentFilter,
  106. })}
  107. <QuestionTooltip
  108. size="sm"
  109. position="top"
  110. title={t(
  111. `Duration Breakdown reflects transaction durations by percentile over time.`
  112. )}
  113. />
  114. </HeaderTitleLegend>
  115. );
  116. const yAxis = yAxisValues.map(v => `${v}(${parameter})`);
  117. return (
  118. <Fragment>
  119. {header}
  120. <EventsRequest
  121. {...requestCommonProps}
  122. organization={organization}
  123. showLoading={false}
  124. includePrevious={false}
  125. yAxis={yAxis}
  126. partial
  127. withoutZerofill={withoutZerofill}
  128. referrer="api.performance.transaction-summary.duration-chart"
  129. queryExtras={queryExtras}
  130. >
  131. {({results, errored, loading, reloading, timeframe: timeFrame}) => {
  132. const stripParamsForLegend = seriesResults =>
  133. seriesResults?.map(series => ({
  134. ...series,
  135. seriesName: `${parseFunction(series.seriesName)?.name}()`,
  136. }));
  137. return (
  138. <Content
  139. series={stripParamsForLegend(results)}
  140. errored={errored}
  141. loading={loading}
  142. reloading={reloading}
  143. timeFrame={timeFrame}
  144. {...contentCommonProps}
  145. />
  146. );
  147. }}
  148. </EventsRequest>
  149. </Fragment>
  150. );
  151. }
  152. export default DurationChart;