/
utils.test.ts
359 lines (336 loc) · 12.1 KB
/
utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import moment from 'moment';
import {
decodePanelOptionsFromQueryString,
encodePanelOptionsToQueryString,
escapeHTML,
formatDuration,
formatTime,
formatRelative,
humanizeDuration,
metricToSeriesName,
now,
parseDuration,
parseOption,
parseTime,
toQueryString,
} from '.';
import { PanelType } from '../pages/graph/Panel';
describe('Utils', () => {
describe('escapeHTML', (): void => {
it('escapes html sequences', () => {
expect(escapeHTML(`<strong>'example'&"another/example"</strong>`)).toEqual(
'<strong>'example'&"another/example"</strong>'
);
});
});
describe('metricToSeriesName', () => {
it('returns "{}" if labels is empty', () => {
const labels = {};
expect(metricToSeriesName(labels)).toEqual('{}');
});
it('returns "metric_name{}" if labels only contains __name__', () => {
const labels = { __name__: 'metric_name' };
expect(metricToSeriesName(labels)).toEqual('metric_name{}');
});
it('returns "{label1=value_1, ..., labeln=value_n} if there are many labels and no name', () => {
const labels = { label1: 'value_1', label2: 'value_2', label3: 'value_3' };
expect(metricToSeriesName(labels)).toEqual('{label1="value_1", label2="value_2", label3="value_3"}');
});
it('returns "metric_name{label1=value_1, ... ,labeln=value_n}" if there are many labels and a name', () => {
const labels = {
__name__: 'metric_name',
label1: 'value_1',
label2: 'value_2',
label3: 'value_3',
};
expect(metricToSeriesName(labels)).toEqual('metric_name{label1="value_1", label2="value_2", label3="value_3"}');
});
});
describe('Time format', () => {
describe('formatTime', () => {
it('returns a time string representing the time in seconds', () => {
expect(formatTime(1572049380000)).toEqual('2019-10-26 00:23:00');
expect(formatTime(0)).toEqual('1970-01-01 00:00:00');
});
});
describe('parseTime', () => {
it('returns a time string representing the time in seconds', () => {
expect(parseTime('2019-10-26 00:23')).toEqual(1572049380000);
expect(parseTime('1970-01-01 00:00')).toEqual(0);
expect(parseTime('0001-01-01T00:00:00Z')).toEqual(-62135596800000);
});
});
describe('parseDuration and formatDuration', () => {
describe('should parse and format durations correctly', () => {
const tests: { input: string; output: number; expectedString?: string }[] = [
{
input: '0',
output: 0,
expectedString: '0s',
},
{
input: '0w',
output: 0,
expectedString: '0s',
},
{
input: '0s',
output: 0,
},
{
input: '324ms',
output: 324,
},
{
input: '3s',
output: 3 * 1000,
},
{
input: '5m',
output: 5 * 60 * 1000,
},
{
input: '1h',
output: 60 * 60 * 1000,
},
{
input: '4d',
output: 4 * 24 * 60 * 60 * 1000,
},
{
input: '4d1h',
output: 4 * 24 * 60 * 60 * 1000 + 1 * 60 * 60 * 1000,
},
{
input: '14d',
output: 14 * 24 * 60 * 60 * 1000,
expectedString: '2w',
},
{
input: '3w',
output: 3 * 7 * 24 * 60 * 60 * 1000,
},
{
input: '3w2d1h',
output: 3 * 7 * 24 * 60 * 60 * 1000 + 2 * 24 * 60 * 60 * 1000 + 60 * 60 * 1000,
expectedString: '23d1h',
},
{
input: '1y2w3d4h5m6s7ms',
output:
1 * 365 * 24 * 60 * 60 * 1000 +
2 * 7 * 24 * 60 * 60 * 1000 +
3 * 24 * 60 * 60 * 1000 +
4 * 60 * 60 * 1000 +
5 * 60 * 1000 +
6 * 1000 +
7,
expectedString: '382d4h5m6s7ms',
},
];
tests.forEach((t) => {
it(t.input, () => {
const d = parseDuration(t.input);
expect(d).toEqual(t.output);
expect(formatDuration(d!)).toEqual(t.expectedString || t.input);
});
});
});
describe('should fail to parse invalid durations', () => {
const tests = ['1', '1y1m1d', '-1w', '1.5d', 'd', ''];
tests.forEach((t) => {
it(t, () => {
expect(parseDuration(t)).toBe(null);
});
});
});
});
describe('humanizeDuration', () => {
it('humanizes zero', () => {
expect(humanizeDuration(0)).toEqual('0s');
});
it('humanizes milliseconds', () => {
expect(humanizeDuration(1.234567)).toEqual('1.235ms');
expect(humanizeDuration(12.34567)).toEqual('12.346ms');
expect(humanizeDuration(123.45678)).toEqual('123.457ms');
expect(humanizeDuration(123)).toEqual('123.000ms');
});
it('humanizes seconds', () => {
expect(humanizeDuration(12340)).toEqual('12.340s');
});
it('humanizes minutes', () => {
expect(humanizeDuration(1234567)).toEqual('20m 34s');
});
it('humanizes hours', () => {
expect(humanizeDuration(12345678)).toEqual('3h 25m 45s');
});
it('humanizes days', () => {
expect(humanizeDuration(123456789)).toEqual('1d 10h 17m 36s');
expect(humanizeDuration(123456789000)).toEqual('1428d 21h 33m 9s');
});
it('takes sign into account', () => {
expect(humanizeDuration(-123456789000)).toEqual('-1428d 21h 33m 9s');
});
});
describe('formatRelative', () => {
it('renders never for pre-beginning-of-time strings', () => {
expect(formatRelative('0001-01-01T00:00:00Z', now())).toEqual('Never');
});
it('renders a humanized duration for sane durations', () => {
expect(formatRelative('2019-11-04T09:15:29.578701-07:00', parseTime('2019-11-04T09:15:35.8701-07:00'))).toEqual(
'6.292s'
);
expect(formatRelative('2019-11-04T09:15:35.8701-07:00', parseTime('2019-11-04T09:15:29.578701-07:00'))).toEqual(
'-6.292s'
);
});
});
});
describe('URL Params', () => {
const stores: any = [
{
name: 'thanos_sidecar_one:10901',
},
];
const panels: any = [
{
key: '0',
options: {
endTime: 1572046620000,
expr: 'rate(node_cpu_seconds_total{mode="system"}[1m])',
range: 60 * 60 * 1000,
resolution: null,
stacked: false,
maxSourceResolution: 'raw',
useDeduplication: true,
usePartialResponse: false,
type: PanelType.Graph,
storeMatches: [],
engine: 'prometheus',
analyze: false,
},
},
{
key: '1',
options: {
endTime: null,
expr: 'node_filesystem_avail_bytes',
range: 60 * 60 * 1000,
resolution: null,
stacked: false,
maxSourceResolution: 'auto',
useDeduplication: false,
usePartialResponse: true,
type: PanelType.Table,
storeMatches: stores,
engine: 'prometheus',
analyze: false,
},
},
];
const query =
'?g0.expr=rate(node_cpu_seconds_total%7Bmode%3D%22system%22%7D%5B1m%5D)&g0.tab=0&g0.stacked=0&g0.range_input=1h&g0.max_source_resolution=raw&g0.deduplicate=1&g0.partial_response=0&g0.store_matches=%5B%5D&g0.engine=prometheus&g0.analyze=0&g0.end_input=2019-10-25%2023%3A37%3A00&g0.moment_input=2019-10-25%2023%3A37%3A00&g1.expr=node_filesystem_avail_bytes&g1.tab=1&g1.stacked=0&g1.range_input=1h&g1.max_source_resolution=auto&g1.deduplicate=0&g1.partial_response=1&g1.store_matches=%5B%7B%22name%22%3A%22thanos_sidecar_one%3A10901%22%7D%5D&g1.engine=prometheus&g1.analyze=0';
describe('decodePanelOptionsFromQueryString', () => {
it('returns [] when query is empty', () => {
expect(decodePanelOptionsFromQueryString('')).toEqual([]);
});
it('returns and array of parsed params when query string is non-empty', () => {
expect(decodePanelOptionsFromQueryString(query)).toMatchObject(panels);
});
});
describe('parseOption', () => {
it('should return empty object for invalid param', () => {
expect(parseOption('invalid_prop=foo')).toEqual({});
});
it('should parse expr param', () => {
expect(parseOption('expr=foo')).toEqual({ expr: 'foo' });
});
it('should parse stacked', () => {
expect(parseOption('stacked=1')).toEqual({ stacked: true });
});
it('should parse end_input', () => {
expect(parseOption('end_input=2019-10-25%2023%3A37')).toEqual({ endTime: moment.utc('2019-10-25 23:37').valueOf() });
});
it('should parse moment_input', () => {
expect(parseOption('moment_input=2019-10-25%2023%3A37')).toEqual({
endTime: moment.utc('2019-10-25 23:37').valueOf(),
});
});
it('should parse max source res', () => {
expect(parseOption('max_source_resolution=auto')).toEqual({ maxSourceResolution: 'auto' });
});
it('should parse use deduplicate', () => {
expect(parseOption('deduplicate=1')).toEqual({ useDeduplication: true });
});
it('should parse partial_response', () => {
expect(parseOption('partial_response=1')).toEqual({ usePartialResponse: true });
});
it('it should parse store_matches', () => {
expect(parseOption('store_matches=%5B%7B%22name%22%3A%22thanos_sidecar_one%3A10901%22%7D%5D')).toEqual({
storeMatches: stores,
});
});
describe('step_input', () => {
it('should return step_input parsed if > 0', () => {
expect(parseOption('step_input=2')).toEqual({ resolution: 2 });
});
it('should return empty object if step is equal 0', () => {
expect(parseOption('step_input=0')).toEqual({});
});
});
describe('range_input', () => {
it('should return range parsed if its not null', () => {
expect(parseOption('range_input=2h')).toEqual({ range: 2 * 60 * 60 * 1000 });
});
it('should return empty object for invalid value', () => {
expect(parseOption('range_input=h')).toEqual({});
});
});
describe('Parse type param', () => {
it('should return panel type "graph" if tab=0', () => {
expect(parseOption('tab=0')).toEqual({ type: PanelType.Graph });
});
it('should return panel type "table" if tab=1', () => {
expect(parseOption('tab=1')).toEqual({ type: PanelType.Table });
});
});
});
describe('toQueryString', () => {
it('should generate query string from panel options', () => {
expect(
toQueryString({
id: 'asdf',
key: '0',
options: {
expr: 'foo',
type: PanelType.Graph,
stacked: true,
range: 0,
endTime: null,
resolution: 1,
maxSourceResolution: 'raw',
useDeduplication: true,
forceTracing: false,
usePartialResponse: false,
storeMatches: [],
engine: 'prometheus',
analyze: false,
disableAnalyzeCheckbox: false,
tenant: 'default-tenant',
},
})
).toEqual(
'g0.expr=foo&g0.tab=0&g0.stacked=1&g0.range_input=0s&g0.max_source_resolution=raw&g0.deduplicate=1&g0.partial_response=0&g0.store_matches=%5B%5D&g0.engine=prometheus&g0.analyze=0&g0.step_input=1'
);
});
});
describe('encodePanelOptionsToQueryString', () => {
it('returns ? when panels is empty', () => {
expect(encodePanelOptionsToQueryString([])).toEqual('?');
});
it('returns an encoded query string otherwise', () => {
expect(encodePanelOptionsToQueryString(panels)).toEqual(query);
});
});
});
});