SingleStatBaseOptions.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import cloneDeep from 'lodash/cloneDeep';
  2. import omit from 'lodash/omit';
  3. import { VizOrientation, PanelModel } from '../../types/panel';
  4. import { FieldDisplayOptions } from '../../utils/fieldDisplay';
  5. import { Field, fieldReducers, Threshold, sortThresholds } from '@grafana/data';
  6. export interface SingleStatBaseOptions {
  7. fieldOptions: FieldDisplayOptions;
  8. orientation: VizOrientation;
  9. }
  10. const optionsToKeep = ['fieldOptions', 'orientation'];
  11. export const sharedSingleStatOptionsCheck = (
  12. options: Partial<SingleStatBaseOptions> | any,
  13. prevPluginId: string,
  14. prevOptions: any
  15. ) => {
  16. for (const k of optionsToKeep) {
  17. if (prevOptions.hasOwnProperty(k)) {
  18. options[k] = cloneDeep(prevOptions[k]);
  19. }
  20. }
  21. return options;
  22. };
  23. export const sharedSingleStatMigrationCheck = (panel: PanelModel<SingleStatBaseOptions>) => {
  24. if (!panel.options) {
  25. // This happens on the first load or when migrating from angular
  26. return {};
  27. }
  28. // This migration aims to keep the most recent changes up-to-date
  29. // Plugins should explicitly migrate for known version changes and only use this
  30. // as a backup
  31. const old = panel.options as any;
  32. if (old.valueOptions) {
  33. const { valueOptions } = old;
  34. const fieldOptions = (old.fieldOptions = {} as FieldDisplayOptions);
  35. const field = (fieldOptions.defaults = {} as Field);
  36. field.mappings = old.valueMappings;
  37. field.thresholds = migrateOldThresholds(old.thresholds);
  38. field.unit = valueOptions.unit;
  39. field.decimals = valueOptions.decimals;
  40. // Make sure the stats have a valid name
  41. if (valueOptions.stat) {
  42. const reducer = fieldReducers.get(valueOptions.stat);
  43. if (reducer) {
  44. fieldOptions.calcs = [reducer.id];
  45. }
  46. }
  47. field.min = old.minValue;
  48. field.max = old.maxValue;
  49. // remove old props
  50. return omit(old, 'valueMappings', 'thresholds', 'valueOptions', 'minValue', 'maxValue');
  51. } else if (old.fieldOptions) {
  52. // Move mappins & thresholds to field defautls (6.4+)
  53. const { mappings, thresholds, ...fieldOptions } = old.fieldOptions;
  54. fieldOptions.defaults = {
  55. mappings,
  56. thresholds: migrateOldThresholds(thresholds),
  57. ...fieldOptions.defaults,
  58. };
  59. old.fieldOptions = fieldOptions;
  60. return old;
  61. }
  62. return panel.options;
  63. };
  64. export function migrateOldThresholds(thresholds?: any[]): Threshold[] | undefined {
  65. if (!thresholds || !thresholds.length) {
  66. return undefined;
  67. }
  68. const copy = thresholds.map(t => {
  69. return {
  70. // Drops 'index'
  71. value: t.value === null ? -Infinity : t.value,
  72. color: t.color,
  73. };
  74. });
  75. sortThresholds(copy);
  76. copy[0].value = -Infinity;
  77. return copy;
  78. }