projectStatsToPredictedSeries.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import moment from 'moment';
  2. import {t} from 'sentry/locale';
  3. import {Outcome, SeriesApi} from 'sentry/types';
  4. import {Series} from 'sentry/types/echarts';
  5. import {defined} from 'sentry/utils';
  6. import commonTheme from 'sentry/utils/theme';
  7. import {quantityField} from '.';
  8. export function projectStatsToPredictedSeries(
  9. projectStats?: SeriesApi,
  10. client?: number,
  11. server?: number,
  12. specifiedClientRate?: number
  13. ): Series[] {
  14. if (!projectStats || !defined(client) || !defined(server)) {
  15. return [];
  16. }
  17. const clientRate = Math.max(Math.min(client, 1), 0);
  18. let serverRate = Math.max(Math.min(server, 1), 0);
  19. const commonSeriesConfig = {
  20. barMinHeight: 0,
  21. type: 'bar',
  22. stack: 'predictedUsage',
  23. };
  24. const seriesData: Record<
  25. 'indexedAndProcessed' | 'processed' | 'discarded',
  26. Series['data']
  27. > = {
  28. indexedAndProcessed: [],
  29. processed: [],
  30. discarded: [],
  31. };
  32. (
  33. projectStats.intervals.map((interval, index) => {
  34. const result = {
  35. indexedAndProcessed: 0,
  36. processed: 0,
  37. discarded: 0,
  38. };
  39. projectStats.groups.forEach(group => {
  40. switch (group.by.outcome) {
  41. case Outcome.ACCEPTED:
  42. result.indexedAndProcessed += group.series[quantityField][index];
  43. break;
  44. case Outcome.CLIENT_DISCARD:
  45. result.discarded += group.series[quantityField][index];
  46. break;
  47. case Outcome.FILTERED:
  48. if (String(group.by.reason).startsWith('Sampled')) {
  49. result.processed += group.series[quantityField][index];
  50. }
  51. break;
  52. default:
  53. // We do not take invalid, rate_limited and other filtered into account
  54. }
  55. });
  56. return {
  57. interval: moment(interval).valueOf(),
  58. ...result,
  59. };
  60. }) as Array<
  61. Record<'indexedAndProcessed' | 'processed' | 'discarded' | 'interval', number>
  62. >
  63. ).forEach((bucket, index) => {
  64. const {indexedAndProcessed, processed, discarded, interval} = bucket;
  65. if (clientRate < serverRate!) {
  66. serverRate = clientRate;
  67. }
  68. let total = indexedAndProcessed + processed + discarded;
  69. if (defined(specifiedClientRate)) {
  70. // We assume that the clientDiscard is 0 and
  71. // calculate the discard client (SDK) bucket according to the specified client rate
  72. const newClientDiscard = total / specifiedClientRate - total;
  73. total += newClientDiscard;
  74. }
  75. const newSentClient = total * clientRate;
  76. const newDiscarded = total - newSentClient;
  77. const newIndexedAndProcessed =
  78. clientRate === 0 ? 0 : newSentClient * (serverRate! / clientRate);
  79. const newProcessed = newSentClient - newIndexedAndProcessed;
  80. seriesData.indexedAndProcessed[index] = {
  81. name: interval,
  82. value: Math.round(newIndexedAndProcessed),
  83. };
  84. seriesData.processed[index] = {
  85. name: interval,
  86. value: Math.round(newProcessed),
  87. };
  88. seriesData.discarded[index] = {
  89. name: interval,
  90. value: Math.round(newDiscarded),
  91. };
  92. });
  93. return [
  94. {
  95. seriesName: t('Indexed and Processed'),
  96. color: commonTheme.green300,
  97. ...commonSeriesConfig,
  98. data: seriesData.indexedAndProcessed,
  99. },
  100. {
  101. seriesName: t('Processed'),
  102. color: commonTheme.yellow300,
  103. data: seriesData.processed,
  104. ...commonSeriesConfig,
  105. },
  106. {
  107. seriesName: t('Discarded'),
  108. color: commonTheme.red300,
  109. data: seriesData.discarded,
  110. ...commonSeriesConfig,
  111. },
  112. ];
  113. }