getIntervalForMetricFunction.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import * as Sentry from '@sentry/react';
  2. import type {DateTimeObject, GranularityLadder} from 'sentry/components/charts/utils';
  3. import {getDiffInMinutes} from 'sentry/components/charts/utils';
  4. import {
  5. COUNTER_GRANULARITIES,
  6. DISTRIBUTION_GRANULARITIES,
  7. } from 'sentry/views/performance/database/settings';
  8. import type {Aggregate, SpanFunctions} from 'sentry/views/starfish/types';
  9. import {
  10. COUNTER_AGGREGATES,
  11. DISTRIBUTION_AGGREGATES,
  12. SPAN_FUNCTIONS,
  13. } from 'sentry/views/starfish/types';
  14. export function getIntervalForMetricFunction(
  15. metricFunction: Aggregate | SpanFunctions | string,
  16. datetimeObj: DateTimeObject
  17. ) {
  18. const interval = Sentry.startSpan(
  19. {op: 'function', name: 'getIntervalForMetricFunction', data: {...datetimeObj}},
  20. () => {
  21. const ladder = GRANULARITIES[metricFunction] ?? COUNTER_GRANULARITIES;
  22. return ladder.getInterval(getDiffInMinutes(datetimeObj));
  23. }
  24. );
  25. return interval;
  26. }
  27. type GranularityLookup = {
  28. [metricName: string]: GranularityLadder;
  29. };
  30. const GRANULARITIES: GranularityLookup = {};
  31. function registerGranularities(
  32. spanFunctionNames: readonly string[],
  33. granularities: GranularityLadder
  34. ) {
  35. spanFunctionNames.forEach(spanFunctionName => {
  36. GRANULARITIES[spanFunctionName] = granularities;
  37. });
  38. }
  39. registerGranularities(COUNTER_AGGREGATES, COUNTER_GRANULARITIES);
  40. registerGranularities(DISTRIBUTION_AGGREGATES, DISTRIBUTION_GRANULARITIES);
  41. registerGranularities(SPAN_FUNCTIONS, COUNTER_GRANULARITIES);