From e777729f6f027a8d9a183affc2284d3289383092 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Tue, 1 Jun 2021 04:15:40 -0400 Subject: [PATCH] TimeSeries: Migration from old graph legend values (#34997) (#35018) * fix(timeseries): filter enabled graph legend values when migrating * test(timeseries): update snapshots * test(timeseries): add additional tests for legend (cherry picked from commit cb3c317e8134224aabdcc84f1dae61351151a6ff) Co-authored-by: Jack Westbrook --- .../__snapshots__/migrations.test.ts.snap | 58 ++++++++++++++++--- .../panel/timeseries/migrations.test.ts | 52 ++++++++++++++--- .../plugins/panel/timeseries/migrations.ts | 5 +- 3 files changed, 99 insertions(+), 16 deletions(-) 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 9bd2fa51f31..ec5e1cc683d 100644 --- a/public/app/plugins/panel/timeseries/__snapshots__/migrations.test.ts.snap +++ b/public/app/plugins/panel/timeseries/__snapshots__/migrations.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Graph Migrations legend 1`] = ` +exports[`Graph Migrations legend with multiple values 1`] = ` Object { "fieldConfig": Object { "defaults": Object { @@ -39,8 +39,6 @@ Object { "calcs": Array [ "mean", "lastNotNull", - "max", - "min", "sum", ], "displayMode": "table", @@ -53,6 +51,56 @@ Object { } `; +exports[`Graph Migrations legend with single value 1`] = ` +Object { + "fieldConfig": Object { + "defaults": Object { + "custom": Object { + "drawStyle": "points", + "spanNulls": false, + }, + }, + "overrides": Array [], + }, + "options": Object { + "legend": Object { + "calcs": Array [ + "sum", + ], + "displayMode": "list", + "placement": "bottom", + }, + "tooltip": Object { + "mode": "single", + }, + }, +} +`; + +exports[`Graph Migrations legend without values 1`] = ` +Object { + "fieldConfig": Object { + "defaults": Object { + "custom": Object { + "drawStyle": "points", + "spanNulls": false, + }, + }, + "overrides": Array [], + }, + "options": Object { + "legend": Object { + "calcs": Array [], + "displayMode": "list", + "placement": "bottom", + }, + "tooltip": Object { + "mode": "single", + }, + }, +} +`; + exports[`Graph Migrations simple bars 1`] = ` Object { "fieldConfig": Object { @@ -152,8 +200,6 @@ Object { "calcs": Array [ "mean", "lastNotNull", - "max", - "min", "sum", ], "displayMode": "table", @@ -209,8 +255,6 @@ Object { "calcs": Array [ "mean", "lastNotNull", - "max", - "min", "sum", ], "displayMode": "table", diff --git a/public/app/plugins/panel/timeseries/migrations.test.ts b/public/app/plugins/panel/timeseries/migrations.test.ts index 8e678c64cd7..5086a53ffca 100644 --- a/public/app/plugins/panel/timeseries/migrations.test.ts +++ b/public/app/plugins/panel/timeseries/migrations.test.ts @@ -40,13 +40,51 @@ describe('Graph Migrations', () => { expect(panel).toMatchSnapshot(); }); - it('legend', () => { - const old: any = { - angular: legend, - }; - const panel = {} as PanelModel; - panel.options = graphPanelChangedHandler(panel, 'graph', old); - expect(panel).toMatchSnapshot(); + describe('legend', () => { + test('without values', () => { + const old: any = { + angular: { + legend: { + show: true, + values: false, + min: false, + max: false, + current: false, + total: false, + avg: false, + }, + }, + }; + const panel = {} as PanelModel; + panel.options = graphPanelChangedHandler(panel, 'graph', old); + expect(panel).toMatchSnapshot(); + }); + test('with single value', () => { + const old: any = { + angular: { + legend: { + show: true, + values: true, + min: false, + max: false, + current: false, + total: true, + avg: false, + }, + }, + }; + const panel = {} as PanelModel; + panel.options = graphPanelChangedHandler(panel, 'graph', old); + expect(panel).toMatchSnapshot(); + }); + test('with multiple values', () => { + const old: any = { + angular: legend, + }; + const panel = {} as PanelModel; + panel.options = graphPanelChangedHandler(panel, 'graph', old); + expect(panel).toMatchSnapshot(); + }); }); describe('stacking', () => { diff --git a/public/app/plugins/panel/timeseries/migrations.ts b/public/app/plugins/panel/timeseries/migrations.ts index 8a28461d94a..48f96b20261 100644 --- a/public/app/plugins/panel/timeseries/migrations.ts +++ b/public/app/plugins/panel/timeseries/migrations.ts @@ -26,7 +26,7 @@ import { TooltipDisplayMode, } from '@grafana/ui'; import { TimeSeriesOptions } from './types'; -import { omitBy, isNil, isNumber, isString } from 'lodash'; +import { omitBy, pickBy, isNil, isNumber, isString } from 'lodash'; import { defaultGraphConfig } from './config'; /** @@ -314,7 +314,8 @@ export function flotToGraphOptions(angular: any): { fieldConfig: FieldConfigSour } if (angular.legend.values) { - options.legend.calcs = getReducersFromLegend(angular.legend); + const enabledLegendValues = pickBy(angular.legend); + options.legend.calcs = getReducersFromLegend(enabledLegendValues); } }