GaugeMigrations.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { PanelModel, Field, getFieldReducers } from '@grafana/ui';
  2. import { GaugeOptions } from './types';
  3. import { sharedSingleStatMigrationCheck } from '@grafana/ui/src/components/SingleStatShared/SingleStatBaseOptions';
  4. import { FieldDisplayOptions } from '@grafana/ui/src/utils/fieldDisplay';
  5. export const gaugePanelMigrationCheck = (panel: PanelModel<GaugeOptions>): Partial<GaugeOptions> => {
  6. if (!panel.options) {
  7. // This happens on the first load or when migrating from angular
  8. return {};
  9. }
  10. if (!panel.pluginVersion || panel.pluginVersion.startsWith('6.1')) {
  11. const old = panel.options as any;
  12. const { valueOptions } = old;
  13. const options = {} as GaugeOptions;
  14. options.showThresholdLabels = old.showThresholdLabels;
  15. options.showThresholdMarkers = old.showThresholdMarkers;
  16. options.orientation = old.orientation;
  17. const fieldOptions = (options.fieldOptions = {} as FieldDisplayOptions);
  18. fieldOptions.mappings = old.valueMappings;
  19. fieldOptions.thresholds = old.thresholds;
  20. const field = (fieldOptions.defaults = {} as Field);
  21. if (valueOptions) {
  22. field.unit = valueOptions.unit;
  23. field.decimals = valueOptions.decimals;
  24. // Make sure the stats have a valid name
  25. if (valueOptions.stat) {
  26. fieldOptions.calcs = getFieldReducers([valueOptions.stat]).map(s => s.id);
  27. }
  28. }
  29. field.min = old.minValue;
  30. field.max = old.maxValue;
  31. return options;
  32. }
  33. // Default to the standard migration path
  34. return sharedSingleStatMigrationCheck(panel);
  35. };