projectStatsToSeries.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import cloneDeep from 'lodash/cloneDeep';
  2. import moment from 'moment';
  3. import {t} from 'sentry/locale';
  4. import {SeriesApi} from 'sentry/types';
  5. import {Series} from 'sentry/types/echarts';
  6. import commonTheme from 'sentry/utils/theme';
  7. import {Outcome} from 'sentry/views/organizationStats/types';
  8. import {
  9. COLOR_DROPPED,
  10. COLOR_TRANSACTIONS,
  11. } from 'sentry/views/organizationStats/usageChart';
  12. import {quantityField} from '.';
  13. export function projectStatsToSeries(projectStats: SeriesApi | undefined): Series[] {
  14. if (!projectStats) {
  15. return [];
  16. }
  17. const commonSeriesConfig = {
  18. barMinHeight: 1,
  19. type: 'bar',
  20. stack: 'usage',
  21. };
  22. const emptySeries = projectStats.intervals.map(interval => ({
  23. name: moment(interval).valueOf(),
  24. value: 0,
  25. }));
  26. const seriesData: Record<string, Series['data']> = {
  27. accepted: cloneDeep(emptySeries),
  28. droppedServer: cloneDeep(emptySeries),
  29. droppedClient: cloneDeep(emptySeries),
  30. };
  31. projectStats.intervals.forEach((_interval, index) => {
  32. projectStats.groups.forEach(group => {
  33. switch (group.by.outcome) {
  34. case Outcome.ACCEPTED:
  35. seriesData.accepted[index].value += group.series[quantityField][index];
  36. break;
  37. case Outcome.CLIENT_DISCARD:
  38. seriesData.droppedClient[index].value += group.series[quantityField][index];
  39. break;
  40. case Outcome.DROPPED:
  41. case Outcome.FILTERED:
  42. case Outcome.INVALID:
  43. case Outcome.RATE_LIMITED:
  44. seriesData.droppedServer[index].value += group.series[quantityField][index];
  45. break;
  46. default:
  47. // We do not care about other outcomes (right now there no other outcomes)
  48. }
  49. });
  50. });
  51. return [
  52. {
  53. seriesName: t('Accepted'),
  54. color: COLOR_TRANSACTIONS,
  55. ...commonSeriesConfig,
  56. data: seriesData.accepted,
  57. },
  58. {
  59. seriesName: t('Dropped (Server)'),
  60. color: COLOR_DROPPED,
  61. data: seriesData.droppedServer,
  62. ...commonSeriesConfig,
  63. },
  64. {
  65. seriesName: t('Dropped (Client)'),
  66. color: commonTheme.yellow300,
  67. data: seriesData.droppedClient,
  68. ...commonSeriesConfig,
  69. },
  70. ];
  71. }