From 4d4ead40c890faa1589ec76560f024b9b7bccf65 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Fri, 25 Jun 2021 09:50:08 +0100 Subject: [PATCH] TimeSeries: Preserve RegExp series overrides when transforming from old graph (#36134) --- .../__snapshots__/migrations.test.ts.snap | 54 +++++++++++++++++++ .../panel/timeseries/migrations.test.ts | 13 +++++ .../plugins/panel/timeseries/migrations.ts | 3 +- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/panel/timeseries/__snapshots__/migrations.test.ts.snap b/public/app/plugins/panel/timeseries/__snapshots__/migrations.test.ts.snap index 180a898127bd..342cbb434f81 100644 --- a/public/app/plugins/panel/timeseries/__snapshots__/migrations.test.ts.snap +++ b/public/app/plugins/panel/timeseries/__snapshots__/migrations.test.ts.snap @@ -155,6 +155,60 @@ Object { } `; +exports[`Graph Migrations preserves series overrides using a regex alias 1`] = ` +Object { + "fieldConfig": Object { + "defaults": Object { + "custom": Object { + "axisPlacement": "hidden", + "drawStyle": "line", + "fillOpacity": 60, + "gradientMode": "opacity", + "lineInterpolation": "stepAfter", + "lineWidth": 1, + "showPoints": "never", + "spanNulls": true, + }, + "nullValueMode": "null", + "unit": "short", + }, + "overrides": Array [ + Object { + "matcher": Object { + "id": "byRegexp", + "options": "/^A-/", + }, + "properties": Array [ + Object { + "id": "color", + "value": Object { + "fixedColor": "rgba(165, 72, 170, 0.77)", + "mode": "fixed", + }, + }, + ], + }, + ], + }, + "options": Object { + "legend": Object { + "calcs": Array [ + "mean", + "lastNotNull", + "max", + "min", + "sum", + ], + "displayMode": "table", + "placement": "bottom", + }, + "tooltip": Object { + "mode": "single", + }, + }, +} +`; + exports[`Graph Migrations simple bars 1`] = ` Object { "fieldConfig": Object { diff --git a/public/app/plugins/panel/timeseries/migrations.test.ts b/public/app/plugins/panel/timeseries/migrations.test.ts index 289fa80d7a3a..af12556a9d75 100644 --- a/public/app/plugins/panel/timeseries/migrations.test.ts +++ b/public/app/plugins/panel/timeseries/migrations.test.ts @@ -1,5 +1,6 @@ import { PanelModel, FieldConfigSource } from '@grafana/data'; import { graphPanelChangedHandler } from './migrations'; +import { cloneDeep } from 'lodash'; describe('Graph Migrations', () => { let prevFieldConfig: FieldConfigSource; @@ -67,6 +68,15 @@ describe('Graph Migrations', () => { expect(panel).toMatchSnapshot(); }); + it('preserves series overrides using a regex alias', () => { + const old: any = { + angular: customColorRegex, + }; + const panel = {} as PanelModel; + panel.options = graphPanelChangedHandler(panel, 'graph', old, prevFieldConfig); + expect(panel).toMatchSnapshot(); + }); + describe('legend', () => { test('without values', () => { const old: any = { @@ -383,6 +393,9 @@ const customColor = { datasource: null, }; +const customColorRegex = cloneDeep(customColor); +customColorRegex.seriesOverrides[0].alias = '/^A-/'; + const stairscase = { aliasColors: {}, dashLength: 10, diff --git a/public/app/plugins/panel/timeseries/migrations.ts b/public/app/plugins/panel/timeseries/migrations.ts index 3fc27251446d..20aca2856781 100644 --- a/public/app/plugins/panel/timeseries/migrations.ts +++ b/public/app/plugins/panel/timeseries/migrations.ts @@ -112,9 +112,10 @@ export function flotToGraphOptions(angular: any): { fieldConfig: FieldConfigSour if (!seriesOverride.alias) { continue; // the matcher config } + const aliasIsRegex = seriesOverride.alias.startsWith('/') && seriesOverride.alias.endsWith('/'); const rule: ConfigOverrideRule = { matcher: { - id: FieldMatcherID.byName, + id: aliasIsRegex ? FieldMatcherID.byRegexp : FieldMatcherID.byName, options: seriesOverride.alias, }, properties: [],