miniGraph.tsx 8.1 KB

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