discover.tsx 13 KB

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