Thresholds: Fixed issue with thresholds in overrides not working after save and reload (#27297)

* WIP: Fix null thresholds in overrides when loading

* Fix thresholds on load instead of in apply field overrides

* simplify expression

* fixed ts issue

* Updated test

* Updated another test

* Updated another test
This commit is contained in:
Torkel Ödegaard
2020-09-01 16:00:38 +02:00
committed by GitHub
parent d07755b624
commit 1f6c2dbcfb
7 changed files with 76 additions and 57 deletions

View File

@@ -16,6 +16,8 @@ import {
PanelEvents,
PanelPlugin,
ScopedVars,
ThresholdsConfig,
ThresholdsMode,
} from '@grafana/data';
import { EDIT_PANEL_ID } from 'app/core/constants';
import config from 'app/core/config';
@@ -317,22 +319,7 @@ export class PanelModel implements DataConfigSource {
}
});
this.fieldConfig = {
defaults: _.mergeWith(
{},
plugin.fieldConfigDefaults.defaults,
this.fieldConfig ? this.fieldConfig.defaults : {},
(objValue: any, srcValue: any): any => {
if (_.isArray(srcValue)) {
return srcValue;
}
}
),
overrides: [
...plugin.fieldConfigDefaults.overrides,
...(this.fieldConfig && this.fieldConfig.overrides ? this.fieldConfig.overrides : []),
],
};
this.fieldConfig = applyFieldConfigDefaults(this.fieldConfig, this.plugin!.fieldConfigDefaults);
}
pluginLoaded(plugin: PanelPlugin) {
@@ -503,6 +490,51 @@ export class PanelModel implements DataConfigSource {
}
}
function applyFieldConfigDefaults(fieldConfig: FieldConfigSource, defaults: FieldConfigSource): FieldConfigSource {
const result: FieldConfigSource = {
defaults: _.mergeWith(
{},
defaults.defaults,
fieldConfig ? fieldConfig.defaults : {},
(objValue: any, srcValue: any): any => {
if (_.isArray(srcValue)) {
return srcValue;
}
}
),
overrides: fieldConfig?.overrides ?? [],
};
// Thresholds base values are null in JSON but need to be converted to -Infinity
if (result.defaults.thresholds) {
fixThresholds(result.defaults.thresholds);
}
for (const override of result.overrides) {
for (const property of override.properties) {
if (property.id === 'thresholds') {
fixThresholds(property.value as ThresholdsConfig);
}
}
}
return result;
}
function fixThresholds(thresholds: ThresholdsConfig) {
if (!thresholds.mode) {
thresholds.mode = ThresholdsMode.Absolute;
}
if (!thresholds.steps) {
thresholds.steps = [];
} else if (thresholds.steps.length) {
// First value is always -Infinity
// JSON saves it as null
thresholds.steps[0].value = -Infinity;
}
}
function getPluginVersion(plugin: PanelPlugin): string {
return plugin && plugin.meta.info.version ? plugin.meta.info.version : config.buildInfo.version;
}