resultsChart.tsx 11 KB

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