From b27573e75026b1e7822dfad6d46ef666be19d1e2 Mon Sep 17 00:00:00 2001 From: Ilya Shevchenko Date: Tue, 23 May 2023 15:53:37 +0300 Subject: [PATCH] feat: add getDataFromGrafana func --- src/__tests__/checkResults.test.ts | 104 +++++++++++++++++++++++++++++ src/checkResults.ts | 63 +++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 src/__tests__/checkResults.test.ts create mode 100644 src/checkResults.ts diff --git a/src/__tests__/checkResults.test.ts b/src/__tests__/checkResults.test.ts new file mode 100644 index 00000000..e8e891f6 --- /dev/null +++ b/src/__tests__/checkResults.test.ts @@ -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'` + ) +}) diff --git a/src/checkResults.ts b/src/checkResults.ts new file mode 100644 index 00000000..9fec1745 --- /dev/null +++ b/src/checkResults.ts @@ -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}'` + ) +}