Skip to content

Commit

Permalink
Update Migration for thresholds color filter (#248)
Browse files Browse the repository at this point in the history
* colors migration added

* update migration

* Update CHANGELOG.md

---------

Co-authored-by: Mikhail Volkov <mikhail@volkovlabs.io>
  • Loading branch information
vitPinchuk and mikhail-vl authored Oct 25, 2024
1 parent 24000ac commit 8b76d1d
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features / Enhancements

- Add Multi Day Events in Yearly View (#243)
- Update Migration for thresholds color filter (#248)

## 3.7.0 (2024-09-15)

Expand Down
229 changes: 229 additions & 0 deletions src/migration.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ColorMode } from 'types';

import { getMigratedOptions } from './migration';

describe('Migration', () => {
Expand All @@ -6,6 +8,10 @@ describe('Migration', () => {
options: {
autoScroll: true,
},
fieldConfig: {
defaults: {},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);
Expand All @@ -18,6 +24,10 @@ describe('Migration', () => {
options: {
displayTime: true,
},
fieldConfig: {
defaults: {},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);
Expand All @@ -30,6 +40,10 @@ describe('Migration', () => {
options: {
calendarType: 'legacy',
},
fieldConfig: {
defaults: {},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);
Expand All @@ -42,6 +56,10 @@ describe('Migration', () => {
options: {
someOption: 'value',
},
fieldConfig: {
defaults: {},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);
Expand All @@ -55,6 +73,10 @@ describe('Migration', () => {
someOption: 'value',
descriptionField: 'description',
},
fieldConfig: {
defaults: {},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);
Expand All @@ -67,10 +89,217 @@ describe('Migration', () => {
options: {
someOption: 'value',
},
fieldConfig: {
defaults: {},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions).not.toHaveProperty('descriptionField');
});

it('Should update colors options for manual overrides', () => {
const panel = {
options: {
colors: ColorMode.FRAME,
colorField: 'color',
},
fieldConfig: {
defaults: {},
overrides: [
{
matcher: {
id: 'byName',
options: 'color',
},
properties: [{}],
},
],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.THRESHOLDS);
});

it('Should update colors options correctly via thresholds', () => {
const panel = {
options: {
colors: ColorMode.FRAME,
colorField: 'color',
},
fieldConfig: {
defaults: {
thresholds: {
steps: [{}, {}],
},
},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.THRESHOLDS);
});

it('Should update colors options correctly via overrides', () => {
const panel = {
options: {
colors: ColorMode.FRAME,
colorField: 'color',
},
fieldConfig: {
defaults: {
thresholds: {
steps: [{}],
},
},
overrides: [
{
matcher: {
options: 'color',
},
},
],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.THRESHOLDS);
});

it('Should not update colors options correctly via overrides if colorField doesn`t use in overrides', () => {
const panel = {
options: {
colors: ColorMode.FRAME,
colorField: 'color',
},
fieldConfig: {
defaults: {
thresholds: {
steps: [{}],
},
},
overrides: [
{
matcher: {
options: 'someField',
},
},
],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.FRAME);
});

it('Should not update colors options if set only base threshold value', () => {
const panel = {
options: {
colors: ColorMode.FRAME,
colorField: 'color',
},
fieldConfig: {
defaults: {
thresholds: {
steps: [
{
color: '#b1b3b5',
value: null,
},
],
},
},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.FRAME);
});

it('Should not update colors options if set event scheme', () => {
const panel = {
options: {
colors: ColorMode.EVENT,
colorField: 'color',
},
fieldConfig: {
defaults: {
thresholds: {
steps: [
{
color: '#b1b3b5',
value: null,
},
{
color: '#b1b3b5',
value: null,
},
],
},
},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.EVENT);
});

it('Should not update colors option if colorField not specified', () => {
const panel = {
options: {
colors: ColorMode.FRAME,
colorField: '',
},
fieldConfig: {
defaults: {
thresholds: {
steps: [
{
color: '#b1b3b5',
value: null,
},
{
color: '#b1b3b5',
value: null,
},
],
},
},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.FRAME);
});

it('Should not update colors option if defaults and overrides not specified', () => {
const panel = {
options: {
colors: ColorMode.FRAME,
colorField: 'color',
},
fieldConfig: {
defaults: {},
overrides: [],
},
} as any;

const migratedOptions = getMigratedOptions(panel);

expect(migratedOptions.colors).toEqual(ColorMode.FRAME);
});
});
15 changes: 14 additions & 1 deletion src/migration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PanelModel } from '@grafana/data';

import { CalendarOptions } from './types';
import { CalendarOptions, ColorMode } from './types';

/**
* Outdated Panel Options
Expand Down Expand Up @@ -34,6 +34,9 @@ interface OutdatedPanelOptions extends CalendarOptions {
*/
export const getMigratedOptions = (panel: PanelModel<OutdatedPanelOptions>): CalendarOptions => {
const { ...options } = panel.options;
const { overrides, defaults } = panel.fieldConfig;
const isColorFieldExistInOverrides =
!!overrides.length && overrides.some((override) => override.matcher.options === options.colorField);

/**
* Remove Legacy option autoScroll
Expand Down Expand Up @@ -63,5 +66,15 @@ export const getMigratedOptions = (panel: PanelModel<OutdatedPanelOptions>): Cal
options.descriptionField = [options.descriptionField];
}

/**
* Overrides color scheme
*/
if (
options.colors === ColorMode.FRAME &&
options.colorField &&
(isColorFieldExistInOverrides || (defaults.thresholds && defaults.thresholds.steps?.length > 1))
) {
options.colors = ColorMode.THRESHOLDS;
}
return options as CalendarOptions;
};

0 comments on commit 8b76d1d

Please sign in to comment.