determineSeriesSampleCount.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {defined} from 'sentry/utils';
  2. import type {TimeSeries} from 'sentry/views/dashboards/widgets/common/types';
  3. export function determineSeriesSampleCountAndIsSampled(
  4. data: TimeSeries[],
  5. topNMode: boolean
  6. ): {isSampled: boolean | null; sampleCount: number} {
  7. if (data.length <= 0) {
  8. return {sampleCount: 0, isSampled: null};
  9. }
  10. if (topNMode) {
  11. // We dont want to count the other series in top N mode
  12. data = data.filter(s => s.field !== 'Other');
  13. }
  14. const merge: (a: number, b: number) => number = topNMode
  15. ? // In top N mode, we know all the timeseries are disjoint, so taking the sum
  16. // gives us an accurate sample count based on the timeseries
  17. (a, b) => a + b
  18. : // Without top N mode, we hae 2 choices, to take the sum or the max. The sum
  19. // will give an overestimate while the max will give an underestimate because
  20. // the series have the potential (not definite) to share samples.
  21. // We choose to use max here because showing the lower number makes it easier
  22. // to justify the potential low accuracy warning.
  23. Math.max;
  24. let hasSampledInterval = false;
  25. let hasUnsampledInterval = false;
  26. const series: number[] = data[0]?.sampleCount?.map(item => item.value) ?? [];
  27. for (let i = 0; i < data.length; i++) {
  28. if (defined(data[i]?.sampleCount)) {
  29. for (let j = 0; j < data[i]!.sampleCount!.length; j++) {
  30. if (i > 0) {
  31. series[j] = merge(series[j]!, data[i]!.sampleCount![j]!.value);
  32. }
  33. const sampleRate = data[i]?.samplingRate?.[j]?.value;
  34. if (sampleRate === 1) {
  35. hasUnsampledInterval = true;
  36. } else if (defined(sampleRate) && sampleRate < 1) {
  37. hasSampledInterval = true;
  38. }
  39. }
  40. }
  41. }
  42. const isSampled = hasSampledInterval ? true : hasUnsampledInterval ? false : null;
  43. return {sampleCount: series.reduce((sum, count) => sum + count, 0), isSampled};
  44. }