miniGraph.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import {Component} from 'react';
  2. import type {Theme} from '@emotion/react';
  3. import {withTheme} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import type {Location} from 'history';
  6. import isEqual from 'lodash/isEqual';
  7. import type {Client} from 'sentry/api';
  8. import type {AreaChartProps} from 'sentry/components/charts/areaChart';
  9. import {AreaChart} from 'sentry/components/charts/areaChart';
  10. import type {BarChartProps} from 'sentry/components/charts/barChart';
  11. import {BarChart} from 'sentry/components/charts/barChart';
  12. import EventsRequest from 'sentry/components/charts/eventsRequest';
  13. import {LineChart} from 'sentry/components/charts/lineChart';
  14. import {getInterval} from 'sentry/components/charts/utils';
  15. import LoadingContainer from 'sentry/components/loading/loadingContainer';
  16. import LoadingIndicator from 'sentry/components/loadingIndicator';
  17. import {getChartColorPalette} from 'sentry/constants/chartPalette';
  18. import {IconWarning} from 'sentry/icons';
  19. import type {Series} from 'sentry/types/echarts';
  20. import type {Organization} from 'sentry/types/organization';
  21. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  22. import {axisLabelFormatter} from 'sentry/utils/discover/charts';
  23. import type EventView from 'sentry/utils/discover/eventView';
  24. import type {PlotType} from 'sentry/utils/discover/fields';
  25. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  26. import {DisplayModes, TOP_N} from 'sentry/utils/discover/types';
  27. import {decodeScalar} from 'sentry/utils/queryString';
  28. import withApi from 'sentry/utils/withApi';
  29. type Props = {
  30. api: Client;
  31. eventView: EventView;
  32. location: Location;
  33. organization: Organization;
  34. theme: Theme;
  35. referrer?: string;
  36. yAxis?: string[];
  37. };
  38. class MiniGraph extends Component<Props> {
  39. shouldComponentUpdate(nextProps: any) {
  40. // We pay for the cost of the deep comparison here since it is cheaper
  41. // than the cost for rendering the graph, which can take ~200ms to ~300ms to
  42. // render.
  43. return !isEqual(this.getRefreshProps(this.props), this.getRefreshProps(nextProps));
  44. }
  45. getRefreshProps(props: Props) {
  46. // get props that are relevant to the API payload for the graph
  47. const {organization, location, eventView, yAxis} = props;
  48. const apiPayload = eventView.getEventsAPIPayload(location);
  49. const query = apiPayload.query;
  50. const start = apiPayload.start ? getUtcToLocalDateObject(apiPayload.start) : null;
  51. const end = apiPayload.end ? getUtcToLocalDateObject(apiPayload.end) : null;
  52. const period: string | undefined = apiPayload.statsPeriod as any;
  53. const display = eventView.getDisplayMode();
  54. const isTopEvents =
  55. display === DisplayModes.TOP5 || display === DisplayModes.DAILYTOP5;
  56. const isDaily = display === DisplayModes.DAILYTOP5 || display === DisplayModes.DAILY;
  57. const field = isTopEvents ? apiPayload.field : undefined;
  58. const topEvents = isTopEvents ? TOP_N : undefined;
  59. const orderby = isTopEvents ? decodeScalar(apiPayload.sort) : undefined;
  60. const intervalFidelity = display === 'bar' ? 'low' : 'high';
  61. const interval = isDaily
  62. ? '1d'
  63. : eventView.interval
  64. ? eventView.interval
  65. : getInterval({start, end, period}, intervalFidelity);
  66. return {
  67. organization,
  68. apiPayload,
  69. query,
  70. start,
  71. end,
  72. period,
  73. interval,
  74. project: eventView.project,
  75. environment: eventView.environment,
  76. yAxis: yAxis ?? eventView.getYAxis(),
  77. field,
  78. topEvents,
  79. orderby,
  80. showDaily: isDaily,
  81. expired: eventView.expired,
  82. name: eventView.name,
  83. display,
  84. dataset: eventView.dataset,
  85. };
  86. }
  87. getChartType({
  88. showDaily,
  89. }: {
  90. showDaily: boolean;
  91. timeseriesData: Series[];
  92. yAxis: string;
  93. }): PlotType {
  94. if (showDaily) {
  95. return 'bar';
  96. }
  97. return 'area';
  98. }
  99. getChartComponent(
  100. chartType: PlotType
  101. ): React.ComponentType<BarChartProps> | React.ComponentType<AreaChartProps> {
  102. switch (chartType) {
  103. case 'bar':
  104. return BarChart;
  105. case 'line':
  106. return LineChart;
  107. case 'area':
  108. return AreaChart;
  109. default:
  110. throw new Error(`Unknown multi plot type for ${chartType}`);
  111. }
  112. }
  113. render() {
  114. const {theme, api, referrer} = this.props;
  115. const {
  116. query,
  117. start,
  118. end,
  119. period,
  120. interval,
  121. organization,
  122. project,
  123. environment,
  124. yAxis,
  125. field,
  126. topEvents,
  127. orderby,
  128. showDaily,
  129. expired,
  130. name,
  131. display,
  132. dataset,
  133. } = this.getRefreshProps(this.props);
  134. return (
  135. <EventsRequest
  136. organization={organization}
  137. api={api}
  138. query={query}
  139. start={start}
  140. end={end}
  141. period={period}
  142. interval={interval}
  143. project={project as number[]}
  144. environment={environment as string[]}
  145. includePrevious={false}
  146. yAxis={yAxis}
  147. field={field}
  148. topEvents={topEvents}
  149. orderby={orderby}
  150. expired={expired}
  151. name={name}
  152. referrer={referrer}
  153. dataset={dataset}
  154. hideError
  155. partial
  156. >
  157. {({loading, timeseriesData, results, errored, errorMessage}) => {
  158. if (errored) {
  159. return (
  160. <StyledGraphContainer>
  161. <IconWarning color="gray300" size="md" />
  162. <StyledErrorMessage>{errorMessage}</StyledErrorMessage>
  163. </StyledGraphContainer>
  164. );
  165. }
  166. if (loading) {
  167. return (
  168. <StyledGraphContainer>
  169. <LoadingIndicator mini />
  170. </StyledGraphContainer>
  171. );
  172. }
  173. const allSeries = timeseriesData ?? results ?? [];
  174. const chartType =
  175. display === 'bar'
  176. ? display
  177. : this.getChartType({
  178. showDaily,
  179. yAxis: Array.isArray(yAxis) ? yAxis[0]! : yAxis,
  180. timeseriesData: allSeries,
  181. });
  182. const data = allSeries.map(series => ({
  183. ...series,
  184. lineStyle: {
  185. opacity: chartType === 'line' ? 1 : 0,
  186. },
  187. }));
  188. const hasOther = topEvents && topEvents + 1 === allSeries.length;
  189. const chartColors = allSeries.length
  190. ? (getChartColorPalette(
  191. allSeries.length - 2 - (hasOther ? 1 : 0)
  192. ).slice() as string[])
  193. : undefined;
  194. if (chartColors?.length && hasOther) {
  195. chartColors.push(theme.chartOther);
  196. }
  197. const chartOptions = {
  198. colors: chartColors,
  199. height: 150,
  200. series: [...data],
  201. xAxis: {
  202. show: false,
  203. axisPointer: {
  204. show: false,
  205. },
  206. },
  207. yAxis: {
  208. show: true,
  209. axisLine: {
  210. show: false,
  211. },
  212. axisLabel: {
  213. color: theme.chartLabel,
  214. fontFamily: theme.text.family,
  215. fontSize: 12,
  216. formatter: (value: number) =>
  217. axisLabelFormatter(
  218. value,
  219. aggregateOutputType(Array.isArray(yAxis) ? yAxis[0] : yAxis),
  220. true
  221. ),
  222. inside: true,
  223. showMinLabel: false,
  224. showMaxLabel: false,
  225. },
  226. splitNumber: 3,
  227. splitLine: {
  228. show: false,
  229. },
  230. zlevel: theme.zIndex.header,
  231. },
  232. tooltip: {
  233. show: false,
  234. },
  235. toolBox: {
  236. show: false,
  237. },
  238. grid: {
  239. left: 0,
  240. top: 0,
  241. right: 0,
  242. bottom: 0,
  243. containLabel: false,
  244. },
  245. stacked:
  246. (typeof topEvents === 'number' && topEvents > 0) ||
  247. (Array.isArray(yAxis) && yAxis.length > 1),
  248. };
  249. const ChartComponent = this.getChartComponent(chartType);
  250. return <ChartComponent {...chartOptions} />;
  251. }}
  252. </EventsRequest>
  253. );
  254. }
  255. }
  256. const StyledGraphContainer = styled((props: any) => (
  257. <LoadingContainer {...props} maskBackgroundColor="transparent" />
  258. ))`
  259. height: 150px;
  260. display: flex;
  261. justify-content: center;
  262. align-items: center;
  263. `;
  264. const StyledErrorMessage = styled('div')`
  265. color: ${p => p.theme.gray300};
  266. margin-left: 4px;
  267. `;
  268. export default withApi(withTheme(MiniGraph));