index.tsx 3.8 KB

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