123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- import {
- MetricChartOverlayType,
- MetricDisplayType,
- MetricExpressionType,
- type MetricsWidget,
- } from 'sentry/utils/metrics/types';
- import {parseMetricWidgetsQueryParam} from 'sentry/views/metrics/utils/parseMetricWidgetsQueryParam';
- function testParsing(input: any, result: MetricsWidget[]) {
- expect(parseMetricWidgetsQueryParam(JSON.stringify(input))).toStrictEqual(result);
- }
- describe('parseMetricWidgetsQueryParam', () => {
- it('returns empty array for invalid param', () => {
- testParsing(undefined, []);
- testParsing({}, []);
- testParsing(true, []);
- testParsing(2, []);
- testParsing('', []);
- testParsing('test', []);
- testParsing([], []);
- });
- it('returns a single widget', () => {
- testParsing(
- [
- // INPUT
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'sum',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: 'line',
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {order: 'asc'},
- isHidden: true,
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'sum',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: undefined, order: 'asc'},
- isHidden: true,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- });
- it('returns multiple widgets', () => {
- testParsing(
- // INPUT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'sum',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: 'line',
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: 'avg', order: 'desc'},
- isHidden: true,
- },
- {
- id: 0,
- type: MetricExpressionType.EQUATION,
- formula: 'a + b',
- displayType: 'line',
- sort: {name: 'avg', order: 'desc'},
- focusedSeries: [],
- isHidden: true,
- },
- {
- id: 1,
- type: MetricExpressionType.QUERY,
- mri: 'd:custom/sentry.event_manager.save@second',
- aggregation: 'avg',
- condition: 1,
- query: '',
- groupBy: ['event_type'],
- displayType: 'line',
- powerUserMode: false,
- focusedSeries: [{id: 'default', groupBy: {event_type: 'default'}}],
- sort: {name: 'sum', order: 'asc'},
- isHidden: false,
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'sum',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: 'avg', order: 'desc'},
- isHidden: true,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- {
- id: 1,
- type: MetricExpressionType.QUERY,
- mri: 'd:custom/sentry.event_manager.save@second',
- aggregation: 'avg',
- condition: 1,
- query: '',
- groupBy: ['event_type'],
- displayType: MetricDisplayType.LINE,
- powerUserMode: false,
- focusedSeries: [{id: 'default', groupBy: {event_type: 'default'}}],
- sort: {name: 'sum', order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- // Formulas should always be at the end
- {
- id: 0,
- type: MetricExpressionType.EQUATION,
- formula: 'a + b',
- displayType: MetricDisplayType.LINE,
- sort: {name: 'avg', order: 'desc'},
- focusedSeries: [],
- isHidden: true,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- });
- it('falls back to defaults', () => {
- // Missing values
- testParsing(
- // INPUT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- },
- {
- type: MetricExpressionType.EQUATION,
- formula: 'a * 2',
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'avg',
- condition: undefined,
- query: '',
- groupBy: [],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [],
- powerUserMode: false,
- sort: {name: undefined, order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- {
- id: 0,
- type: MetricExpressionType.EQUATION,
- formula: 'a * 2',
- displayType: MetricDisplayType.LINE,
- focusedSeries: [],
- sort: {name: undefined, order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- // Invalid values
- testParsing(
- // INPUT
- [
- {
- id: 'invalid',
- type: 123,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 1,
- condition: 'test',
- query: 12,
- groupBy: true,
- displayType: 'aasfcsdf',
- focusedSeries: {},
- powerUserMode: 1,
- sort: {name: 1, order: 'invalid'},
- isHidden: 'foo',
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'avg',
- condition: undefined,
- query: '',
- groupBy: [],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [],
- powerUserMode: false,
- sort: {name: undefined, order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- });
- it('ignores invalid widgets', () => {
- testParsing(
- // INPUT
- [
- {
- id: 0,
- mri: 'd:transactions/duration@millisecond',
- },
- {
- // Missing MRI
- },
- {
- // Mallformed MRI
- mri: 'transactions/duration@millisecond',
- },
- {
- // Duplicate id
- id: 0,
- mri: 'd:transactions/duration@second',
- },
- {
- // Missing formula
- type: MetricExpressionType.EQUATION,
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'avg',
- condition: undefined,
- query: '',
- groupBy: [],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [],
- powerUserMode: false,
- sort: {name: undefined, order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- });
- it('returns empty array if there is no valid widget', () => {
- testParsing(
- // INPUT
- [
- {
- // Missing MRI
- },
- {
- // Missing formula
- type: MetricExpressionType.EQUATION,
- },
- ],
- // RESULT
- []
- );
- });
- it('handles missing array in array params', () => {
- testParsing(
- // INPUT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- condition: 1,
- aggregation: 'sum',
- query: 'test:query',
- groupBy: 'dist',
- displayType: 'line',
- focusedSeries: {id: 'default', groupBy: {dist: 'default'}},
- powerUserMode: true,
- sort: {order: 'asc'},
- isHidden: false,
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- condition: 1,
- aggregation: 'sum',
- query: 'test:query',
- groupBy: ['dist'],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: undefined, order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- });
- it('handles overlays array in array params', () => {
- testParsing(
- // INPUT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- condition: 1,
- aggregation: 'sum',
- query: 'test:query',
- groupBy: 'dist',
- displayType: 'line',
- focusedSeries: {id: 'default', groupBy: {dist: 'default'}},
- powerUserMode: true,
- sort: {order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES, MetricChartOverlayType.RELEASES],
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- condition: 1,
- aggregation: 'sum',
- query: 'test:query',
- groupBy: ['dist'],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: undefined, order: 'asc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES, MetricChartOverlayType.RELEASES],
- },
- ]
- );
- });
- it('adds missing ids', () => {
- function widgetWithId<T extends number | undefined>(id: T) {
- return {
- id,
- type: MetricExpressionType.QUERY as const,
- mri: 'd:transactions/duration@millisecond' as const,
- aggregation: 'sum' as const,
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: 'avg' as const, order: 'desc' as const},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- };
- }
- testParsing(
- // INPUT
- [
- widgetWithId(0),
- widgetWithId(undefined),
- widgetWithId(2),
- {
- // Invalid widget
- },
- widgetWithId(undefined),
- widgetWithId(3),
- ],
- // RESULT
- [
- widgetWithId(0),
- widgetWithId(1),
- widgetWithId(2),
- widgetWithId(4),
- widgetWithId(3),
- ]
- );
- });
- it('resets the id of a single widget to 0', () => {
- testParsing(
- // INPUT
- [
- {
- id: 5,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'sum',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: 'line',
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: 'avg', order: 'desc'},
- isHidden: false,
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'sum',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: 'avg', order: 'desc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- });
- it('tries to parse op if aggregation is not present', () => {
- testParsing(
- // INPUT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: 'line',
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: 'avg', order: 'desc'},
- isHidden: false,
- op: 'avg',
- },
- ],
- // RESULT
- [
- {
- id: 0,
- type: MetricExpressionType.QUERY,
- mri: 'd:transactions/duration@millisecond',
- aggregation: 'avg',
- condition: 1,
- query: 'test:query',
- groupBy: ['dist'],
- displayType: MetricDisplayType.LINE,
- focusedSeries: [{id: 'default', groupBy: {dist: 'default'}}],
- powerUserMode: true,
- sort: {name: 'avg', order: 'desc'},
- isHidden: false,
- overlays: [MetricChartOverlayType.SAMPLES],
- },
- ]
- );
- });
- });
|