-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathwindow.ts
More file actions
77 lines (67 loc) · 2.28 KB
/
window.ts
File metadata and controls
77 lines (67 loc) · 2.28 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
/*
* 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
*/
// Here we add some handy stuff to window for use from the browser JS console.
// requests will use the session cookie, same as normal API calls
import { ALL_ISH } from '~/util/consts'
import { type ApiResult } from './__generated__/http-client'
import { api } from './client'
function zip<T, U>(arr1: T[], arr2: U[]): [T, U][] {
const length = Math.min(arr1.length, arr2.length)
const result: [T, U][] = []
for (let i = 0; i < length; i++) {
result.push([arr1[i], arr2[i]])
}
return result
}
function handleResult<T>(result: ApiResult<T>): T {
if (result.type !== 'success') {
if (result.type === 'error') console.error(result.data.message)
throw result
}
return result.data
}
function logHeading(s: string) {
console.info(`%c${s}`, 'font-size: 16px; font-weight: bold;')
}
if (typeof window !== 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(window as any).oxide = api
// @ts-expect-error
window.oxql = {
query: async (q: string) => {
const result = await api.systemTimeseriesQuery({ body: { query: q } })
const data = handleResult(result).tables
logHeading(data.length + ' timeseries returned')
for (const table of data) {
for (const ts of table.timeseries) {
const fields = Object.entries(ts.fields)
.map(([k, v]) => `${k}: ${v.value}`)
.join(', ')
logHeading(`Data for { ${fields} }`)
console.table(
zip(ts.points.timestamps, ts.points.values[0].values.values as number[])
)
}
}
return data
},
schemas: async (search?: string) => {
const result = await api.systemTimeseriesSchemaList({
query: { limit: ALL_ISH },
})
const data = handleResult(result)
let filtered = data.items
if (search) {
filtered = filtered.filter((s) => s.timeseriesName.includes(search))
}
// note we both print as table _and_ return in case the caller wants the data
console.table(filtered)
return filtered
},
}
}