Skip to content

Commit d9e9843

Browse files
authored
FieldConfig: add thresholds and color modes (grafana#21273)
1 parent 36aad1c commit d9e9843

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1593
-724
lines changed

devenv/dev-dashboards/datasource-testdata/bar-gauge-demo2.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"orientation": "horizontal",
5353
"showUnfilled": true
5454
},
55-
"pluginVersion": "6.6.0-pre",
55+
"pluginVersion": "6.5.0-pre",
5656
"targets": [
5757
{
5858
"refId": "A",
@@ -169,7 +169,7 @@
169169
"orientation": "vertical",
170170
"showUnfilled": true
171171
},
172-
"pluginVersion": "6.6.0-pre",
172+
"pluginVersion": "6.5.0-pre",
173173
"targets": [
174174
{
175175
"refId": "A",
@@ -244,7 +244,7 @@
244244
"orientation": "horizontal",
245245
"showUnfilled": true
246246
},
247-
"pluginVersion": "6.6.0-pre",
247+
"pluginVersion": "6.5.0-pre",
248248
"targets": [
249249
{
250250
"refId": "A",
@@ -351,7 +351,7 @@
351351
"orientation": "vertical",
352352
"showUnfilled": true
353353
},
354-
"pluginVersion": "6.6.0-pre",
354+
"pluginVersion": "6.5.0-pre",
355355
"targets": [
356356
{
357357
"refId": "A",
@@ -430,7 +430,7 @@
430430
"orientation": "vertical",
431431
"showUnfilled": true
432432
},
433-
"pluginVersion": "6.6.0-pre",
433+
"pluginVersion": "6.5.0-pre",
434434
"targets": [
435435
{
436436
"refId": "A",

devenv/dev-dashboards/panel-bargauge/panel_tests_bar_gauge2.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"orientation": "vertical",
5959
"showUnfilled": false
6060
},
61-
"pluginVersion": "6.6.0-pre",
61+
"pluginVersion": "6.5.0-pre",
6262
"targets": [
6363
{
6464
"refId": "C",
@@ -159,7 +159,7 @@
159159
"orientation": "vertical",
160160
"showUnfilled": true
161161
},
162-
"pluginVersion": "6.6.0-pre",
162+
"pluginVersion": "6.5.0-pre",
163163
"targets": [
164164
{
165165
"refId": "H",
@@ -249,7 +249,7 @@
249249
"orientation": "horizontal",
250250
"showUnfilled": false
251251
},
252-
"pluginVersion": "6.6.0-pre",
252+
"pluginVersion": "6.5.0-pre",
253253
"targets": [
254254
{
255255
"alias": "Inside",
@@ -325,7 +325,7 @@
325325
"orientation": "horizontal",
326326
"showUnfilled": true
327327
},
328-
"pluginVersion": "6.6.0-pre",
328+
"pluginVersion": "6.5.0-pre",
329329
"targets": [
330330
{
331331
"alias": "Inside",
@@ -401,7 +401,7 @@
401401
"orientation": "horizontal",
402402
"showUnfilled": false
403403
},
404-
"pluginVersion": "6.6.0-pre",
404+
"pluginVersion": "6.5.0-pre",
405405
"targets": [
406406
{
407407
"alias": "Inside",
@@ -477,7 +477,7 @@
477477
"orientation": "horizontal",
478478
"showUnfilled": true
479479
},
480-
"pluginVersion": "6.6.0-pre",
480+
"pluginVersion": "6.5.0-pre",
481481
"targets": [
482482
{
483483
"alias": "Inside",

devenv/dev-dashboards/panel-stat/panel-stat-tests.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"show": true
7070
}
7171
},
72-
"pluginVersion": "6.6.0-pre",
72+
"pluginVersion": "6.5.0-pre",
7373
"targets": [
7474
{
7575
"alias": "A longer title",
@@ -160,7 +160,7 @@
160160
"show": true
161161
}
162162
},
163-
"pluginVersion": "6.6.0-pre",
163+
"pluginVersion": "6.5.0-pre",
164164
"targets": [
165165
{
166166
"alias": "AB",
@@ -251,7 +251,7 @@
251251
"show": true
252252
}
253253
},
254-
"pluginVersion": "6.6.0-pre",
254+
"pluginVersion": "6.5.0-pre",
255255
"targets": [
256256
{
257257
"refId": "A",
@@ -336,7 +336,7 @@
336336
"show": true
337337
}
338338
},
339-
"pluginVersion": "6.6.0-pre",
339+
"pluginVersion": "6.5.0-pre",
340340
"targets": [
341341
{
342342
"refId": "A",
@@ -424,7 +424,7 @@
424424
"show": true
425425
}
426426
},
427-
"pluginVersion": "6.6.0-pre",
427+
"pluginVersion": "6.5.0-pre",
428428
"targets": [
429429
{
430430
"refId": "A",
@@ -512,7 +512,7 @@
512512
"show": true
513513
}
514514
},
515-
"pluginVersion": "6.6.0-pre",
515+
"pluginVersion": "6.5.0-pre",
516516
"targets": [
517517
{
518518
"refId": "A",

packages/grafana-data/src/field/displayProcessor.test.ts

+55-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
import { getDisplayProcessor, getColorFromThreshold } from './displayProcessor';
1+
import { getDisplayProcessor } from './displayProcessor';
22
import { DisplayProcessor, DisplayValue } from '../types/displayValue';
33
import { ValueMapping, MappingType } from '../types/valueMapping';
4-
import { FieldType } from '../types';
4+
import { FieldType, Threshold, GrafanaTheme, Field, FieldConfig, ThresholdsMode } from '../types';
5+
import { getScaleCalculator, sortThresholds } from './scale';
6+
import { ArrayVector } from '../vector';
7+
import { validateFieldConfig } from './fieldOverrides';
8+
9+
function getDisplayProcessorFromConfig(config: FieldConfig) {
10+
return getDisplayProcessor({
11+
field: {
12+
config,
13+
type: FieldType.number,
14+
},
15+
});
16+
}
17+
18+
function getColorFromThreshold(value: number, steps: Threshold[], theme?: GrafanaTheme): string {
19+
const field: Field = {
20+
name: 'test',
21+
config: { thresholds: { mode: ThresholdsMode.Absolute, steps: sortThresholds(steps) } },
22+
type: FieldType.number,
23+
values: new ArrayVector([]),
24+
};
25+
validateFieldConfig(field.config!);
26+
const calc = getScaleCalculator(field, theme);
27+
return calc(value).color!;
28+
}
529

630
function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) {
731
processors.forEach(processor => {
@@ -20,10 +44,10 @@ describe('Process simple display values', () => {
2044
getDisplayProcessor(),
2145

2246
// Add a simple option that is not used (uses a different base class)
23-
getDisplayProcessor({ config: { min: 0, max: 100 } }),
47+
getDisplayProcessorFromConfig({ min: 0, max: 100 }),
2448

2549
// Add a simple option that is not used (uses a different base class)
26-
getDisplayProcessor({ config: { unit: 'locale' } }),
50+
getDisplayProcessorFromConfig({ unit: 'locale' }),
2751
];
2852

2953
it('support null', () => {
@@ -108,7 +132,7 @@ describe('Format value', () => {
108132
it('should return if value isNaN', () => {
109133
const valueMappings: ValueMapping[] = [];
110134
const value = 'N/A';
111-
const instance = getDisplayProcessor({ config: { mappings: valueMappings } });
135+
const instance = getDisplayProcessorFromConfig({ mappings: valueMappings });
112136

113137
const result = instance(value);
114138

@@ -119,7 +143,7 @@ describe('Format value', () => {
119143
const valueMappings: ValueMapping[] = [];
120144
const value = '6';
121145

122-
const instance = getDisplayProcessor({ config: { decimals: 1, mappings: valueMappings } });
146+
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
123147

124148
const result = instance(value);
125149

@@ -132,7 +156,7 @@ describe('Format value', () => {
132156
{ id: 1, operator: '', text: '1-9', type: MappingType.RangeToText, from: '1', to: '9' },
133157
];
134158
const value = '10';
135-
const instance = getDisplayProcessor({ config: { decimals: 1, mappings: valueMappings } });
159+
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
136160

137161
const result = instance(value);
138162

@@ -141,20 +165,20 @@ describe('Format value', () => {
141165

142166
it('should set auto decimals, 1 significant', () => {
143167
const value = 3.23;
144-
const instance = getDisplayProcessor({ config: { decimals: null } });
168+
const instance = getDisplayProcessorFromConfig({ decimals: null });
145169
expect(instance(value).text).toEqual('3.2');
146170
});
147171

148172
it('should set auto decimals, 2 significant', () => {
149173
const value = 0.0245;
150-
const instance = getDisplayProcessor({ config: { decimals: null } });
174+
const instance = getDisplayProcessorFromConfig({ decimals: null });
151175

152176
expect(instance(value).text).toEqual('0.025');
153177
});
154178

155179
it('should use override decimals', () => {
156180
const value = 100030303;
157-
const instance = getDisplayProcessor({ config: { decimals: 2, unit: 'bytes' } });
181+
const instance = getDisplayProcessorFromConfig({ decimals: 2, unit: 'bytes' });
158182
const disp = instance(value);
159183
expect(disp.text).toEqual('95.40');
160184
expect(disp.suffix).toEqual(' MiB');
@@ -166,7 +190,7 @@ describe('Format value', () => {
166190
{ id: 1, operator: '', text: 'elva', type: MappingType.ValueToText, value: '11' },
167191
];
168192
const value = '11';
169-
const instance = getDisplayProcessor({ config: { decimals: 1, mappings: valueMappings } });
193+
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
170194

171195
expect(instance(value).text).toEqual('1-20');
172196
});
@@ -176,7 +200,7 @@ describe('Format value', () => {
176200
{ id: 1, operator: '', text: '', type: MappingType.ValueToText, value: '1' },
177201
];
178202
const value = '1';
179-
const instance = getDisplayProcessor({ config: { decimals: 1, mappings: valueMappings } });
203+
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
180204

181205
expect(instance(value).text).toEqual('');
182206
expect(instance(value).numeric).toEqual(1);
@@ -188,31 +212,31 @@ describe('Format value', () => {
188212

189213
it('with value 1000 and unit short', () => {
190214
const value = 1000;
191-
const instance = getDisplayProcessor({ config: { decimals: null, unit: 'short' } });
215+
const instance = getDisplayProcessorFromConfig({ decimals: null, unit: 'short' });
192216
const disp = instance(value);
193217
expect(disp.text).toEqual('1.000');
194218
expect(disp.suffix).toEqual(' K');
195219
});
196220

197221
it('with value 1200 and unit short', () => {
198222
const value = 1200;
199-
const instance = getDisplayProcessor({ config: { decimals: null, unit: 'short' } });
223+
const instance = getDisplayProcessorFromConfig({ decimals: null, unit: 'short' });
200224
const disp = instance(value);
201225
expect(disp.text).toEqual('1.200');
202226
expect(disp.suffix).toEqual(' K');
203227
});
204228

205229
it('with value 1250 and unit short', () => {
206230
const value = 1250;
207-
const instance = getDisplayProcessor({ config: { decimals: null, unit: 'short' } });
231+
const instance = getDisplayProcessorFromConfig({ decimals: null, unit: 'short' });
208232
const disp = instance(value);
209233
expect(disp.text).toEqual('1.250');
210234
expect(disp.suffix).toEqual(' K');
211235
});
212236

213237
it('with value 10000000 and unit short', () => {
214238
const value = 1000000;
215-
const instance = getDisplayProcessor({ config: { decimals: null, unit: 'short' } });
239+
const instance = getDisplayProcessorFromConfig({ decimals: null, unit: 'short' });
216240
const disp = instance(value);
217241
expect(disp.text).toEqual('1.000');
218242
expect(disp.suffix).toEqual(' Mil');
@@ -222,32 +246,38 @@ describe('Format value', () => {
222246
describe('Date display options', () => {
223247
it('should format UTC dates', () => {
224248
const processor = getDisplayProcessor({
225-
type: FieldType.time,
226249
isUtc: true,
227-
config: {
228-
unit: 'xyz', // ignore non-date formats
250+
field: {
251+
type: FieldType.time,
252+
config: {
253+
unit: 'xyz', // ignore non-date formats
254+
},
229255
},
230256
});
231257
expect(processor(0).text).toEqual('1970-01-01 00:00:00');
232258
});
233259

234260
it('should pick configured time format', () => {
235261
const processor = getDisplayProcessor({
236-
type: FieldType.time,
237262
isUtc: true,
238-
config: {
239-
unit: 'dateTimeAsUS', // A configurable date format
263+
field: {
264+
type: FieldType.time,
265+
config: {
266+
unit: 'dateTimeAsUS', // ignore non-date formats
267+
},
240268
},
241269
});
242270
expect(processor(0).text).toEqual('01/01/1970 12:00:00 am');
243271
});
244272

245273
it('respect the configured date format', () => {
246274
const processor = getDisplayProcessor({
247-
type: FieldType.time,
248275
isUtc: true,
249-
config: {
250-
unit: 'time:YYYY',
276+
field: {
277+
type: FieldType.time,
278+
config: {
279+
unit: 'time:YYYY', // ignore non-date formats
280+
},
251281
},
252282
});
253283
expect(processor(0).text).toEqual('1970');

0 commit comments

Comments
 (0)