miniGraph.tsx 8.4 KB

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