grafana/public/app/plugins/panel/state-timeline/migrations.ts
Victor Marin 6a93c77082
Refactor state timeline/status history to cue model and refactor TimelineChart component (#61631)
* Adapt state timeline to scuemata

* Refactor status history to cue model

* Refactor

* Refactor TimelineChart as a core component

* wip

* Change as per CR

Co-authored-by: sam boyer <sdboyer@grafana.com>
2023-01-26 07:03:59 +00:00

83 lines
2.2 KiB
TypeScript

import { isArray } from 'lodash';
import { FieldConfigSource, MappingType, PanelModel, ValueMap } from '@grafana/data';
import { PanelFieldConfig, PanelOptions } from './panelcfg.gen';
// This is called when the panel changes from another panel
export const timelinePanelChangedHandler = (
panel: PanelModel<Partial<PanelOptions>> | any,
prevPluginId: string,
prevOptions: any
) => {
let options = (panel.options ?? {}) as PanelOptions;
// Changing from angular singlestat
if (prevPluginId === 'natel-discrete-panel' && prevOptions.angular) {
const oldOptions = prevOptions.angular;
const fieldConfig: FieldConfigSource = panel.fieldConfig ?? { defaults: {}, overrides: [] };
if (oldOptions.units) {
fieldConfig.defaults.unit = oldOptions.units;
}
const custom: PanelFieldConfig = {
fillOpacity: 100,
lineWidth: 0,
};
fieldConfig.defaults.custom = custom;
options.mergeValues = true;
// Convert mappings
const valuemap: ValueMap = { type: MappingType.ValueToText, options: {} };
fieldConfig.defaults.mappings = [valuemap];
if (isArray(oldOptions.colorMaps)) {
for (const p of oldOptions.colorMaps) {
const color = p.color as string;
if (color) {
valuemap.options[p.text as string] = { color };
}
}
}
if (isArray(oldOptions.valueMaps)) {
for (const p of oldOptions.valueMaps) {
const text = p.text as string;
const value = p.value as string;
if (text && value) {
let old = valuemap.options[value];
if (old) {
old.text = text;
} else {
valuemap.options[value] = { text };
}
}
}
}
if (isArray(oldOptions.rangeMaps)) {
for (const p of oldOptions.rangeMaps) {
let from = +p.from;
let to = +p.to;
const text = p.text as string;
if (text) {
fieldConfig.defaults.mappings.push({
type: MappingType.RangeToText,
options: {
from,
to,
result: { text },
},
});
}
}
}
// mutates the input
panel.fieldConfig = fieldConfig;
}
return options;
};