miniGraph.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. };
  84. }
  85. getChartType({
  86. showDaily,
  87. }: {
  88. showDaily: boolean;
  89. timeseriesData: Series[];
  90. yAxis: string;
  91. }): PlotType {
  92. if (showDaily) {
  93. return 'bar';
  94. }
  95. return 'area';
  96. }
  97. getChartComponent(
  98. chartType: PlotType
  99. ): React.ComponentType<BarChartProps> | React.ComponentType<AreaChartProps> {
  100. switch (chartType) {
  101. case 'bar':
  102. return BarChart;
  103. case 'line':
  104. return LineChart;
  105. case 'area':
  106. return AreaChart;
  107. default:
  108. throw new Error(`Unknown multi plot type for ${chartType}`);
  109. }
  110. }
  111. render() {
  112. const {theme, api, referrer} = this.props;
  113. const {
  114. query,
  115. start,
  116. end,
  117. period,
  118. interval,
  119. organization,
  120. project,
  121. environment,
  122. yAxis,
  123. field,
  124. topEvents,
  125. orderby,
  126. showDaily,
  127. expired,
  128. name,
  129. display,
  130. } = this.getRefreshProps(this.props);
  131. return (
  132. <EventsRequest
  133. organization={organization}
  134. api={api}
  135. query={query}
  136. start={start}
  137. end={end}
  138. period={period}
  139. interval={interval}
  140. project={project as number[]}
  141. environment={environment as string[]}
  142. includePrevious={false}
  143. yAxis={yAxis}
  144. field={field}
  145. topEvents={topEvents}
  146. orderby={orderby}
  147. expired={expired}
  148. name={name}
  149. referrer={referrer}
  150. hideError
  151. partial
  152. >
  153. {({loading, timeseriesData, results, errored, errorMessage}) => {
  154. if (errored) {
  155. return (
  156. <StyledGraphContainer>
  157. <IconWarning color="gray300" size="md" />
  158. <StyledErrorMessage>{errorMessage}</StyledErrorMessage>
  159. </StyledGraphContainer>
  160. );
  161. }
  162. if (loading) {
  163. return (
  164. <StyledGraphContainer>
  165. <LoadingIndicator mini />
  166. </StyledGraphContainer>
  167. );
  168. }
  169. const allSeries = timeseriesData ?? results ?? [];
  170. const chartType =
  171. display === 'bar'
  172. ? display
  173. : this.getChartType({
  174. showDaily,
  175. yAxis: Array.isArray(yAxis) ? yAxis[0] : yAxis,
  176. timeseriesData: allSeries,
  177. });
  178. const data = allSeries.map(series => ({
  179. ...series,
  180. lineStyle: {
  181. opacity: chartType === 'line' ? 1 : 0,
  182. },
  183. }));
  184. const hasOther = topEvents && topEvents + 1 === allSeries.length;
  185. const chartColors = allSeries.length
  186. ? [...theme.charts.getColorPalette(allSeries.length - 2 - (hasOther ? 1 : 0))]
  187. : undefined;
  188. if (chartColors?.length && hasOther) {
  189. chartColors.push(theme.chartOther);
  190. }
  191. const chartOptions = {
  192. colors: chartColors,
  193. height: 150,
  194. series: [...data],
  195. xAxis: {
  196. show: false,
  197. axisPointer: {
  198. show: false,
  199. },
  200. },
  201. yAxis: {
  202. show: true,
  203. axisLine: {
  204. show: false,
  205. },
  206. axisLabel: {
  207. color: theme.chartLabel,
  208. fontFamily: theme.text.family,
  209. fontSize: 12,
  210. formatter: (value: number) =>
  211. axisLabelFormatter(
  212. value,
  213. aggregateOutputType(Array.isArray(yAxis) ? yAxis[0] : yAxis),
  214. true
  215. ),
  216. inside: true,
  217. showMinLabel: false,
  218. showMaxLabel: false,
  219. },
  220. splitNumber: 3,
  221. splitLine: {
  222. show: false,
  223. },
  224. zlevel: theme.zIndex.header,
  225. },
  226. tooltip: {
  227. show: false,
  228. },
  229. toolBox: {
  230. show: false,
  231. },
  232. grid: {
  233. left: 0,
  234. top: 0,
  235. right: 0,
  236. bottom: 0,
  237. containLabel: false,
  238. },
  239. stacked:
  240. (typeof topEvents === 'number' && topEvents > 0) ||
  241. (Array.isArray(yAxis) && yAxis.length > 1),
  242. };
  243. const ChartComponent = this.getChartComponent(chartType);
  244. return <ChartComponent {...chartOptions} />;
  245. }}
  246. </EventsRequest>
  247. );
  248. }
  249. }
  250. const StyledGraphContainer = styled(props => (
  251. <LoadingContainer {...props} maskBackgroundColor="transparent" />
  252. ))`
  253. height: 150px;
  254. display: flex;
  255. justify-content: center;
  256. align-items: center;
  257. `;
  258. const StyledErrorMessage = styled('div')`
  259. color: ${p => p.theme.gray300};
  260. margin-left: 4px;
  261. `;
  262. export default withApi(withTheme(MiniGraph));