getIntervalForMetricFunction.tsx 1.4 KB

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