mapSeriesToChart.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import * as Sentry from '@sentry/react';
  2. import startCase from 'lodash/startCase';
  3. import moment from 'moment';
  4. import type {TooltipSubLabel} from 'sentry/components/charts/components/tooltip';
  5. import type {DataCategoryInfo, IntervalPeriod} from 'sentry/types';
  6. import {Outcome} from 'sentry/types';
  7. import {getDateFromMoment} from './usageChart/utils';
  8. import {getReasonGroupName} from './getReasonGroupName';
  9. import type {UsageSeries, UsageStat} from './types';
  10. import type {ChartStats} from './usageChart';
  11. import {SeriesTypes} from './usageChart';
  12. import {formatUsageWithUnits, getFormatUsageOptions} from './utils';
  13. export function mapSeriesToChart({
  14. orgStats,
  15. dataCategory,
  16. chartDateUtc,
  17. endpointQuery,
  18. chartDateInterval,
  19. }: {
  20. chartDateInterval: IntervalPeriod;
  21. chartDateUtc: boolean;
  22. dataCategory: DataCategoryInfo['plural'];
  23. endpointQuery: Record<string, unknown>;
  24. orgStats?: UsageSeries;
  25. }): {
  26. cardStats: {
  27. accepted?: string;
  28. filtered?: string;
  29. invalid?: string;
  30. rateLimited?: string;
  31. total?: string;
  32. };
  33. chartStats: ChartStats;
  34. chartSubLabels: TooltipSubLabel[];
  35. dataError?: Error;
  36. } {
  37. const cardStats = {
  38. total: undefined,
  39. accepted: undefined,
  40. filtered: undefined,
  41. invalid: undefined,
  42. rateLimited: undefined,
  43. };
  44. const chartStats: ChartStats = {
  45. accepted: [],
  46. filtered: [],
  47. rateLimited: [],
  48. invalid: [],
  49. clientDiscard: [],
  50. projected: [],
  51. };
  52. const chartSubLabels: TooltipSubLabel[] = [];
  53. if (!orgStats) {
  54. return {cardStats, chartStats, chartSubLabels};
  55. }
  56. try {
  57. const usageStats: UsageStat[] = orgStats.intervals.map(interval => {
  58. const dateTime = moment(interval);
  59. return {
  60. date: getDateFromMoment(dateTime, chartDateInterval, chartDateUtc),
  61. total: 0,
  62. accepted: 0,
  63. filtered: 0,
  64. rateLimited: 0,
  65. invalid: 0,
  66. clientDiscard: 0,
  67. };
  68. });
  69. // Tally totals for card data
  70. const count = {
  71. total: 0,
  72. [Outcome.ACCEPTED]: 0,
  73. [Outcome.FILTERED]: 0,
  74. [Outcome.INVALID]: 0,
  75. [Outcome.RATE_LIMITED]: 0, // Combined with dropped later
  76. [Outcome.CLIENT_DISCARD]: 0,
  77. [Outcome.CARDINALITY_LIMITED]: 0, // Combined with dropped later
  78. [Outcome.ABUSE]: 0, // Combined with dropped later
  79. };
  80. orgStats.groups.forEach(group => {
  81. const {outcome} = group.by;
  82. if (outcome !== Outcome.CLIENT_DISCARD) {
  83. count.total += group.totals['sum(quantity)'];
  84. }
  85. count[outcome] += group.totals['sum(quantity)'];
  86. group.series['sum(quantity)'].forEach((stat, i) => {
  87. const dataObject = {name: orgStats.intervals[i], value: stat};
  88. const strigfiedReason = String(group.by.reason ?? '');
  89. const reason = getReasonGroupName(outcome, strigfiedReason);
  90. const label = startCase(reason.replace(/-|_/g, ' '));
  91. // Combine rate limited counts
  92. count[Outcome.RATE_LIMITED] +=
  93. count[Outcome.ABUSE] + count[Outcome.CARDINALITY_LIMITED];
  94. // Function to handle chart sub-label updates
  95. const updateChartSubLabels = (parentLabel: SeriesTypes) => {
  96. const existingSubLabel = chartSubLabels.find(
  97. subLabel => subLabel.label === label && subLabel.parentLabel === parentLabel
  98. );
  99. if (existingSubLabel) {
  100. // Check if the existing sub-label's data length matches the intervals length
  101. if (existingSubLabel.data.length === group.series['sum(quantity)'].length) {
  102. // Update the value of the current interval
  103. existingSubLabel.data[i].value += stat;
  104. } else {
  105. // Add a new data object if the length does not match
  106. existingSubLabel.data.push(dataObject);
  107. }
  108. } else {
  109. chartSubLabels.push({
  110. parentLabel,
  111. label,
  112. data: [dataObject],
  113. });
  114. }
  115. };
  116. switch (outcome) {
  117. case Outcome.FILTERED:
  118. usageStats[i].filtered += stat;
  119. updateChartSubLabels(SeriesTypes.FILTERED);
  120. break;
  121. case Outcome.ACCEPTED:
  122. usageStats[i].accepted += stat;
  123. break;
  124. case Outcome.CARDINALITY_LIMITED:
  125. case Outcome.RATE_LIMITED:
  126. case Outcome.ABUSE:
  127. usageStats[i].rateLimited += stat;
  128. updateChartSubLabels(SeriesTypes.RATE_LIMITED);
  129. break;
  130. case Outcome.CLIENT_DISCARD:
  131. usageStats[i].clientDiscard += stat;
  132. updateChartSubLabels(SeriesTypes.CLIENT_DISCARD);
  133. break;
  134. case Outcome.INVALID:
  135. usageStats[i].invalid += stat;
  136. updateChartSubLabels(SeriesTypes.INVALID);
  137. break;
  138. default:
  139. break;
  140. }
  141. });
  142. });
  143. usageStats.forEach(stat => {
  144. stat.total = [
  145. stat.accepted,
  146. stat.filtered,
  147. stat.rateLimited,
  148. stat.invalid,
  149. stat.clientDiscard,
  150. ].reduce((acc, val) => acc + val, 0);
  151. // Chart Data
  152. const chartData = [
  153. {key: 'accepted', value: stat.accepted},
  154. {key: 'filtered', value: stat.filtered},
  155. {key: 'rateLimited', value: stat.rateLimited},
  156. {key: 'invalid', value: stat.invalid},
  157. {key: 'clientDiscard', value: stat.clientDiscard},
  158. ];
  159. chartData.forEach(data => {
  160. (chartStats[data.key] as any[]).push({value: [stat.date, data.value]});
  161. });
  162. });
  163. return {
  164. cardStats: {
  165. total: formatUsageWithUnits(
  166. count.total,
  167. dataCategory,
  168. getFormatUsageOptions(dataCategory)
  169. ),
  170. accepted: formatUsageWithUnits(
  171. count[Outcome.ACCEPTED],
  172. dataCategory,
  173. getFormatUsageOptions(dataCategory)
  174. ),
  175. filtered: formatUsageWithUnits(
  176. count[Outcome.FILTERED],
  177. dataCategory,
  178. getFormatUsageOptions(dataCategory)
  179. ),
  180. invalid: formatUsageWithUnits(
  181. count[Outcome.INVALID],
  182. dataCategory,
  183. getFormatUsageOptions(dataCategory)
  184. ),
  185. rateLimited: formatUsageWithUnits(
  186. count[Outcome.RATE_LIMITED],
  187. dataCategory,
  188. getFormatUsageOptions(dataCategory)
  189. ),
  190. },
  191. chartStats,
  192. chartSubLabels,
  193. };
  194. } catch (err) {
  195. Sentry.withScope(scope => {
  196. scope.setContext('query', endpointQuery);
  197. scope.setContext('body', {...orgStats});
  198. Sentry.captureException(err);
  199. });
  200. return {
  201. cardStats,
  202. chartStats,
  203. chartSubLabels,
  204. dataError: new Error('Failed to parse stats data'),
  205. };
  206. }
  207. }