index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {useTheme} from '@emotion/react';
  4. import {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} from 'sentry/locale';
  11. import {OrganizationSummary} from 'sentry/types';
  12. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  13. import useApi from 'sentry/utils/useApi';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import useRouter from 'sentry/utils/useRouter';
  16. import {TrendFunctionField} from '../../../trends/types';
  17. import {generateTrendFunctionAsString} from '../../../trends/utils';
  18. import {ViewProps} from '../../../types';
  19. import Content from './content';
  20. type Props = ViewProps & {
  21. organization: OrganizationSummary;
  22. queryExtra: Query;
  23. trendFunction: TrendFunctionField;
  24. trendParameter: string;
  25. withoutZerofill: boolean;
  26. };
  27. function TrendChart({
  28. project,
  29. environment,
  30. organization,
  31. query,
  32. statsPeriod,
  33. trendFunction,
  34. trendParameter,
  35. queryExtra,
  36. withoutZerofill,
  37. start: propsStart,
  38. end: propsEnd,
  39. }: Props) {
  40. const router = useRouter();
  41. const location = useLocation();
  42. const api = useApi();
  43. const theme = useTheme();
  44. function handleLegendSelectChanged(legendChange: {
  45. name: string;
  46. selected: Record<string, boolean>;
  47. type: string;
  48. }) {
  49. const {selected} = legendChange;
  50. const unselected = Object.keys(selected).filter(key => !selected[key]);
  51. const to = {
  52. ...location,
  53. query: {
  54. ...location.query,
  55. unselectedSeries: unselected,
  56. },
  57. };
  58. browserHistory.push(to);
  59. }
  60. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  61. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  62. const utc = normalizeDateTimeParams(location.query)?.utc === 'true';
  63. const period = statsPeriod;
  64. const legend = {
  65. right: 10,
  66. top: 0,
  67. selected: getSeriesSelection(location),
  68. };
  69. const datetimeSelection = {start, end, period};
  70. const contentCommonProps = {
  71. theme,
  72. router,
  73. start,
  74. end,
  75. utc,
  76. legend,
  77. queryExtra,
  78. period,
  79. projects: project,
  80. environments: environment,
  81. onLegendSelectChanged: handleLegendSelectChanged,
  82. };
  83. const requestCommonProps = {
  84. api,
  85. start,
  86. end,
  87. project,
  88. environment,
  89. query,
  90. period,
  91. interval: getInterval(datetimeSelection, 'high'),
  92. };
  93. const header = (
  94. <HeaderTitleLegend>
  95. {t('Trend')}
  96. <QuestionTooltip
  97. size="sm"
  98. position="top"
  99. title={t('Trends shows the smoothed value of an aggregate over time.')}
  100. />
  101. </HeaderTitleLegend>
  102. );
  103. const trendDisplay = generateTrendFunctionAsString(trendFunction, trendParameter);
  104. return (
  105. <Fragment>
  106. {header}
  107. <EventsRequest
  108. {...requestCommonProps}
  109. organization={organization}
  110. showLoading={false}
  111. includePrevious={false}
  112. yAxis={trendDisplay}
  113. currentSeriesNames={[trendDisplay]}
  114. partial
  115. withoutZerofill={withoutZerofill}
  116. referrer="api.performance.transaction-summary.trends-chart"
  117. >
  118. {({errored, loading, reloading, timeseriesData, timeframe: timeFrame}) => (
  119. <Content
  120. series={timeseriesData}
  121. errored={errored}
  122. loading={loading}
  123. reloading={reloading}
  124. timeFrame={timeFrame}
  125. {...contentCommonProps}
  126. />
  127. )}
  128. </EventsRequest>
  129. </Fragment>
  130. );
  131. }
  132. export default TrendChart;