utils.tsx 1.9 KB

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