resultsChart.tsx 11 KB

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