discover.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import type {SeriesOption} from 'echarts';
  2. import XAxis from 'sentry/components/charts/components/xAxis';
  3. import AreaSeries from 'sentry/components/charts/series/areaSeries';
  4. import BarSeries from 'sentry/components/charts/series/barSeries';
  5. import LineSeries from 'sentry/components/charts/series/lineSeries';
  6. import {lightenHexToRgb} from 'sentry/components/charts/utils';
  7. import {t} from 'sentry/locale';
  8. import {EventsStats} from 'sentry/types';
  9. import {lightTheme as theme} from 'sentry/utils/theme';
  10. import {DEFAULT_FONT_FAMILY, slackChartDefaults, slackChartSize} from './slack';
  11. import {ChartType, RenderDescriptor} from './types';
  12. const discoverxAxis = XAxis({
  13. theme,
  14. splitNumber: 3,
  15. isGroupedByDate: true,
  16. axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY},
  17. });
  18. export const discoverCharts: RenderDescriptor<ChartType>[] = [];
  19. discoverCharts.push({
  20. key: ChartType.SLACK_DISCOVER_TOTAL_PERIOD,
  21. getOption: (
  22. data:
  23. | {seriesName: string; stats: EventsStats}
  24. | {stats: Record<string, EventsStats>; seriesName?: string}
  25. ) => {
  26. if (Array.isArray(data.stats.data)) {
  27. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  28. const areaSeries = AreaSeries({
  29. name: data.seriesName,
  30. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  31. timestamp * 1000,
  32. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  33. ]),
  34. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  35. areaStyle: {color: color?.[0], opacity: 1},
  36. });
  37. return {
  38. ...slackChartDefaults,
  39. useUTC: true,
  40. color,
  41. series: [areaSeries],
  42. };
  43. }
  44. const stats = Object.keys(data.stats).map(key =>
  45. Object.assign({}, {key}, data.stats[key])
  46. );
  47. const color = theme.charts.getColorPalette(stats.length - 2);
  48. const series = stats
  49. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  50. .map((s, i) =>
  51. AreaSeries({
  52. name: s.key,
  53. stack: 'area',
  54. data: s.data.map(([timestamp, countsForTimestamp]) => [
  55. timestamp * 1000,
  56. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  57. ]),
  58. lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
  59. areaStyle: {color: color?.[i], opacity: 1},
  60. })
  61. );
  62. return {
  63. ...slackChartDefaults,
  64. xAxis: discoverxAxis,
  65. useUTC: true,
  66. color,
  67. series,
  68. };
  69. },
  70. ...slackChartSize,
  71. });
  72. discoverCharts.push({
  73. key: ChartType.SLACK_DISCOVER_TOTAL_DAILY,
  74. getOption: (
  75. data:
  76. | {seriesName: string; stats: EventsStats}
  77. | {stats: Record<string, EventsStats>; seriesName?: string}
  78. ) => {
  79. if (Array.isArray(data.stats.data)) {
  80. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  81. const barSeries = BarSeries({
  82. name: data.seriesName,
  83. data: data.stats.data.map(([timestamp, countsForTimestamp]) => ({
  84. value: [
  85. timestamp * 1000,
  86. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  87. ],
  88. })),
  89. itemStyle: {color: color?.[0], opacity: 1},
  90. });
  91. return {
  92. ...slackChartDefaults,
  93. xAxis: discoverxAxis,
  94. useUTC: true,
  95. color,
  96. series: [barSeries],
  97. };
  98. }
  99. const stats = Object.keys(data.stats).map(key =>
  100. Object.assign({}, {key}, data.stats[key])
  101. );
  102. const color = theme.charts.getColorPalette(stats.length - 2);
  103. const series = stats
  104. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  105. .map((s, i) =>
  106. BarSeries({
  107. name: s.key,
  108. stack: 'area',
  109. data: s.data.map(([timestamp, countsForTimestamp]) => ({
  110. value: [
  111. timestamp * 1000,
  112. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  113. ],
  114. })),
  115. itemStyle: {color: color?.[i], opacity: 1},
  116. })
  117. );
  118. return {
  119. ...slackChartDefaults,
  120. xAxis: discoverxAxis,
  121. useUTC: true,
  122. color,
  123. series,
  124. };
  125. },
  126. ...slackChartSize,
  127. });
  128. discoverCharts.push({
  129. key: ChartType.SLACK_DISCOVER_TOP5_PERIOD,
  130. getOption: (
  131. data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
  132. ) => {
  133. if (Array.isArray(data.stats.data)) {
  134. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  135. const areaSeries = AreaSeries({
  136. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  137. timestamp * 1000,
  138. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  139. ]),
  140. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  141. areaStyle: {color: color?.[0], opacity: 1},
  142. });
  143. return {
  144. ...slackChartDefaults,
  145. useUTC: true,
  146. color,
  147. series: [areaSeries],
  148. };
  149. }
  150. const stats = Object.values(data.stats);
  151. const hasOther = Object.keys(data.stats).includes('Other');
  152. const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
  153. if (hasOther) {
  154. color.push(theme.chartOther);
  155. }
  156. const series = stats
  157. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  158. .map((topSeries, i) =>
  159. AreaSeries({
  160. stack: 'area',
  161. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  162. timestamp * 1000,
  163. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  164. ]),
  165. lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
  166. areaStyle: {color: color?.[i], opacity: 1},
  167. })
  168. );
  169. return {
  170. ...slackChartDefaults,
  171. xAxis: discoverxAxis,
  172. useUTC: true,
  173. color,
  174. series,
  175. };
  176. },
  177. ...slackChartSize,
  178. });
  179. discoverCharts.push({
  180. key: ChartType.SLACK_DISCOVER_TOP5_PERIOD_LINE,
  181. getOption: (
  182. data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
  183. ) => {
  184. if (Array.isArray(data.stats.data)) {
  185. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  186. const lineSeries = LineSeries({
  187. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  188. timestamp * 1000,
  189. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  190. ]),
  191. lineStyle: {color: color?.[0], opacity: 1},
  192. itemStyle: {color: color?.[0]},
  193. });
  194. return {
  195. ...slackChartDefaults,
  196. useUTC: true,
  197. color,
  198. series: [lineSeries],
  199. };
  200. }
  201. const stats = Object.values(data.stats);
  202. const hasOther = Object.keys(data.stats).includes('Other');
  203. const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
  204. if (hasOther) {
  205. color.push(theme.chartOther);
  206. }
  207. const series = stats
  208. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  209. .map((topSeries, i) =>
  210. LineSeries({
  211. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  212. timestamp * 1000,
  213. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  214. ]),
  215. lineStyle: {color: color?.[i], opacity: 1},
  216. itemStyle: {color: color?.[i]},
  217. })
  218. );
  219. return {
  220. ...slackChartDefaults,
  221. xAxis: discoverxAxis,
  222. useUTC: true,
  223. color,
  224. series,
  225. };
  226. },
  227. ...slackChartSize,
  228. });
  229. discoverCharts.push({
  230. key: ChartType.SLACK_DISCOVER_TOP5_DAILY,
  231. getOption: (
  232. data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
  233. ) => {
  234. if (Array.isArray(data.stats.data)) {
  235. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  236. const areaSeries = AreaSeries({
  237. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  238. timestamp * 1000,
  239. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  240. ]),
  241. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  242. areaStyle: {color: color?.[0], opacity: 1},
  243. });
  244. return {
  245. ...slackChartDefaults,
  246. useUTC: true,
  247. color,
  248. series: [areaSeries],
  249. };
  250. }
  251. const stats = Object.values(data.stats);
  252. const hasOther = Object.keys(data.stats).includes('Other');
  253. const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
  254. if (hasOther) {
  255. color.push(theme.chartOther);
  256. }
  257. const series = stats
  258. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  259. .map((topSeries, i) =>
  260. BarSeries({
  261. stack: 'area',
  262. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  263. timestamp * 1000,
  264. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  265. ]),
  266. itemStyle: {color: color?.[i], opacity: 1},
  267. })
  268. );
  269. return {
  270. ...slackChartDefaults,
  271. xAxis: discoverxAxis,
  272. useUTC: true,
  273. color,
  274. series,
  275. };
  276. },
  277. ...slackChartSize,
  278. });
  279. discoverCharts.push({
  280. key: ChartType.SLACK_DISCOVER_PREVIOUS_PERIOD,
  281. getOption: (
  282. data:
  283. | {seriesName: string; stats: EventsStats}
  284. | {stats: Record<string, EventsStats>; seriesName?: string}
  285. ) => {
  286. if (Array.isArray(data.stats.data)) {
  287. const dataMiddleIndex = Math.floor(data.stats.data.length / 2);
  288. const current = data.stats.data.slice(dataMiddleIndex);
  289. const previous = data.stats.data.slice(0, dataMiddleIndex);
  290. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  291. const areaSeries = AreaSeries({
  292. name: data.seriesName,
  293. data: current.map(([timestamp, countsForTimestamp]) => [
  294. timestamp * 1000,
  295. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  296. ]),
  297. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  298. areaStyle: {color: color?.[0], opacity: 1},
  299. });
  300. const previousPeriod = LineSeries({
  301. name: t('previous %s', data.seriesName),
  302. data: previous.map(([_, countsForTimestamp], i) => [
  303. current[i][0] * 1000,
  304. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  305. ]),
  306. lineStyle: {color: theme.gray200, type: 'dotted'},
  307. itemStyle: {color: theme.gray200},
  308. });
  309. return {
  310. ...slackChartDefaults,
  311. useUTC: true,
  312. color,
  313. series: [areaSeries, previousPeriod],
  314. };
  315. }
  316. const stats = Object.keys(data.stats).map(key =>
  317. Object.assign({}, {key}, data.stats[key])
  318. );
  319. const color = theme.charts.getColorPalette(stats.length - 2);
  320. const previousPeriodColor = lightenHexToRgb(color);
  321. const areaSeries: SeriesOption[] = [];
  322. const lineSeries: SeriesOption[] = [];
  323. stats
  324. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  325. .forEach((s, i) => {
  326. const dataMiddleIndex = Math.floor(s.data.length / 2);
  327. const current = s.data.slice(dataMiddleIndex);
  328. const previous = s.data.slice(0, dataMiddleIndex);
  329. areaSeries.push(
  330. AreaSeries({
  331. name: s.key,
  332. stack: 'area',
  333. data: s.data
  334. .slice(dataMiddleIndex)
  335. .map(([timestamp, countsForTimestamp]) => [
  336. timestamp * 1000,
  337. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  338. ]),
  339. lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
  340. areaStyle: {color: color?.[i], opacity: 1},
  341. })
  342. );
  343. lineSeries.push(
  344. LineSeries({
  345. name: t('previous %s', s.key),
  346. stack: 'previous',
  347. data: previous.map(([_, countsForTimestamp], index) => [
  348. current[index][0] * 1000,
  349. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  350. ]),
  351. lineStyle: {color: previousPeriodColor?.[i], type: 'dotted'},
  352. itemStyle: {color: previousPeriodColor?.[i]},
  353. })
  354. );
  355. });
  356. return {
  357. ...slackChartDefaults,
  358. xAxis: discoverxAxis,
  359. useUTC: true,
  360. color,
  361. series: [...areaSeries, ...lineSeries],
  362. };
  363. },
  364. ...slackChartSize,
  365. });