miniGraph.tsx 9.6 KB

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