Skip to content

Commit

Permalink
feat: add getDataFromGrafana func
Browse files Browse the repository at this point in the history
  • Loading branch information
zeruk committed May 23, 2023
1 parent 09cfe3b commit b27573e
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/__tests__/checkResults.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {expect, test, jest} from '@jest/globals'

// const callExecutables = jest.createMockFromModule(
// '../callExecutables'
// ) as typeof import('../callExecutables')
// callExecutables.callKubernetesPathAsync = async generator => {
// return generator('kubectl')
// }

jest.mock('../callExecutables', () => ({
__esModule: true, // this property makes it work
callKubernetesPathAsync: async (generator: (s: string) => string) => {
return generator('kubectl')
}
}))

import {callKubernetesPathAsync, prepareK8S} from '../callExecutables'
callKubernetesPathAsync(t => 'test')
import {getDataFromGrafana} from '../checkResults'

test('checkResults wget generation', async () => {
expect(
await getDataFromGrafana(new Date(1234000), new Date(5678000), [
{
expr: 'expr1',
key: 'key1',
refId: 'refId1',
reqId: 'req1',
interval: '1s'
},
{
expr: 'expr2',
key: 'key2',
refId: 'refId2',
reqId: 'req2',
interval: '',
format: 'time_series'
}
])
).toStrictEqual(
`kubectl run -q -i --image=busybox --rm grafana-result-peeker --restart=Never -- sh -c '` +
`wget -q -O- --header='\\''content-type: application/json'\\'' ` +
`--post-data='\\''{
"queries": [
{
"refId": "refId1",
"expr": "expr1",
"key": "key1",
"requestId": "req1",
"interval": "1s",
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"editorMode": "code",
"legendFormat": "__auto",
"range": false,
"instant": true,
"exemplar": false,
"hide": false,
"queryType": "timeSeriesQuery",
"utcOffsetSec": 14400,
"datasourceId": 1,
"intervalMs": 5000,
"maxDataPoints": 1514
},
{
"refId": "refId2",
"expr": "expr2",
"key": "key2",
"requestId": "req2",
"interval": "",
"format": "time_series",
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"editorMode": "code",
"legendFormat": "__auto",
"range": false,
"instant": true,
"exemplar": false,
"hide": false,
"queryType": "timeSeriesQuery",
"utcOffsetSec": 14400,
"datasourceId": 1,
"intervalMs": 5000,
"maxDataPoints": 1514
}
],
"range": {
"from": "1970-01-01T00:20:34.000Z",
"to": "1970-01-01T01:34:38.000Z",
"raw": {
"from": "1970-01-01T00:20:34.000Z",
"to": "1970-01-01T01:34:38.000Z"
}
},
"from": 1234000,
"to": 5678000
}'\\''`.replace(/[\n ]/g, '') +
` '\\''http://grafana/api/ds/query'\\'' | base64'`
)
})
63 changes: 63 additions & 0 deletions src/checkResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {callKubernetesPathAsync} from './callExecutables'

interface IGrafanaQuery {
refId: string
expr: string
reqId: string
key: string
interval: '1s' | ''
format?: 'time_series'
}

export function getDataFromGrafana(
fromDate: Date,
toDate: Date,
queries: IGrafanaQuery[]
) {
const data = {
queries: queries.map(q => ({
refId: q.refId,
expr: q.expr,
key: q.key,
requestId: q.reqId,
interval: q.interval,
...(q.format ? {format: q.format} : {}),
datasource: {
type: 'prometheus',
uid: 'prometheus'
},
editorMode: 'code',
legendFormat: '__auto',
range: false,
instant: true,
exemplar: false,
hide: false,
queryType: 'timeSeriesQuery',
utcOffsetSec: 14400,
datasourceId: 1,
intervalMs: 5000,
maxDataPoints: 1514
})),
range: {
from: fromDate.toISOString(),
to: toDate.toISOString(),
raw: {
from: fromDate.toISOString(),
to: toDate.toISOString()
}
},
from: fromDate.valueOf(),
to: toDate.valueOf()
}

let busyboxCmd = `wget -q -O- --header='content-type: application/json' --post-data='${JSON.stringify(
data
)}' 'http://grafana/api/ds/query' | base64`

busyboxCmd = busyboxCmd.replace(/'/g, "'\\''")

return callKubernetesPathAsync(
kubectl =>
`${kubectl} run -q -i --image=busybox --rm grafana-result-peeker --restart=Never -- sh -c '${busyboxCmd}'`
)
}

0 comments on commit b27573e

Please sign in to comment.