miniGraph.tsx 7.3 KB

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