-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathutil.spec.ts
More file actions
384 lines (358 loc) · 10.9 KB
/
util.spec.ts
File metadata and controls
384 lines (358 loc) · 10.9 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import * as R from 'remeda'
import { describe, expect, test } from 'vitest'
import type { OxqlQueryResult } from '~/api'
import {
composeOxqlData,
getLargestValue,
getMaxExponent,
getMeanWithinSeconds,
getTimePropsForOxqlQuery,
getUtilizationChartProps,
oxqlTimestamp,
sumValues,
yAxisLabelForCountChart,
} from './util'
test('oxqlTimestamp', () => {
const date1 = new Date('2025-02-11T00:00:01.234Z')
expect(oxqlTimestamp(date1)).toEqual('2025-02-11T00:00:01.000')
const datePST = new Date('2025-02-11T00:00:00-08:00')
expect(oxqlTimestamp(datePST)).toEqual('2025-02-11T08:00:00.000')
})
describe('getMeanWithinSeconds', () => {
const start = new Date('2025-02-11T00:00:00Z')
test('calculates the mean window for a 10-minute range', () => {
const end = new Date('2025-02-11T00:10:00Z') // 10 minutes later
expect(getMeanWithinSeconds(start, end)).toBe(10)
})
test('calculates the mean window for a 1-hour range', () => {
const end = new Date('2025-02-11T01:00:00Z') // 60 minutes later
expect(getMeanWithinSeconds(start, end)).toBe(60)
})
test('calculates the mean window for a 24-hour range', () => {
const end = new Date('2025-02-12T00:00:00Z') // 24 hours later
expect(getMeanWithinSeconds(start, end)).toBe(1440)
})
test('calculates the mean window for a 1-week range', () => {
const end = new Date('2025-02-18T00:00:00Z') // 1 week later
expect(getMeanWithinSeconds(start, end)).toBe(10080)
})
test('calculates the mean window for a 10-minute range, but with only 5 datapoints', () => {
const end = new Date('2025-02-11T00:10:00Z') // 10 minutes later
const datapoints = 5
expect(getMeanWithinSeconds(start, end, datapoints)).toBe(120)
})
test('calculates the mean window for a 2-hour range, but with only 5 datapoints', () => {
const end = new Date('2025-02-11T02:00:00Z') // 120 minutes later
const datapoints = 5
expect(getMeanWithinSeconds(start, end, datapoints)).toBe(1440)
})
test('calculates the mean window for a 1-month range', () => {
const end = new Date('2025-03-11T00:00:00Z') // 28 days later
expect(getMeanWithinSeconds(start, end)).toBe(40320)
})
test('calculates the mean window for a 1-month range, with only 20 datapoints', () => {
const end = new Date('2025-03-11T00:00:00Z') // 28 days later
expect(getMeanWithinSeconds(start, end, 20)).toBe(120960)
})
})
test('getTimePropsForOxqlQuery', () => {
const startTime = new Date('2025-02-21T01:00:00Z')
const endTime = new Date('2025-02-21T02:00:00Z')
const { meanWithinSeconds, adjustedStart } = getTimePropsForOxqlQuery(startTime, endTime)
expect(meanWithinSeconds).toEqual(60)
expect(adjustedStart).toEqual(new Date('2025-02-21T00:58:00.000Z'))
})
test('getMaxExponent', () => {
expect(getMaxExponent(5, 1000)).toEqual(0)
expect(getMaxExponent(1000, 1000)).toEqual(1)
expect(getMaxExponent(1001, 1000)).toEqual(1)
expect(getMaxExponent(10 ** 6, 1000)).toEqual(2)
expect(getMaxExponent(10 ** 6 + 1, 1000)).toEqual(2)
expect(getMaxExponent(10 ** 9, 1000)).toEqual(3)
expect(getMaxExponent(10 ** 9 + 1, 1000)).toEqual(3)
// Bytes
expect(getMaxExponent(5, 1024)).toEqual(0)
// KiBs
expect(getMaxExponent(1024, 1024)).toEqual(1)
expect(getMaxExponent(1025, 1024)).toEqual(1)
expect(getMaxExponent(2 ** 20 - 1, 1024)).toEqual(1)
// MiBs
expect(getMaxExponent(2 ** 20, 1024)).toEqual(2)
expect(getMaxExponent(2 ** 20 + 1, 1024)).toEqual(2)
expect(getMaxExponent(2 ** 30 - 1, 1024)).toEqual(2)
// GiBs
expect(getMaxExponent(2 ** 30, 1024)).toEqual(3)
expect(getMaxExponent(2 ** 30 + 1, 1024)).toEqual(3)
expect(getMaxExponent(0, 1000)).toEqual(0)
})
test('yAxisLabelForCountChart', () => {
expect(yAxisLabelForCountChart(5, 0)).toEqual('5')
expect(yAxisLabelForCountChart(1000, 1)).toEqual('1k')
expect(yAxisLabelForCountChart(1001, 1)).toEqual('1k')
expect(yAxisLabelForCountChart(10 ** 6, 2)).toEqual('1M')
expect(yAxisLabelForCountChart(10 ** 6 + 1, 2)).toEqual('1M')
expect(yAxisLabelForCountChart(10 ** 9, 3)).toEqual('1B')
expect(yAxisLabelForCountChart(10 ** 9 + 1, 3)).toEqual('1B')
expect(yAxisLabelForCountChart(10 ** 12, 4)).toEqual('1T')
expect(yAxisLabelForCountChart(10 ** 12 + 1, 4)).toEqual('1T')
})
test('getLargestValue', () => {
const sampleData = [
{ timestamp: 1739232000000, value: 5 },
{ timestamp: 1739232060000, value: 10 },
{ timestamp: 1739232120000, value: 15 },
]
expect(getLargestValue(sampleData)).toEqual(15)
expect(getLargestValue([])).toEqual(0)
})
// Just including four datapoints for this test
const utilizationQueryResult1: OxqlQueryResult = {
tables: [
{
name: 'virtual_machine:vcpu_usage',
timeseries: [
{
fields: {
vcpuId: {
type: 'u32',
value: 0,
},
},
points: {
timestamps: [
new Date('2025-02-21T19:28:43Z'),
new Date('2025-02-21T19:29:43Z'),
new Date('2025-02-21T19:30:43Z'),
new Date('2025-02-21T19:31:43Z'),
],
values: [
{
values: {
type: 'double',
values: [4991154550.953981, 5002306111.529594, 5005747970.58788, null],
},
metricType: 'gauge',
},
],
},
},
],
},
],
}
const timeseries1 = utilizationQueryResult1.tables[0].timeseries[0]
const utilizationQueryResult2: OxqlQueryResult = {
tables: [
{
name: 'virtual_machine:vcpu_usage',
timeseries: [
{
fields: {
vcpuId: {
type: 'u32',
value: 0,
},
},
points: {
timestamps: [
new Date('2025-02-21T19:28:43Z'),
new Date('2025-02-21T19:29:43Z'),
new Date('2025-02-21T19:30:43Z'),
new Date('2025-02-21T19:31:43Z'),
new Date('2025-02-21T19:32:43Z'),
new Date('2025-02-21T19:33:43Z'),
new Date('2025-02-21T19:34:43Z'),
new Date('2025-02-21T19:35:43Z'),
],
values: [
{
values: {
type: 'double',
values: R.range(1, 9).map((value) => value * 1000000),
},
metricType: 'gauge',
},
],
},
},
{
fields: {
vcpuId: {
type: 'u32',
value: 1,
},
},
points: {
timestamps: [
new Date('2025-02-21T19:32:43Z'),
new Date('2025-02-21T19:33:43Z'),
new Date('2025-02-21T19:34:43Z'),
new Date('2025-02-21T19:35:43Z'),
],
values: [
{
values: {
type: 'double',
values: R.range(1, 5).map((value) => value * 1000000),
},
metricType: 'gauge',
},
],
},
},
],
},
],
}
test('sumValues', () => {
expect(sumValues([], 0)).toEqual([])
expect(sumValues([timeseries1], 4)).toEqual([
4991154550.953981,
5002306111.529594,
5005747970.58788,
null,
])
// we're just including this dataset twice to show that the numbers are getting added together
expect(sumValues([timeseries1, timeseries1], 4)).toEqual([
9982309101.907963,
10004612223.059189,
10011495941.17576,
null,
])
// and for good measure, we'll include it three times
expect(sumValues([timeseries1, timeseries1, timeseries1], 4)).toEqual([
14973463652.861944,
15006918334.588783,
15017243911.763641,
null,
])
})
// Note how this only has three values, where the original data had four,
// because we've intentionally removed the first one
const composedUtilizationData = {
chartData: [
{
timestamp: 1740166183000,
value: 5002306111.529594,
},
{
timestamp: 1740166243000,
value: 5005747970.58788,
},
{
timestamp: 1740166303000,
value: null,
},
],
valueCounts: [1, 1, 1, 0],
}
const composedUtilizationData2 = {
chartData: [
{
timestamp: 1740166183000,
value: 4000000,
},
{
timestamp: 1740166243000,
value: 6000000,
},
{
timestamp: 1740166303000,
value: 8000000,
},
{
timestamp: 1740166363000,
value: 5000000,
},
{
timestamp: 1740166423000,
value: 6000000,
},
{
timestamp: 1740166483000,
value: 7000000,
},
{
timestamp: 1740166543000,
value: 8000000,
},
],
valueCounts: [2, 2, 2, 2, 1, 1, 1, 1],
}
// As above, we've removed the first value from the original data
const utilizationChartData = [
{
timestamp: 1740166183000,
value: 100.04612223059189,
},
{
timestamp: 1740166243000,
value: 100.11495941175761,
},
{
timestamp: 1740166303000,
value: null,
},
]
// These are the exepcted values if the same data came back for a 4-vcpu instance
// As above, we've discarded the first value from the original data
const utilizationChartData4 = [
{
timestamp: 1740166183000,
value: 25.011530557647973,
},
{
timestamp: 1740166243000,
value: 25.028739852939403,
},
{
timestamp: 1740166303000,
value: null,
},
]
// These are the exepcted values if the vcpu count changed mid-query.
// As above, we've discarded the first value from the original data
const utilizationChartData5 = [
{
timestamp: 1740166183000,
value: 100.04612223059189,
},
{
timestamp: 1740166243000,
value: 25.028739852939403,
},
{
timestamp: 1740166303000,
value: null,
},
]
test('get utilization chart data and process it for chart display', () => {
const composedData = composeOxqlData(utilizationQueryResult1)
expect(composedData).toEqual(composedUtilizationData)
const composedData2 = composeOxqlData(utilizationQueryResult2)
expect(composedData2).toEqual(composedUtilizationData2)
const { data: chartData } = getUtilizationChartProps(
composedUtilizationData.chartData,
[1, 1, 1, 0]
)
expect(chartData).toEqual(utilizationChartData)
// Testing the same data, but for a 4-vcpu instance
const { data: chartData4 } = getUtilizationChartProps(
composedUtilizationData.chartData,
[4, 4, 4, 4]
)
expect(chartData4).toEqual(utilizationChartData4)
// Testing the same data, but where the cpu count changed mid-results
const { data: chartData5 } = getUtilizationChartProps(
composedUtilizationData.chartData,
[1, 4, 4, 4]
)
expect(chartData5).toEqual(utilizationChartData5)
})