trendChart.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {Component, Fragment} from 'react';
  2. import * as ReactRouter from 'react-router';
  3. import {browserHistory} from 'react-router';
  4. import {withTheme} from '@emotion/react';
  5. import {Location, Query} from 'history';
  6. import {Client} from 'app/api';
  7. import ChartZoom from 'app/components/charts/chartZoom';
  8. import ErrorPanel from 'app/components/charts/errorPanel';
  9. import EventsRequest from 'app/components/charts/eventsRequest';
  10. import LineChart from 'app/components/charts/lineChart';
  11. import ReleaseSeries from 'app/components/charts/releaseSeries';
  12. import {HeaderTitleLegend} from 'app/components/charts/styles';
  13. import TransitionChart from 'app/components/charts/transitionChart';
  14. import TransparentLoadingMask from 'app/components/charts/transparentLoadingMask';
  15. import {getInterval, getSeriesSelection} from 'app/components/charts/utils';
  16. import {getParams} from 'app/components/organizations/globalSelectionHeader/getParams';
  17. import Placeholder from 'app/components/placeholder';
  18. import QuestionTooltip from 'app/components/questionTooltip';
  19. import {IconWarning} from 'app/icons';
  20. import {t} from 'app/locale';
  21. import {OrganizationSummary} from 'app/types';
  22. import {getUtcToLocalDateObject} from 'app/utils/dates';
  23. import {axisLabelFormatter, tooltipFormatter} from 'app/utils/discover/charts';
  24. import EventView from 'app/utils/discover/eventView';
  25. import getDynamicText from 'app/utils/getDynamicText';
  26. import {Theme} from 'app/utils/theme';
  27. import withApi from 'app/utils/withApi';
  28. import {transformEventStatsSmoothed} from '../trends/utils';
  29. const QUERY_KEYS = [
  30. 'environment',
  31. 'project',
  32. 'query',
  33. 'start',
  34. 'end',
  35. 'statsPeriod',
  36. ] as const;
  37. type ViewProps = Pick<EventView, typeof QUERY_KEYS[number]>;
  38. type Props = ReactRouter.WithRouterProps &
  39. ViewProps & {
  40. theme: Theme;
  41. api: Client;
  42. location: Location;
  43. organization: OrganizationSummary;
  44. queryExtra: Query;
  45. trendDisplay: string;
  46. };
  47. class TrendChart extends Component<Props> {
  48. handleLegendSelectChanged = legendChange => {
  49. const {location} = this.props;
  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. trendsUnselectedSeries: unselected,
  57. },
  58. };
  59. browserHistory.push(to);
  60. };
  61. render() {
  62. const {
  63. theme,
  64. api,
  65. project,
  66. environment,
  67. location,
  68. organization,
  69. query,
  70. statsPeriod,
  71. router,
  72. trendDisplay,
  73. queryExtra,
  74. } = this.props;
  75. const start = this.props.start ? getUtcToLocalDateObject(this.props.start) : null;
  76. const end = this.props.end ? getUtcToLocalDateObject(this.props.end) : null;
  77. const {utc} = getParams(location.query);
  78. const legend = {
  79. right: 10,
  80. top: 0,
  81. selected: getSeriesSelection(location, 'trendsUnselectedSeries'),
  82. };
  83. const datetimeSelection = {
  84. start,
  85. end,
  86. period: statsPeriod,
  87. };
  88. const chartOptions = {
  89. grid: {
  90. left: '10px',
  91. right: '10px',
  92. top: '40px',
  93. bottom: '0px',
  94. },
  95. seriesOptions: {
  96. showSymbol: false,
  97. },
  98. tooltip: {
  99. trigger: 'axis' as const,
  100. valueFormatter: value => tooltipFormatter(value, 'p50()'),
  101. },
  102. yAxis: {
  103. min: 0,
  104. axisLabel: {
  105. color: theme.chartLabel,
  106. // p50() coerces the axis to be time based
  107. formatter: (value: number) => axisLabelFormatter(value, 'p50()'),
  108. },
  109. },
  110. };
  111. return (
  112. <Fragment>
  113. <HeaderTitleLegend>
  114. {t('Trend')}
  115. <QuestionTooltip
  116. size="sm"
  117. position="top"
  118. title={t(`Trends shows the smoothed value of an aggregate over time.`)}
  119. />
  120. </HeaderTitleLegend>
  121. <ChartZoom
  122. router={router}
  123. period={statsPeriod}
  124. start={start}
  125. end={end}
  126. utc={utc === 'true'}
  127. >
  128. {zoomRenderProps => (
  129. <EventsRequest
  130. api={api}
  131. organization={organization}
  132. period={statsPeriod}
  133. project={project}
  134. environment={environment}
  135. start={start}
  136. end={end}
  137. interval={getInterval(datetimeSelection, true)}
  138. showLoading={false}
  139. query={query}
  140. includePrevious={false}
  141. yAxis={trendDisplay}
  142. currentSeriesName={trendDisplay}
  143. partial
  144. >
  145. {({errored, loading, reloading, timeseriesData}) => {
  146. if (errored) {
  147. return (
  148. <ErrorPanel>
  149. <IconWarning color="gray300" size="lg" />
  150. </ErrorPanel>
  151. );
  152. }
  153. const series = timeseriesData
  154. ? timeseriesData
  155. .map(values => {
  156. return {
  157. ...values,
  158. color: theme.purple300,
  159. lineStyle: {
  160. opacity: 0.75,
  161. width: 1,
  162. },
  163. };
  164. })
  165. .reverse()
  166. : [];
  167. const {smoothedResults} = transformEventStatsSmoothed(
  168. timeseriesData,
  169. t('Smoothed')
  170. );
  171. const smoothedSeries = smoothedResults
  172. ? smoothedResults.map(values => {
  173. return {
  174. ...values,
  175. color: theme.purple300,
  176. lineStyle: {
  177. opacity: 1,
  178. },
  179. };
  180. })
  181. : [];
  182. return (
  183. <ReleaseSeries
  184. start={start}
  185. end={end}
  186. queryExtra={queryExtra}
  187. period={statsPeriod}
  188. utc={utc === 'true'}
  189. projects={project}
  190. environments={environment}
  191. >
  192. {({releaseSeries}) => (
  193. <TransitionChart loading={loading} reloading={reloading}>
  194. <TransparentLoadingMask visible={reloading} />
  195. {getDynamicText({
  196. value: (
  197. <LineChart
  198. {...zoomRenderProps}
  199. {...chartOptions}
  200. legend={legend}
  201. onLegendSelectChanged={this.handleLegendSelectChanged}
  202. series={[...series, ...smoothedSeries, ...releaseSeries]}
  203. />
  204. ),
  205. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  206. })}
  207. </TransitionChart>
  208. )}
  209. </ReleaseSeries>
  210. );
  211. }}
  212. </EventsRequest>
  213. )}
  214. </ChartZoom>
  215. </Fragment>
  216. );
  217. }
  218. }
  219. export default withApi(withTheme(ReactRouter.withRouter(TrendChart)));