1
- import { getDisplayProcessor , getColorFromThreshold } from './displayProcessor' ;
1
+ import { getDisplayProcessor } from './displayProcessor' ;
2
2
import { DisplayProcessor , DisplayValue } from '../types/displayValue' ;
3
3
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
+ }
5
29
6
30
function assertSame ( input : any , processors : DisplayProcessor [ ] , match : DisplayValue ) {
7
31
processors . forEach ( processor => {
@@ -20,10 +44,10 @@ describe('Process simple display values', () => {
20
44
getDisplayProcessor ( ) ,
21
45
22
46
// 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 } ) ,
24
48
25
49
// Add a simple option that is not used (uses a different base class)
26
- getDisplayProcessor ( { config : { unit : 'locale' } } ) ,
50
+ getDisplayProcessorFromConfig ( { unit : 'locale' } ) ,
27
51
] ;
28
52
29
53
it ( 'support null' , ( ) => {
@@ -108,7 +132,7 @@ describe('Format value', () => {
108
132
it ( 'should return if value isNaN' , ( ) => {
109
133
const valueMappings : ValueMapping [ ] = [ ] ;
110
134
const value = 'N/A' ;
111
- const instance = getDisplayProcessor ( { config : { mappings : valueMappings } } ) ;
135
+ const instance = getDisplayProcessorFromConfig ( { mappings : valueMappings } ) ;
112
136
113
137
const result = instance ( value ) ;
114
138
@@ -119,7 +143,7 @@ describe('Format value', () => {
119
143
const valueMappings : ValueMapping [ ] = [ ] ;
120
144
const value = '6' ;
121
145
122
- const instance = getDisplayProcessor ( { config : { decimals : 1 , mappings : valueMappings } } ) ;
146
+ const instance = getDisplayProcessorFromConfig ( { decimals : 1 , mappings : valueMappings } ) ;
123
147
124
148
const result = instance ( value ) ;
125
149
@@ -132,7 +156,7 @@ describe('Format value', () => {
132
156
{ id : 1 , operator : '' , text : '1-9' , type : MappingType . RangeToText , from : '1' , to : '9' } ,
133
157
] ;
134
158
const value = '10' ;
135
- const instance = getDisplayProcessor ( { config : { decimals : 1 , mappings : valueMappings } } ) ;
159
+ const instance = getDisplayProcessorFromConfig ( { decimals : 1 , mappings : valueMappings } ) ;
136
160
137
161
const result = instance ( value ) ;
138
162
@@ -141,20 +165,20 @@ describe('Format value', () => {
141
165
142
166
it ( 'should set auto decimals, 1 significant' , ( ) => {
143
167
const value = 3.23 ;
144
- const instance = getDisplayProcessor ( { config : { decimals : null } } ) ;
168
+ const instance = getDisplayProcessorFromConfig ( { decimals : null } ) ;
145
169
expect ( instance ( value ) . text ) . toEqual ( '3.2' ) ;
146
170
} ) ;
147
171
148
172
it ( 'should set auto decimals, 2 significant' , ( ) => {
149
173
const value = 0.0245 ;
150
- const instance = getDisplayProcessor ( { config : { decimals : null } } ) ;
174
+ const instance = getDisplayProcessorFromConfig ( { decimals : null } ) ;
151
175
152
176
expect ( instance ( value ) . text ) . toEqual ( '0.025' ) ;
153
177
} ) ;
154
178
155
179
it ( 'should use override decimals' , ( ) => {
156
180
const value = 100030303 ;
157
- const instance = getDisplayProcessor ( { config : { decimals : 2 , unit : 'bytes' } } ) ;
181
+ const instance = getDisplayProcessorFromConfig ( { decimals : 2 , unit : 'bytes' } ) ;
158
182
const disp = instance ( value ) ;
159
183
expect ( disp . text ) . toEqual ( '95.40' ) ;
160
184
expect ( disp . suffix ) . toEqual ( ' MiB' ) ;
@@ -166,7 +190,7 @@ describe('Format value', () => {
166
190
{ id : 1 , operator : '' , text : 'elva' , type : MappingType . ValueToText , value : '11' } ,
167
191
] ;
168
192
const value = '11' ;
169
- const instance = getDisplayProcessor ( { config : { decimals : 1 , mappings : valueMappings } } ) ;
193
+ const instance = getDisplayProcessorFromConfig ( { decimals : 1 , mappings : valueMappings } ) ;
170
194
171
195
expect ( instance ( value ) . text ) . toEqual ( '1-20' ) ;
172
196
} ) ;
@@ -176,7 +200,7 @@ describe('Format value', () => {
176
200
{ id : 1 , operator : '' , text : '' , type : MappingType . ValueToText , value : '1' } ,
177
201
] ;
178
202
const value = '1' ;
179
- const instance = getDisplayProcessor ( { config : { decimals : 1 , mappings : valueMappings } } ) ;
203
+ const instance = getDisplayProcessorFromConfig ( { decimals : 1 , mappings : valueMappings } ) ;
180
204
181
205
expect ( instance ( value ) . text ) . toEqual ( '' ) ;
182
206
expect ( instance ( value ) . numeric ) . toEqual ( 1 ) ;
@@ -188,31 +212,31 @@ describe('Format value', () => {
188
212
189
213
it ( 'with value 1000 and unit short' , ( ) => {
190
214
const value = 1000 ;
191
- const instance = getDisplayProcessor ( { config : { decimals : null , unit : 'short' } } ) ;
215
+ const instance = getDisplayProcessorFromConfig ( { decimals : null , unit : 'short' } ) ;
192
216
const disp = instance ( value ) ;
193
217
expect ( disp . text ) . toEqual ( '1.000' ) ;
194
218
expect ( disp . suffix ) . toEqual ( ' K' ) ;
195
219
} ) ;
196
220
197
221
it ( 'with value 1200 and unit short' , ( ) => {
198
222
const value = 1200 ;
199
- const instance = getDisplayProcessor ( { config : { decimals : null , unit : 'short' } } ) ;
223
+ const instance = getDisplayProcessorFromConfig ( { decimals : null , unit : 'short' } ) ;
200
224
const disp = instance ( value ) ;
201
225
expect ( disp . text ) . toEqual ( '1.200' ) ;
202
226
expect ( disp . suffix ) . toEqual ( ' K' ) ;
203
227
} ) ;
204
228
205
229
it ( 'with value 1250 and unit short' , ( ) => {
206
230
const value = 1250 ;
207
- const instance = getDisplayProcessor ( { config : { decimals : null , unit : 'short' } } ) ;
231
+ const instance = getDisplayProcessorFromConfig ( { decimals : null , unit : 'short' } ) ;
208
232
const disp = instance ( value ) ;
209
233
expect ( disp . text ) . toEqual ( '1.250' ) ;
210
234
expect ( disp . suffix ) . toEqual ( ' K' ) ;
211
235
} ) ;
212
236
213
237
it ( 'with value 10000000 and unit short' , ( ) => {
214
238
const value = 1000000 ;
215
- const instance = getDisplayProcessor ( { config : { decimals : null , unit : 'short' } } ) ;
239
+ const instance = getDisplayProcessorFromConfig ( { decimals : null , unit : 'short' } ) ;
216
240
const disp = instance ( value ) ;
217
241
expect ( disp . text ) . toEqual ( '1.000' ) ;
218
242
expect ( disp . suffix ) . toEqual ( ' Mil' ) ;
@@ -222,32 +246,38 @@ describe('Format value', () => {
222
246
describe ( 'Date display options' , ( ) => {
223
247
it ( 'should format UTC dates' , ( ) => {
224
248
const processor = getDisplayProcessor ( {
225
- type : FieldType . time ,
226
249
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
+ } ,
229
255
} ,
230
256
} ) ;
231
257
expect ( processor ( 0 ) . text ) . toEqual ( '1970-01-01 00:00:00' ) ;
232
258
} ) ;
233
259
234
260
it ( 'should pick configured time format' , ( ) => {
235
261
const processor = getDisplayProcessor ( {
236
- type : FieldType . time ,
237
262
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
+ } ,
240
268
} ,
241
269
} ) ;
242
270
expect ( processor ( 0 ) . text ) . toEqual ( '01/01/1970 12:00:00 am' ) ;
243
271
} ) ;
244
272
245
273
it ( 'respect the configured date format' , ( ) => {
246
274
const processor = getDisplayProcessor ( {
247
- type : FieldType . time ,
248
275
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
+ } ,
251
281
} ,
252
282
} ) ;
253
283
expect ( processor ( 0 ) . text ) . toEqual ( '1970' ) ;
0 commit comments