getIntervalForMetricFunction.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/insights/database/settings';
  8. import type {Aggregate, SpanFunctions} from 'sentry/views/insights/types';
  9. import {
  10. COUNTER_AGGREGATES,
  11. DISTRIBUTION_AGGREGATES,
  12. SPAN_FUNCTIONS,
  13. } from 'sentry/views/insights/types';
  14. export function getIntervalForMetricFunction(
  15. metricFunction: Aggregate | SpanFunctions | string,
  16. datetimeObj: DateTimeObject
  17. ) {
  18. const {start, end, period, utc} = datetimeObj;
  19. const interval = Sentry.startSpan(
  20. {
  21. op: 'function',
  22. name: 'getIntervalForMetricFunction',
  23. attributes: {
  24. start: start ? start.toString() : undefined,
  25. end: end ? end.toString() : undefined,
  26. period: period || undefined,
  27. utc: utc || undefined,
  28. },
  29. },
  30. () => {
  31. const ladder = GRANULARITIES[metricFunction] ?? COUNTER_GRANULARITIES;
  32. return ladder.getInterval(getDiffInMinutes(datetimeObj));
  33. }
  34. );
  35. return interval;
  36. }
  37. type GranularityLookup = {
  38. [metricName: string]: GranularityLadder;
  39. };
  40. const GRANULARITIES: GranularityLookup = {};
  41. function registerGranularities(
  42. spanFunctionNames: readonly string[],
  43. granularities: GranularityLadder
  44. ) {
  45. spanFunctionNames.forEach(spanFunctionName => {
  46. GRANULARITIES[spanFunctionName] = granularities;
  47. });
  48. }
  49. registerGranularities(COUNTER_AGGREGATES, COUNTER_GRANULARITIES);
  50. registerGranularities(DISTRIBUTION_AGGREGATES, DISTRIBUTION_GRANULARITIES);
  51. registerGranularities(SPAN_FUNCTIONS, COUNTER_GRANULARITIES);