resultsChart.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. organization={organization}
  128. showLegend
  129. yAxis={yAxisValue}
  130. projects={globalSelection.projects}
  131. environments={globalSelection.environments}
  132. start={start}
  133. end={end}
  134. period={globalSelection.datetime.period}
  135. disablePrevious={!isPrevious}
  136. disableReleases={!isPeriod}
  137. field={isTopEvents ? apiPayload.field : undefined}
  138. interval={interval}
  139. showDaily={isDaily}
  140. topEvents={isTopEvents ? topEvents : undefined}
  141. orderby={isTopEvents ? decodeScalar(apiPayload.sort) : undefined}
  142. utc={utc === 'true'}
  143. confirmedQuery={confirmedQuery}
  144. withoutZerofill={hasPerformanceChartInterpolation}
  145. chartComponent={chartComponent}
  146. referrer={referrer}
  147. fromDiscover
  148. disableableSeries={disableableSeries}
  149. />
  150. ),
  151. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  152. })}
  153. </Fragment>
  154. );
  155. }
  156. }
  157. type ContainerProps = {
  158. api: Client;
  159. confirmedQuery: boolean;
  160. eventView: EventView;
  161. location: Location;
  162. onAxisChange: (value: string[]) => void;
  163. onDisplayChange: (value: string) => void;
  164. onIntervalChange: (value: string | undefined) => void;
  165. onTopEventsChange: (value: string) => void;
  166. organization: Organization;
  167. router: InjectedRouter;
  168. // chart footer props
  169. total: number | null;
  170. yAxis: string[];
  171. };
  172. type ContainerState = {
  173. yAxisOptions: SelectValue<string>[];
  174. };
  175. class ResultsChartContainer extends Component<ContainerProps, ContainerState> {
  176. state: ContainerState = {
  177. yAxisOptions: this.getYAxisOptions(this.props.eventView),
  178. };
  179. componentWillReceiveProps(nextProps) {
  180. const yAxisOptions = this.getYAxisOptions(this.props.eventView);
  181. const nextYAxisOptions = this.getYAxisOptions(nextProps.eventView);
  182. if (!valueIsEqual(yAxisOptions, nextYAxisOptions, true)) {
  183. this.setState({yAxisOptions: nextYAxisOptions});
  184. }
  185. }
  186. shouldComponentUpdate(nextProps: ContainerProps) {
  187. const {eventView, ...restProps} = this.props;
  188. const {eventView: nextEventView, ...restNextProps} = nextProps;
  189. if (
  190. !eventView.isEqualTo(nextEventView) ||
  191. this.props.confirmedQuery !== nextProps.confirmedQuery
  192. ) {
  193. return true;
  194. }
  195. return !isEqual(restProps, restNextProps);
  196. }
  197. getYAxisOptions(eventView) {
  198. const yAxisOptions = eventView.getYAxisOptions();
  199. // Equations on World Map isn't supported on the events-geo endpoint
  200. // Disabling equations as an option to prevent erroring out
  201. if (eventView.getDisplayMode() === DisplayModes.WORLDMAP) {
  202. return yAxisOptions.filter(({value}) => !isEquation(value));
  203. }
  204. return yAxisOptions;
  205. }
  206. render() {
  207. const {
  208. api,
  209. eventView,
  210. location,
  211. router,
  212. total,
  213. onAxisChange,
  214. onDisplayChange,
  215. onIntervalChange,
  216. onTopEventsChange,
  217. organization,
  218. confirmedQuery,
  219. yAxis,
  220. } = this.props;
  221. const {yAxisOptions} = this.state;
  222. const hasQueryFeature = organization.features.includes('discover-query');
  223. const displayOptions = eventView
  224. .getDisplayOptions()
  225. .filter(opt => {
  226. // top5 modes are only available with larger packages in saas.
  227. // We remove instead of disable here as showing tooltips in dropdown
  228. // menus is clunky.
  229. if (TOP_EVENT_MODES.includes(opt.value) && !hasQueryFeature) {
  230. return false;
  231. }
  232. return true;
  233. })
  234. .map(opt => {
  235. // Can only use default display or total daily with multi y axis
  236. if (TOP_EVENT_MODES.includes(opt.value)) {
  237. opt.label = DisplayModes.TOP5 === opt.value ? 'Top Period' : 'Top Daily';
  238. }
  239. if (
  240. yAxis.length > 1 &&
  241. !MULTI_Y_AXIS_SUPPORTED_DISPLAY_MODES.includes(opt.value as DisplayModes)
  242. ) {
  243. return {
  244. ...opt,
  245. disabled: true,
  246. tooltip: t(
  247. 'Change the Y-Axis dropdown to display only 1 function to use this view.'
  248. ),
  249. };
  250. }
  251. return opt;
  252. });
  253. return (
  254. <StyledPanel>
  255. {(yAxis.length > 0 && (
  256. <CustomMeasurementsContext.Consumer>
  257. {contextValue => (
  258. <ResultsChart
  259. api={api}
  260. eventView={eventView}
  261. location={location}
  262. organization={organization}
  263. router={router}
  264. confirmedQuery={confirmedQuery}
  265. yAxisValue={yAxis}
  266. customMeasurements={contextValue?.customMeasurements}
  267. />
  268. )}
  269. </CustomMeasurementsContext.Consumer>
  270. )) || <NoChartContainer>{t('No Y-Axis selected.')}</NoChartContainer>}
  271. <ChartFooter
  272. total={total}
  273. yAxisValue={yAxis}
  274. yAxisOptions={yAxisOptions}
  275. eventView={eventView}
  276. onAxisChange={onAxisChange}
  277. displayOptions={displayOptions}
  278. displayMode={eventView.getDisplayMode()}
  279. onDisplayChange={onDisplayChange}
  280. onTopEventsChange={onTopEventsChange}
  281. onIntervalChange={onIntervalChange}
  282. topEvents={eventView.topEvents ?? TOP_N.toString()}
  283. />
  284. </StyledPanel>
  285. );
  286. }
  287. }
  288. export default withApi(ResultsChartContainer);
  289. const StyledPanel = styled(Panel)`
  290. @media (min-width: ${p => p.theme.breakpoints.large}) {
  291. margin: 0;
  292. }
  293. `;
  294. const NoChartContainer = styled('div')<{height?: string}>`
  295. display: flex;
  296. flex-direction: column;
  297. justify-content: center;
  298. align-items: center;
  299. flex: 1;
  300. flex-shrink: 0;
  301. overflow: hidden;
  302. height: ${p => p.height || '200px'};
  303. position: relative;
  304. border-color: transparent;
  305. margin-bottom: 0;
  306. color: ${p => p.theme.gray300};
  307. font-size: ${p => p.theme.fontSizeExtraLarge};
  308. `;