module.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { ReactPanelPlugin, getStatsCalculators, PanelModel } from '@grafana/ui';
  2. import { SingleStatOptions, defaults, SingleStatBaseOptions } from './types';
  3. import { SingleStatPanel } from './SingleStatPanel';
  4. import cloneDeep from 'lodash/cloneDeep';
  5. import { SingleStatEditor } from './SingleStatEditor';
  6. const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'thresholds', 'valueMappings'];
  7. export const singleStatBaseOptionsCheck = (
  8. options: Partial<SingleStatBaseOptions>,
  9. prevPluginId: string,
  10. prevOptions: any
  11. ) => {
  12. for (const k of optionsToKeep) {
  13. if (prevOptions.hasOwnProperty(k)) {
  14. options[k] = cloneDeep(prevOptions[k]);
  15. }
  16. }
  17. return options;
  18. };
  19. export const singleStatMigrationCheck = (panel: PanelModel<SingleStatOptions>) => {
  20. const options = panel.options;
  21. if (!options) {
  22. // This happens on the first load or when migrating from angular
  23. return {};
  24. }
  25. if (options.valueOptions) {
  26. // 6.1 renamed some stats, This makes sure they are up to date
  27. // avg -> mean, current -> last, total -> sum
  28. const { valueOptions } = options;
  29. if (valueOptions && valueOptions.stat) {
  30. valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0];
  31. }
  32. }
  33. return options;
  34. };
  35. export const reactPanel = new ReactPanelPlugin<SingleStatOptions>(SingleStatPanel)
  36. .setDefaults(defaults)
  37. .setEditor(SingleStatEditor)
  38. .setPanelChangeHandler(singleStatMigrationCheck)
  39. .setMigrationHandler(singleStatMigrationCheck);