migrations.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { isArray } from 'lodash';
  2. import { FieldConfigSource, MappingType, PanelModel, ValueMap } from '@grafana/data';
  3. import { TimelineFieldConfig, TimelineOptions } from './types';
  4. // This is called when the panel changes from another panel
  5. export const timelinePanelChangedHandler = (
  6. panel: PanelModel<Partial<TimelineOptions>> | any,
  7. prevPluginId: string,
  8. prevOptions: any
  9. ) => {
  10. let options = (panel.options ?? {}) as TimelineOptions;
  11. // Changing from angular singlestat
  12. if (prevPluginId === 'natel-discrete-panel' && prevOptions.angular) {
  13. const oldOptions = prevOptions.angular;
  14. const fieldConfig: FieldConfigSource = panel.fieldConfig ?? { defaults: {}, overrides: [] };
  15. if (oldOptions.units) {
  16. fieldConfig.defaults.unit = oldOptions.units;
  17. }
  18. const custom: TimelineFieldConfig = {
  19. fillOpacity: 100,
  20. lineWidth: 0,
  21. };
  22. fieldConfig.defaults.custom = custom;
  23. options.mergeValues = true;
  24. // Convert mappings
  25. const valuemap: ValueMap = { type: MappingType.ValueToText, options: {} };
  26. fieldConfig.defaults.mappings = [valuemap];
  27. if (isArray(oldOptions.colorMaps)) {
  28. for (const p of oldOptions.colorMaps) {
  29. const color = p.color as string;
  30. if (color) {
  31. valuemap.options[p.text as string] = { color };
  32. }
  33. }
  34. }
  35. if (isArray(oldOptions.valueMaps)) {
  36. for (const p of oldOptions.valueMaps) {
  37. const text = p.text as string;
  38. const value = p.value as string;
  39. if (text && value) {
  40. let old = valuemap.options[value];
  41. if (old) {
  42. old.text = text;
  43. } else {
  44. valuemap.options[value] = { text };
  45. }
  46. }
  47. }
  48. }
  49. if (isArray(oldOptions.rangeMaps)) {
  50. for (const p of oldOptions.rangeMaps) {
  51. let from = +p.from;
  52. let to = +p.to;
  53. const text = p.text as string;
  54. if (text) {
  55. fieldConfig.defaults.mappings.push({
  56. type: MappingType.RangeToText,
  57. options: {
  58. from,
  59. to,
  60. result: { text },
  61. },
  62. });
  63. }
  64. }
  65. }
  66. // mutates the input
  67. panel.fieldConfig = fieldConfig;
  68. }
  69. return options;
  70. };