resultsChart.tsx 12 KB

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