determineSeriesConfidence.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import type {
  2. Confidence,
  3. EventsStats,
  4. MultiSeriesEventsStats,
  5. } from 'sentry/types/organization';
  6. import {defined} from 'sentry/utils';
  7. // Timeseries with more than this ratio of low confidence intervals will be considered low confidence
  8. const LOW_CONFIDENCE_THRESHOLD = 0.25;
  9. export function determineSeriesConfidence(
  10. data: EventsStats,
  11. threshold = LOW_CONFIDENCE_THRESHOLD
  12. ): Confidence {
  13. if (!defined(data.confidence) || data.confidence.length < 1) {
  14. return null;
  15. }
  16. const perDataUnitConfidence: Confidence[] = data.confidence.map(unit => {
  17. return unit[1].reduce(
  18. (acc, entry) => combineConfidence(acc, entry.count),
  19. null as Confidence
  20. );
  21. });
  22. const {lowConfidence, highConfidence, nullConfidence} = perDataUnitConfidence.reduce(
  23. (acc, confidence) => {
  24. if (confidence === 'low') {
  25. acc.lowConfidence += 1;
  26. } else if (confidence === 'high') {
  27. acc.highConfidence += 1;
  28. } else {
  29. acc.nullConfidence += 1;
  30. }
  31. return acc;
  32. },
  33. {lowConfidence: 0, highConfidence: 0, nullConfidence: 0}
  34. );
  35. if (lowConfidence <= 0 && highConfidence <= 0 && nullConfidence >= 0) {
  36. return null;
  37. }
  38. // Do not divide by (low + high + null) because nulls then can then heavily influence the final confidence
  39. if (lowConfidence / (lowConfidence + highConfidence) > threshold) {
  40. return 'low';
  41. }
  42. return 'high';
  43. }
  44. export function determineMultiSeriesConfidence(
  45. data: MultiSeriesEventsStats,
  46. threshold = LOW_CONFIDENCE_THRESHOLD
  47. ): Confidence {
  48. return Object.values(data).reduce(
  49. (acc, eventsStats) =>
  50. combineConfidence(acc, determineSeriesConfidence(eventsStats, threshold)),
  51. null as Confidence
  52. );
  53. }
  54. export function combineConfidence(a: Confidence, b: Confidence): Confidence {
  55. if (!defined(a)) {
  56. return b;
  57. }
  58. if (!defined(b)) {
  59. return a;
  60. }
  61. if (a === 'low' || b === 'low') {
  62. return 'low';
  63. }
  64. return 'high';
  65. }