From 82dfc20b2f61286b66f7734ee5c7f765a4d29777 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 19 Aug 2022 14:54:39 +0300 Subject: [PATCH] init CLI arguments --- package.json | 1 + src/tsdb-kit/index.ts | 22 +++++++++++++++++----- webpack.config.js | 2 +- yarn.lock | 5 +++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 069505a..bcba604 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "devDependencies": { "@types/jest": "^26.0.15", "@types/lodash": "^4.14.165", + "argparse": "^2.0.1", "jest": "^26.6.3", "ts-jest": "^26.4.4", "ts-loader": "^9.3.1", diff --git a/src/tsdb-kit/index.ts b/src/tsdb-kit/index.ts index 66e54ef..04f99c0 100644 --- a/src/tsdb-kit/index.ts +++ b/src/tsdb-kit/index.ts @@ -2,14 +2,26 @@ import { queryByConfig, QueryConfig } from '..'; import { DatasourceType, QueryType } from '../connectors'; +import { ArgumentParser } from 'argparse'; import * as _ from 'lodash'; +const parser = new ArgumentParser(); -// TODO: these `const`s should be CLI arguments -const PROMETHEUS_URL = 'http://localhost:9090'; -const QUERY = '100-(avg by (instance) (irate(node_cpu_seconds_total{job="nvicta-ai-node-exporter",mode="idle"}[5m])) * 100)'; -const FROM = 1660670020000; // ms -const TO = 1660670026000; // ms +// TODO: parse version from package.json +parser.add_argument('-v', '--version', { action: 'version', version: '0.1.1' }); +parser.add_argument('-u', '--url', { help: 'Datasource URL', required: true }); +parser.add_argument('-q', '--query', { help: 'Query Template', required: true }); +parser.add_argument('-f', '--from', { help: 'From timestamp (ms), e.g. 1660670020000. If not specified, `now-5m` is used' }); +parser.add_argument('-t', '--to', { help: 'To timestamp (ms), e.g. 1660670026000. If not specified, `now` is used' }); + +const args = parser.parse_args(); + +const timeNowInMs = new Date().getTime(); + +const PROMETHEUS_URL = args.url; +const QUERY = args.query; +const FROM = args.from || timeNowInMs - 5 * 60 * 1000; +const TO = args.to || timeNowInMs; const datasource = { type: DatasourceType.PROMETHEUS, diff --git a/webpack.config.js b/webpack.config.js index 96ee9fd..2cd6b25 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -18,7 +18,7 @@ module.exports = { module: { rules: [ { - test: /.ts?$/, + test: /.ts$/, loader: 'ts-loader', options: { configFile: 'bin.tsconfig.json' diff --git a/yarn.lock b/yarn.lock index 67afc9d..e539f99 100644 --- a/yarn.lock +++ b/yarn.lock @@ -905,6 +905,11 @@ argparse@^1.0.7: dependencies: sprintf-js "~1.0.2" +argparse@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38" + integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q== + arr-diff@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520"