miniGraph.tsx 9.5 KB

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