discover.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. import type {SeriesOption} from 'echarts';
  2. import isArray from 'lodash/isArray';
  3. import max from 'lodash/max';
  4. import XAxis from 'sentry/components/charts/components/xAxis';
  5. import AreaSeries from 'sentry/components/charts/series/areaSeries';
  6. import BarSeries from 'sentry/components/charts/series/barSeries';
  7. import LineSeries from 'sentry/components/charts/series/lineSeries';
  8. import MapSeries from 'sentry/components/charts/series/mapSeries';
  9. import {lightenHexToRgb} from 'sentry/components/charts/utils';
  10. import * as countryCodesMap from 'sentry/data/countryCodesMap';
  11. import {t} from 'sentry/locale';
  12. import {EventsGeoData, EventsStats} from 'sentry/types';
  13. import {lightTheme as theme} from 'sentry/utils/theme';
  14. import {
  15. DEFAULT_FONT_FAMILY,
  16. slackChartDefaults,
  17. slackChartSize,
  18. slackGeoChartSize,
  19. } from './slack';
  20. import {ChartType, RenderDescriptor} from './types';
  21. const discoverxAxis = XAxis({
  22. theme,
  23. splitNumber: 3,
  24. isGroupedByDate: true,
  25. axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY},
  26. });
  27. export const discoverCharts: RenderDescriptor<ChartType>[] = [];
  28. discoverCharts.push({
  29. key: ChartType.SLACK_DISCOVER_TOTAL_PERIOD,
  30. getOption: (
  31. data:
  32. | {seriesName: string; stats: EventsStats}
  33. | {stats: Record<string, EventsStats>; seriesName?: string}
  34. ) => {
  35. if (isArray(data.stats.data)) {
  36. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  37. const areaSeries = AreaSeries({
  38. name: data.seriesName,
  39. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  40. timestamp * 1000,
  41. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  42. ]),
  43. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  44. areaStyle: {color: color?.[0], opacity: 1},
  45. });
  46. return {
  47. ...slackChartDefaults,
  48. useUTC: true,
  49. color,
  50. series: [areaSeries],
  51. };
  52. }
  53. const stats = Object.keys(data.stats).map(key =>
  54. Object.assign({}, {key}, data.stats[key])
  55. );
  56. const color = theme.charts.getColorPalette(stats.length - 2);
  57. const series = stats
  58. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  59. .map((s, i) =>
  60. AreaSeries({
  61. name: s.key,
  62. stack: 'area',
  63. data: s.data.map(([timestamp, countsForTimestamp]) => [
  64. timestamp * 1000,
  65. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  66. ]),
  67. lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
  68. areaStyle: {color: color?.[i], opacity: 1},
  69. })
  70. );
  71. return {
  72. ...slackChartDefaults,
  73. xAxis: discoverxAxis,
  74. useUTC: true,
  75. color,
  76. series,
  77. };
  78. },
  79. ...slackChartSize,
  80. });
  81. discoverCharts.push({
  82. key: ChartType.SLACK_DISCOVER_TOTAL_DAILY,
  83. getOption: (
  84. data:
  85. | {seriesName: string; stats: EventsStats}
  86. | {stats: Record<string, EventsStats>; seriesName?: string}
  87. ) => {
  88. if (isArray(data.stats.data)) {
  89. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  90. const barSeries = BarSeries({
  91. name: data.seriesName,
  92. data: data.stats.data.map(([timestamp, countsForTimestamp]) => ({
  93. value: [
  94. timestamp * 1000,
  95. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  96. ],
  97. })),
  98. itemStyle: {color: color?.[0], opacity: 1},
  99. });
  100. return {
  101. ...slackChartDefaults,
  102. xAxis: discoverxAxis,
  103. useUTC: true,
  104. color,
  105. series: [barSeries],
  106. };
  107. }
  108. const stats = Object.keys(data.stats).map(key =>
  109. Object.assign({}, {key}, data.stats[key])
  110. );
  111. const color = theme.charts.getColorPalette(stats.length - 2);
  112. const series = stats
  113. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  114. .map((s, i) =>
  115. BarSeries({
  116. name: s.key,
  117. stack: 'area',
  118. data: s.data.map(([timestamp, countsForTimestamp]) => ({
  119. value: [
  120. timestamp * 1000,
  121. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  122. ],
  123. })),
  124. itemStyle: {color: color?.[i], opacity: 1},
  125. })
  126. );
  127. return {
  128. ...slackChartDefaults,
  129. xAxis: discoverxAxis,
  130. useUTC: true,
  131. color,
  132. series,
  133. };
  134. },
  135. ...slackChartSize,
  136. });
  137. discoverCharts.push({
  138. key: ChartType.SLACK_DISCOVER_TOP5_PERIOD,
  139. getOption: (
  140. data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
  141. ) => {
  142. if (isArray(data.stats.data)) {
  143. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  144. const areaSeries = AreaSeries({
  145. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  146. timestamp * 1000,
  147. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  148. ]),
  149. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  150. areaStyle: {color: color?.[0], opacity: 1},
  151. });
  152. return {
  153. ...slackChartDefaults,
  154. useUTC: true,
  155. color,
  156. series: [areaSeries],
  157. };
  158. }
  159. const stats = Object.values(data.stats);
  160. const hasOther = Object.keys(data.stats).includes('Other');
  161. const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
  162. if (hasOther) {
  163. color.push(theme.chartOther);
  164. }
  165. const series = stats
  166. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  167. .map((topSeries, i) =>
  168. AreaSeries({
  169. stack: 'area',
  170. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  171. timestamp * 1000,
  172. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  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 (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.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
  213. if (hasOther) {
  214. color.push(theme.chartOther);
  215. }
  216. const series = stats
  217. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  218. .map((topSeries, i) =>
  219. LineSeries({
  220. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  221. timestamp * 1000,
  222. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  223. ]),
  224. lineStyle: {color: color?.[i], opacity: 1},
  225. itemStyle: {color: color?.[i]},
  226. })
  227. );
  228. return {
  229. ...slackChartDefaults,
  230. xAxis: discoverxAxis,
  231. useUTC: true,
  232. color,
  233. series,
  234. };
  235. },
  236. ...slackChartSize,
  237. });
  238. discoverCharts.push({
  239. key: ChartType.SLACK_DISCOVER_TOP5_DAILY,
  240. getOption: (
  241. data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
  242. ) => {
  243. if (isArray(data.stats.data)) {
  244. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  245. const areaSeries = AreaSeries({
  246. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  247. timestamp * 1000,
  248. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  249. ]),
  250. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  251. areaStyle: {color: color?.[0], opacity: 1},
  252. });
  253. return {
  254. ...slackChartDefaults,
  255. useUTC: true,
  256. color,
  257. series: [areaSeries],
  258. };
  259. }
  260. const stats = Object.values(data.stats);
  261. const hasOther = Object.keys(data.stats).includes('Other');
  262. const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
  263. if (hasOther) {
  264. color.push(theme.chartOther);
  265. }
  266. const series = stats
  267. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  268. .map((topSeries, i) =>
  269. BarSeries({
  270. stack: 'area',
  271. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  272. timestamp * 1000,
  273. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  274. ]),
  275. itemStyle: {color: color?.[i], opacity: 1},
  276. })
  277. );
  278. return {
  279. ...slackChartDefaults,
  280. xAxis: discoverxAxis,
  281. useUTC: true,
  282. color,
  283. series,
  284. };
  285. },
  286. ...slackChartSize,
  287. });
  288. discoverCharts.push({
  289. key: ChartType.SLACK_DISCOVER_PREVIOUS_PERIOD,
  290. getOption: (
  291. data:
  292. | {seriesName: string; stats: EventsStats}
  293. | {stats: Record<string, EventsStats>; seriesName?: string}
  294. ) => {
  295. if (isArray(data.stats.data)) {
  296. const dataMiddleIndex = Math.floor(data.stats.data.length / 2);
  297. const current = data.stats.data.slice(dataMiddleIndex);
  298. const previous = data.stats.data.slice(0, dataMiddleIndex);
  299. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  300. const areaSeries = AreaSeries({
  301. name: data.seriesName,
  302. data: current.map(([timestamp, countsForTimestamp]) => [
  303. timestamp * 1000,
  304. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  305. ]),
  306. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  307. areaStyle: {color: color?.[0], opacity: 1},
  308. });
  309. const previousPeriod = LineSeries({
  310. name: t('previous %s', data.seriesName),
  311. data: previous.map(([_, countsForTimestamp], i) => [
  312. current[i][0] * 1000,
  313. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  314. ]),
  315. lineStyle: {color: theme.gray200, type: 'dotted'},
  316. itemStyle: {color: theme.gray200},
  317. });
  318. return {
  319. ...slackChartDefaults,
  320. useUTC: true,
  321. color,
  322. series: [areaSeries, previousPeriod],
  323. };
  324. }
  325. const stats = Object.keys(data.stats).map(key =>
  326. Object.assign({}, {key}, data.stats[key])
  327. );
  328. const color = theme.charts.getColorPalette(stats.length - 2);
  329. const previousPeriodColor = lightenHexToRgb(color);
  330. const areaSeries: SeriesOption[] = [];
  331. const lineSeries: SeriesOption[] = [];
  332. stats
  333. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  334. .forEach((s, i) => {
  335. const dataMiddleIndex = Math.floor(s.data.length / 2);
  336. const current = s.data.slice(dataMiddleIndex);
  337. const previous = s.data.slice(0, dataMiddleIndex);
  338. areaSeries.push(
  339. AreaSeries({
  340. name: s.key,
  341. stack: 'area',
  342. data: s.data
  343. .slice(dataMiddleIndex)
  344. .map(([timestamp, countsForTimestamp]) => [
  345. timestamp * 1000,
  346. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  347. ]),
  348. lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
  349. areaStyle: {color: color?.[i], opacity: 1},
  350. })
  351. );
  352. lineSeries.push(
  353. LineSeries({
  354. name: t('previous %s', s.key),
  355. stack: 'previous',
  356. data: previous.map(([_, countsForTimestamp], index) => [
  357. current[index][0] * 1000,
  358. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  359. ]),
  360. lineStyle: {color: previousPeriodColor?.[i], type: 'dotted'},
  361. itemStyle: {color: previousPeriodColor?.[i]},
  362. })
  363. );
  364. });
  365. return {
  366. ...slackChartDefaults,
  367. xAxis: discoverxAxis,
  368. useUTC: true,
  369. color,
  370. series: [...areaSeries, ...lineSeries],
  371. };
  372. },
  373. ...slackChartSize,
  374. });
  375. discoverCharts.push({
  376. key: ChartType.SLACK_DISCOVER_WORLDMAP,
  377. getOption: (data: {seriesName: string; stats: {data: EventsGeoData}}) => {
  378. const mapSeries = MapSeries({
  379. map: 'sentryWorld',
  380. name: data.seriesName,
  381. data: data.stats.data.map(country => ({
  382. name: country['geo.country_code'],
  383. value: country.count,
  384. })),
  385. nameMap: countryCodesMap.default,
  386. aspectScale: 0.85,
  387. zoom: 1.1,
  388. center: [10.97, 9.71],
  389. itemStyle: {
  390. areaColor: theme.gray200,
  391. borderColor: theme.backgroundSecondary,
  392. },
  393. });
  394. // For absolute values, we want min/max to based on min/max of series
  395. // Otherwise it should be 0-100
  396. const maxValue = max(data.stats.data.map(value => value.count)) || 1;
  397. return {
  398. backgroundColor: theme.background,
  399. visualMap: [
  400. {
  401. left: 'right',
  402. min: 0,
  403. max: maxValue,
  404. inRange: {
  405. color: [theme.purple200, theme.purple300],
  406. },
  407. text: ['High', 'Low'],
  408. textStyle: {
  409. color: theme.textColor,
  410. },
  411. // Whether show handles, which can be dragged to adjust "selected range".
  412. // False because the handles are pretty ugly
  413. calculable: false,
  414. },
  415. ],
  416. series: [mapSeries],
  417. };
  418. },
  419. ...slackGeoChartSize,
  420. });