2019-03-24 17:03:27 +01:00
|
|
|
import { ReactPanelPlugin, getStatsCalculators, PanelModel } from '@grafana/ui';
|
2019-03-15 08:38:29 -07:00
|
|
|
import { SingleStatOptions, defaults, SingleStatBaseOptions } from './types';
|
2019-03-14 13:20:24 -07:00
|
|
|
import { SingleStatPanel } from './SingleStatPanel';
|
|
|
|
|
import cloneDeep from 'lodash/cloneDeep';
|
|
|
|
|
import { SingleStatEditor } from './SingleStatEditor';
|
|
|
|
|
|
|
|
|
|
const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'thresholds', 'valueMappings'];
|
|
|
|
|
|
2019-03-15 08:38:29 -07:00
|
|
|
export const singleStatBaseOptionsCheck = (
|
|
|
|
|
options: Partial<SingleStatBaseOptions>,
|
2019-03-22 10:22:25 -07:00
|
|
|
prevPluginId: string,
|
2019-03-24 18:34:43 +01:00
|
|
|
prevOptions: any
|
2019-03-14 13:20:24 -07:00
|
|
|
) => {
|
2019-03-24 18:34:43 +01:00
|
|
|
optionsToKeep.forEach(v => {
|
|
|
|
|
if (prevOptions.hasOwnProperty(v)) {
|
|
|
|
|
options[v] = cloneDeep(prevOptions.display);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-03-22 10:22:25 -07:00
|
|
|
return options;
|
|
|
|
|
};
|
2019-03-14 13:20:24 -07:00
|
|
|
|
2019-03-24 17:03:27 +01:00
|
|
|
export const singleStatMigrationCheck = (panel: PanelModel<SingleStatOptions>) => {
|
|
|
|
|
const options = panel.options;
|
2019-03-22 13:45:09 -07:00
|
|
|
if (options.valueOptions) {
|
|
|
|
|
// 6.1 renamed some stats, This makes sure they are up to date
|
|
|
|
|
// avg -> mean, current -> last, total -> sum
|
|
|
|
|
const { valueOptions } = options;
|
|
|
|
|
if (valueOptions && valueOptions.stat) {
|
|
|
|
|
valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0];
|
|
|
|
|
}
|
2019-03-22 00:14:50 -07:00
|
|
|
}
|
2019-03-14 13:20:24 -07:00
|
|
|
return options;
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-24 15:56:32 +01:00
|
|
|
export const reactPanel = new ReactPanelPlugin<SingleStatOptions>(SingleStatPanel)
|
|
|
|
|
.setDefaults(defaults)
|
|
|
|
|
.setEditor(SingleStatEditor)
|
|
|
|
|
.setPanelChangeHandler(singleStatMigrationCheck)
|
|
|
|
|
.setMigrationHandler(singleStatMigrationCheck);
|