resultsChart.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import {Component, Fragment} from 'react';
  2. import type {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import isEqual from 'lodash/isEqual';
  6. import type {Client} from 'sentry/api';
  7. import {AreaChart} from 'sentry/components/charts/areaChart';
  8. import {BarChart} from 'sentry/components/charts/barChart';
  9. import EventsChart from 'sentry/components/charts/eventsChart';
  10. import {getInterval, getPreviousSeriesName} from 'sentry/components/charts/utils';
  11. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  12. import Panel from 'sentry/components/panels/panel';
  13. import Placeholder from 'sentry/components/placeholder';
  14. import {t} from 'sentry/locale';
  15. import type {SelectValue} from 'sentry/types/core';
  16. import type {Organization} from 'sentry/types/organization';
  17. import type {CustomMeasurementCollection} from 'sentry/utils/customMeasurements/customMeasurements';
  18. import {CustomMeasurementsContext} from 'sentry/utils/customMeasurements/customMeasurementsContext';
  19. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  20. import type EventView from 'sentry/utils/discover/eventView';
  21. import {getAggregateArg, stripEquationPrefix} from 'sentry/utils/discover/fields';
  22. import {
  23. DisplayModes,
  24. MULTI_Y_AXIS_SUPPORTED_DISPLAY_MODES,
  25. TOP_EVENT_MODES,
  26. TOP_N,
  27. } from 'sentry/utils/discover/types';
  28. import getDynamicText from 'sentry/utils/getDynamicText';
  29. import {valueIsEqual} from 'sentry/utils/object/valueIsEqual';
  30. import {decodeScalar} from 'sentry/utils/queryString';
  31. import withApi from 'sentry/utils/withApi';
  32. import {isCustomMeasurement} from '../dashboards/utils';
  33. import ChartFooter from './chartFooter';
  34. type ResultsChartProps = {
  35. api: Client;
  36. confirmedQuery: boolean;
  37. eventView: EventView;
  38. location: Location;
  39. organization: Organization;
  40. router: InjectedRouter;
  41. yAxisValue: string[];
  42. customMeasurements?: CustomMeasurementCollection | undefined;
  43. };
  44. class ResultsChart extends Component<ResultsChartProps> {
  45. shouldComponentUpdate(nextProps: ResultsChartProps) {
  46. const {eventView, ...restProps} = this.props;
  47. const {eventView: nextEventView, ...restNextProps} = nextProps;
  48. if (!eventView.isEqualTo(nextEventView)) {
  49. return true;
  50. }
  51. return !isEqual(restProps, restNextProps);
  52. }
  53. render() {
  54. const {
  55. api,
  56. eventView,
  57. location,
  58. organization,
  59. router,
  60. confirmedQuery,
  61. yAxisValue,
  62. customMeasurements,
  63. } = this.props;
  64. const hasPerformanceChartInterpolation = organization.features.includes(
  65. 'performance-chart-interpolation'
  66. );
  67. const globalSelection = eventView.getPageFilters();
  68. const start = globalSelection.datetime.start
  69. ? getUtcToLocalDateObject(globalSelection.datetime.start)
  70. : null;
  71. const end = globalSelection.datetime.end
  72. ? getUtcToLocalDateObject(globalSelection.datetime.end)
  73. : null;
  74. const {utc} = normalizeDateTimeParams(location.query);
  75. const apiPayload = eventView.getEventsAPIPayload(location);
  76. const display = eventView.getDisplayMode();
  77. const isTopEvents =
  78. display === DisplayModes.TOP5 || display === DisplayModes.DAILYTOP5;
  79. const isPeriod = display === DisplayModes.DEFAULT || display === DisplayModes.TOP5;
  80. const isDaily = display === DisplayModes.DAILYTOP5 || display === DisplayModes.DAILY;
  81. const isPrevious = display === DisplayModes.PREVIOUS;
  82. const referrer = `api.discover.${display}-chart`;
  83. const topEvents = eventView.topEvents ? parseInt(eventView.topEvents, 10) : TOP_N;
  84. const aggregateParam = getAggregateArg(yAxisValue[0]) || '';
  85. const customPerformanceMetricFieldType = isCustomMeasurement(aggregateParam)
  86. ? customMeasurements
  87. ? customMeasurements[aggregateParam]?.fieldType
  88. : null
  89. : null;
  90. const chartComponent =
  91. display === DisplayModes.BAR
  92. ? BarChart
  93. : customPerformanceMetricFieldType === 'size' && isTopEvents
  94. ? AreaChart
  95. : undefined;
  96. const interval =
  97. display === DisplayModes.BAR
  98. ? getInterval(
  99. {
  100. start,
  101. end,
  102. period: globalSelection.datetime.period,
  103. utc: utc === 'true',
  104. },
  105. 'low'
  106. )
  107. : eventView.interval;
  108. const seriesLabels = yAxisValue.map(stripEquationPrefix);
  109. const disableableSeries = [
  110. ...seriesLabels,
  111. ...seriesLabels.map(getPreviousSeriesName),
  112. ];
  113. return (
  114. <Fragment>
  115. {getDynamicText({
  116. value: (
  117. <EventsChart
  118. api={api}
  119. router={router}
  120. query={apiPayload.query}
  121. dataset={apiPayload.dataset}
  122. organization={organization}
  123. showLegend
  124. yAxis={yAxisValue}
  125. projects={globalSelection.projects}
  126. environments={globalSelection.environments}
  127. start={start}
  128. end={end}
  129. period={globalSelection.datetime.period}
  130. disablePrevious={!isPrevious}
  131. disableReleases={!isPeriod}
  132. field={isTopEvents ? apiPayload.field : undefined}
  133. interval={interval}
  134. showDaily={isDaily}
  135. topEvents={isTopEvents ? topEvents : undefined}
  136. orderby={isTopEvents ? decodeScalar(apiPayload.sort) : undefined}
  137. utc={utc === 'true'}
  138. confirmedQuery={confirmedQuery}
  139. withoutZerofill={hasPerformanceChartInterpolation}
  140. chartComponent={chartComponent}
  141. referrer={referrer}
  142. fromDiscover
  143. disableableSeries={disableableSeries}
  144. />
  145. ),
  146. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  147. })}
  148. </Fragment>
  149. );
  150. }
  151. }
  152. type ContainerProps = {
  153. api: Client;
  154. confirmedQuery: boolean;
  155. eventView: EventView;
  156. location: Location;
  157. onAxisChange: (value: string[]) => void;
  158. onDisplayChange: (value: string) => void;
  159. onIntervalChange: (value: string | undefined) => void;
  160. onTopEventsChange: (value: string) => void;
  161. organization: Organization;
  162. router: InjectedRouter;
  163. // chart footer props
  164. total: number | null;
  165. yAxis: string[];
  166. };
  167. type ContainerState = {
  168. yAxisOptions: SelectValue<string>[];
  169. };
  170. class ResultsChartContainer extends Component<ContainerProps, ContainerState> {
  171. state: ContainerState = {
  172. yAxisOptions: this.props.eventView.getYAxisOptions(),
  173. };
  174. UNSAFE_componentWillReceiveProps(nextProps) {
  175. const yAxisOptions = this.props.eventView.getYAxisOptions();
  176. const nextYAxisOptions = nextProps.eventView.getYAxisOptions();
  177. if (!valueIsEqual(yAxisOptions, nextYAxisOptions, true)) {
  178. this.setState({yAxisOptions: nextYAxisOptions});
  179. }
  180. }
  181. shouldComponentUpdate(nextProps: ContainerProps) {
  182. const {eventView, ...restProps} = this.props;
  183. const {eventView: nextEventView, ...restNextProps} = nextProps;
  184. if (
  185. !eventView.isEqualTo(nextEventView) ||
  186. this.props.confirmedQuery !== nextProps.confirmedQuery
  187. ) {
  188. return true;
  189. }
  190. return !isEqual(restProps, restNextProps);
  191. }
  192. render() {
  193. const {
  194. api,
  195. eventView,
  196. location,
  197. router,
  198. total,
  199. onAxisChange,
  200. onDisplayChange,
  201. onIntervalChange,
  202. onTopEventsChange,
  203. organization,
  204. confirmedQuery,
  205. yAxis,
  206. } = this.props;
  207. const {yAxisOptions} = this.state;
  208. const hasQueryFeature = organization.features.includes('discover-query');
  209. const displayOptions = eventView
  210. .getDisplayOptions()
  211. .filter(opt => {
  212. // top5 modes are only available with larger packages in saas.
  213. // We remove instead of disable here as showing tooltips in dropdown
  214. // menus is clunky.
  215. if (TOP_EVENT_MODES.includes(opt.value) && !hasQueryFeature) {
  216. return false;
  217. }
  218. return true;
  219. })
  220. .map(opt => {
  221. // Can only use default display or total daily with multi y axis
  222. if (TOP_EVENT_MODES.includes(opt.value)) {
  223. opt.label = DisplayModes.TOP5 === opt.value ? 'Top Period' : 'Top Daily';
  224. }
  225. if (
  226. yAxis.length > 1 &&
  227. !MULTI_Y_AXIS_SUPPORTED_DISPLAY_MODES.includes(opt.value as DisplayModes)
  228. ) {
  229. return {
  230. ...opt,
  231. disabled: true,
  232. tooltip: t(
  233. 'Change the Y-Axis dropdown to display only 1 function to use this view.'
  234. ),
  235. };
  236. }
  237. return opt;
  238. });
  239. return (
  240. <StyledPanel>
  241. {(yAxis.length > 0 && (
  242. <CustomMeasurementsContext.Consumer>
  243. {contextValue => (
  244. <ResultsChart
  245. api={api}
  246. eventView={eventView}
  247. location={location}
  248. organization={organization}
  249. router={router}
  250. confirmedQuery={confirmedQuery}
  251. yAxisValue={yAxis}
  252. customMeasurements={contextValue?.customMeasurements}
  253. />
  254. )}
  255. </CustomMeasurementsContext.Consumer>
  256. )) || <NoChartContainer>{t('No Y-Axis selected.')}</NoChartContainer>}
  257. <ChartFooter
  258. total={total}
  259. yAxisValue={yAxis}
  260. yAxisOptions={yAxisOptions}
  261. eventView={eventView}
  262. onAxisChange={onAxisChange}
  263. displayOptions={displayOptions}
  264. displayMode={eventView.getDisplayMode()}
  265. onDisplayChange={onDisplayChange}
  266. onTopEventsChange={onTopEventsChange}
  267. onIntervalChange={onIntervalChange}
  268. topEvents={eventView.topEvents ?? TOP_N.toString()}
  269. />
  270. </StyledPanel>
  271. );
  272. }
  273. }
  274. export default withApi(ResultsChartContainer);
  275. const StyledPanel = styled(Panel)`
  276. @media (min-width: ${p => p.theme.breakpoints.large}) {
  277. margin: 0;
  278. }
  279. `;
  280. const NoChartContainer = styled('div')<{height?: string}>`
  281. display: flex;
  282. flex-direction: column;
  283. justify-content: center;
  284. align-items: center;
  285. flex: 1;
  286. flex-shrink: 0;
  287. overflow: hidden;
  288. height: ${p => p.height || '200px'};
  289. position: relative;
  290. border-color: transparent;
  291. margin-bottom: 0;
  292. color: ${p => p.theme.gray300};
  293. font-size: ${p => p.theme.fontSizeExtraLarge};
  294. `;