resultsChart.tsx 8.8 KB

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