utils.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {Organization} from 'sentry/types';
  2. import {AggregationKey} from 'sentry/utils/fields';
  3. import {isOnDemandQueryString} from 'sentry/utils/onDemandMetrics';
  4. import {hasOnDemandMetricWidgetFeature} from 'sentry/utils/onDemandMetrics/features';
  5. import {Widget, WidgetType} from 'sentry/views/dashboards/types';
  6. /**
  7. * We determine that a widget is an on-demand metric widget if the widget
  8. * 1. type is discover
  9. * 2. contains no grouping
  10. * 3. contains only one query condition
  11. * 4. contains only one aggregate and does not contain unsupported aggregates
  12. * 5. contains one of the keys that are not supported by the standard metrics.
  13. */
  14. export function isOnDemandMetricWidget(widget: Widget): boolean {
  15. if (widget.widgetType !== WidgetType.DISCOVER) {
  16. return false;
  17. }
  18. // currently we only support widgets without grouping
  19. const columns = widget.queries.flatMap(query => query.columns);
  20. if (columns.length > 0) {
  21. return false;
  22. }
  23. const conditions = widget.queries.flatMap(query => query.conditions);
  24. const hasNonStandardConditions = conditions.some(condition =>
  25. isOnDemandQueryString(condition)
  26. );
  27. // currently we only support one query per widget for on-demand metrics
  28. if (conditions.length > 1 || !hasNonStandardConditions) {
  29. return false;
  30. }
  31. const aggregates = widget.queries.flatMap(query => query.aggregates);
  32. const unsupportedAggregates = [
  33. AggregationKey.PERCENTILE,
  34. AggregationKey.APDEX,
  35. AggregationKey.FAILURE_RATE,
  36. ];
  37. // check if any of the aggregates contains unsupported aggregates as substr
  38. const hasUnsupportedAggregates = aggregates.some(aggregate =>
  39. unsupportedAggregates.some(agg => aggregate.includes(agg))
  40. );
  41. // currently we only support one aggregate per widget for on-demand metrics
  42. return aggregates.length > 1 || !hasUnsupportedAggregates;
  43. }
  44. export const shouldUseOnDemandMetrics = (organization: Organization, widget: Widget) => {
  45. return isOnDemandMetricWidget(widget) && hasOnDemandMetricWidgetFeature(organization);
  46. };