123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import {MetricDisplayType, MetricExpressionType} from 'sentry/utils/metrics/types';
- import type {DashboardMetricsExpression} from 'sentry/views/dashboards/metrics/types';
- import type {Widget} from 'sentry/views/dashboards/types';
- import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
- import {expressionsToWidget, getMetricExpressions, toMetricDisplayType} from './utils';
- const mockGetVirtualMRIQuery = jest.fn(() => {
- return {
- mri: 'v:custom/span.duration@milisecond' as const,
- conditionId: 1,
- aggregation: 'sum' as const,
- };
- });
- describe('getMetricExpressions function', () => {
- it('should return a query', () => {
- const widget = {
- queries: [
- {
- aggregates: ['avg(d:transactions/duration@milisecond)'],
- conditions: 'foo:bar',
- columns: ['release'],
- name: 'query_1',
- orderby: 'asc',
- },
- ],
- } as Widget;
- const metricQueries = getMetricExpressions(widget, undefined, mockGetVirtualMRIQuery);
- expect(metricQueries).toEqual([
- {
- groupBy: ['release'],
- id: 0,
- mri: 'd:transactions/duration@milisecond',
- aggregation: 'avg',
- query: 'foo:bar',
- type: MetricExpressionType.QUERY,
- orderBy: 'asc',
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- ]);
- expect(mockGetVirtualMRIQuery).not.toHaveBeenCalled();
- });
- it('should return an equation', () => {
- const widget = {
- queries: [
- {
- aggregates: ['equation|$a + $b'],
- conditions: 'foo:bar',
- columns: ['release'],
- name: 'query_1',
- },
- ],
- } as Widget;
- const metricQueries = getMetricExpressions(widget, undefined, mockGetVirtualMRIQuery);
- expect(metricQueries).toEqual([
- {
- id: 0,
- formula: '$a + $b',
- type: MetricExpressionType.EQUATION,
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- ]);
- });
- it('should return metricQueries with correct parameters with dashboardFilters', () => {
- const widget = {
- queries: [
- {
- aggregates: ['avg(d:transactions/duration@milisecond)'],
- conditions: 'foo:bar',
- columns: ['release'],
- name: '0',
- orderby: 'desc',
- },
- {
- aggregates: ['avg(d:transactions/duration@milisecond)'],
- conditions: 'foo:baz',
- columns: [],
- name: '1',
- orderby: '',
- },
- ],
- } as Widget;
- const metricQueries = getMetricExpressions(
- widget,
- {release: ['1.0']},
- mockGetVirtualMRIQuery
- );
- expect(metricQueries).toEqual([
- {
- groupBy: ['release'],
- id: 0,
- mri: 'd:transactions/duration@milisecond',
- aggregation: 'avg',
- query: 'foo:bar release:1.0',
- type: MetricExpressionType.QUERY,
- orderBy: 'desc',
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- {
- groupBy: [],
- id: 1,
- mri: 'd:transactions/duration@milisecond',
- aggregation: 'avg',
- query: 'foo:baz release:1.0',
- type: MetricExpressionType.QUERY,
- orderBy: undefined,
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- ]);
- });
- it('should return metricQueries with correct parameters with multiple dashboardFilters', () => {
- const widget = {
- queries: [
- {
- aggregates: ['avg(d:transactions/duration@milisecond)'],
- conditions: '',
- columns: ['release'],
- name: '1',
- },
- ],
- } as Widget;
- const metricQueries = getMetricExpressions(
- widget,
- {release: ['1.0', '2.0']},
- mockGetVirtualMRIQuery
- );
- expect(metricQueries).toEqual([
- {
- groupBy: ['release'],
- id: 1,
- mri: 'd:transactions/duration@milisecond',
- aggregation: 'avg',
- query: 'release:[1.0,2.0]',
- type: MetricExpressionType.QUERY,
- orderBy: undefined,
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- ]);
- });
- it('should map span extracted metrics to virtual metrics', () => {
- const widget = {
- queries: [
- {
- aggregates: ['sum(g:custom/span_attribute_123@milisecond)'],
- conditions: 'foo:bar',
- columns: ['release'],
- name: '0',
- orderby: 'desc',
- },
- ],
- } as Widget;
- const metricQueries = getMetricExpressions(widget, undefined, mockGetVirtualMRIQuery);
- expect(metricQueries).toEqual([
- {
- groupBy: ['release'],
- id: 0,
- mri: 'v:custom/span.duration@milisecond',
- aggregation: 'sum',
- condition: 1,
- query: 'foo:bar',
- type: MetricExpressionType.QUERY,
- orderBy: 'desc',
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- ]);
- });
- });
- describe('toMetricDisplayType', () => {
- it('should return the displayType if it is a valid MetricDisplayType', () => {
- expect(MetricDisplayType.BAR).toEqual(toMetricDisplayType(DisplayType.BAR));
- expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.LINE));
- expect(MetricDisplayType.AREA).toEqual(toMetricDisplayType(DisplayType.AREA));
- });
- it('should return MetricDisplayType.LINE if the displayType is invalid or unsupported', () => {
- expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.BIG_NUMBER));
- expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.TABLE));
- expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.TOP_N));
- expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(undefined));
- expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(''));
- });
- });
- describe('expressionsToWidget', () => {
- it('should return a widget with queries', () => {
- const metricExpressions = [
- {
- groupBy: ['release'],
- id: 0,
- mri: 'd:transactions/duration@milisecond',
- aggregation: 'avg',
- query: 'foo:bar',
- type: MetricExpressionType.QUERY,
- orderBy: 'asc',
- isHidden: true,
- } satisfies DashboardMetricsExpression,
- ];
- const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
- expect(widget).toEqual({
- title: 'title',
- displayType: DisplayType.LINE,
- interval: '5m',
- limit: 10,
- widgetType: WidgetType.METRICS,
- queries: [
- {
- aggregates: ['avg(d:transactions/duration@milisecond)'],
- fields: ['avg(d:transactions/duration@milisecond)'],
- conditions: 'foo:bar',
- columns: ['release'],
- name: '0',
- orderby: 'asc',
- isHidden: true,
- fieldAliases: [],
- },
- ],
- } satisfies Widget);
- });
- it('should return a widget with equations', () => {
- const metricExpressions = [
- {
- id: 1,
- formula: '$a + $b',
- type: MetricExpressionType.EQUATION,
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- ];
- const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
- expect(widget).toEqual({
- title: 'title',
- displayType: DisplayType.LINE,
- interval: '5m',
- limit: 10,
- widgetType: WidgetType.METRICS,
- queries: [
- {
- aggregates: ['equation|$a + $b'],
- fields: ['equation|$a + $b'],
- conditions: '',
- columns: [],
- name: '1',
- orderby: '',
- isHidden: false,
- fieldAliases: [],
- },
- ],
- } satisfies Widget);
- });
- it('should should be reversible by getMetricExpressions', () => {
- const metricExpressions = [
- {
- groupBy: ['release'],
- id: 0,
- mri: 'd:transactions/duration@milisecond',
- aggregation: 'avg',
- query: 'foo:bar',
- type: MetricExpressionType.QUERY,
- orderBy: 'asc',
- isHidden: true,
- } satisfies DashboardMetricsExpression,
- {
- id: 1,
- formula: '$a + $b',
- type: MetricExpressionType.EQUATION,
- isHidden: false,
- } satisfies DashboardMetricsExpression,
- ];
- const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
- expect(getMetricExpressions(widget, undefined, mockGetVirtualMRIQuery)).toEqual(
- metricExpressions
- );
- });
- });
|