getNewMetricsWidget.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import * as Sentry from '@sentry/react';
  2. import type {MetricMeta} from 'sentry/types/metrics';
  3. import {
  4. getDefaultAggregation,
  5. isAllowedAggregation,
  6. isVirtualMetric,
  7. } from 'sentry/utils/metrics';
  8. import {emptyMetricsQueryWidget} from 'sentry/utils/metrics/constants';
  9. /**
  10. * Creates a new widget, can be passed a metrics meta object to base the new widget on it
  11. * @param metricsMeta metrics meta on which the widget should be based
  12. * @param defaultCondition selected condition. only needed for virtual metrics
  13. * @returns
  14. */
  15. export function getNewMetricsWidget(metricsMeta?: MetricMeta, defaultCondition?: number) {
  16. const mri = metricsMeta?.mri || emptyMetricsQueryWidget.mri;
  17. let condition = emptyMetricsQueryWidget.condition;
  18. let aggregation = getDefaultAggregation(mri);
  19. if (metricsMeta && isVirtualMetric(metricsMeta)) {
  20. if (!defaultCondition) {
  21. Sentry.captureMessage(
  22. 'Metrics: Trying to create widget from virtual MRI without condition'
  23. );
  24. // Invalid data -> falling back to default
  25. return emptyMetricsQueryWidget;
  26. }
  27. condition = defaultCondition;
  28. const allowedAggregation = metricsMeta.operations.find(isAllowedAggregation);
  29. if (!allowedAggregation) {
  30. Sentry.captureMessage(
  31. 'Metrics: No allowed aggregations available for virtual metric found'
  32. );
  33. // Invalid data -> falling back to default
  34. return emptyMetricsQueryWidget;
  35. }
  36. aggregation = allowedAggregation;
  37. }
  38. return {
  39. ...emptyMetricsQueryWidget,
  40. mri,
  41. condition,
  42. aggregation,
  43. };
  44. }