module.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { ReactPanelPlugin, getStatsCalculators } 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. if (prevOptions) {
  13. optionsToKeep.forEach(v => {
  14. if (prevOptions.hasOwnProperty(v)) {
  15. options[v] = cloneDeep(prevOptions.display);
  16. }
  17. });
  18. }
  19. return options;
  20. };
  21. export const singleStatMigrationCheck = (exiting: any, oldVersion?: string) => {
  22. const options = exiting as Partial<SingleStatOptions>;
  23. if (options.valueOptions) {
  24. // 6.1 renamed some stats, This makes sure they are up to date
  25. // avg -> mean, current -> last, total -> sum
  26. const { valueOptions } = options;
  27. if (valueOptions && valueOptions.stat) {
  28. valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0];
  29. }
  30. }
  31. return options;
  32. };
  33. export const reactPanel = new ReactPanelPlugin<SingleStatOptions>(SingleStatPanel, defaults);
  34. reactPanel.editor = SingleStatEditor;
  35. reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;
  36. reactPanel.onPanelMigration = singleStatMigrationCheck;