index.tsx 4.6 KB

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