resultsChart.tsx 9.3 KB

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