utils.tsx 2.0 KB

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