diff --git a/src/tsdb-kit/index.ts b/src/tsdb-kit/index.ts index 47854a7..875fe0d 100644 --- a/src/tsdb-kit/index.ts +++ b/src/tsdb-kit/index.ts @@ -10,36 +10,46 @@ const parser = new ArgumentParser(); parser.add_argument('-v', '--version', { action: 'version', version }); parser.add_argument('-U', '--url', { help: 'Datasource URL', required: true }); -parser.add_argument('-q', '--query', { help: 'Query Template', 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' }); parser.add_argument('-u', '--username', { help: 'Basic Auth Username' }); parser.add_argument('-p', '--password', { help: 'Basic Auth Password' }); +parser.add_argument('-a', '--api-key', { help: 'Grafana API Key', dest: 'apiKey' }); 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 GRAFANA_URL = args.url; +// const QUERY = args.query; +const FROM = args.from || timeNowInMs - 30 * 24 * 60 * 60 * 1000; const TO = args.to || timeNowInMs; const USERNAME = args.username; const PASSWORD = args.password; +const API_KEY = args.apiKey; let auth; if(USERNAME && PASSWORD) { auth = { username: USERNAME, password: PASSWORD }; } const datasource = { - type: DatasourceType.PROMETHEUS, - // TODO: remove PROMETHEUS_URL from here - url: `${PROMETHEUS_URL}/api/v1/query_range?query=${QUERY}&start=1543411320&end=1543432950&step=30`, + type: DatasourceType.POSTGRES, + // TODO: remove GRAFANA_URL from here + url: `${GRAFANA_URL}/api/ds/query`, auth, }; -const targets = []; -const queryConfig = new QueryConfig(QueryType.DIRECT, datasource, targets); -queryByConfig(queryConfig, PROMETHEUS_URL, FROM, TO) +const targets = [{ + datasource: { uid: "Jivp0LOVk", type: "postgres" }, + datasourceId: 2, + format: "time_series", + intervalMs: 7200000, + maxDataPoints: 434, + rawSql: "SELECT\n \"time\" AS \"time\",\n eur\nFROM rate_test\nWHERE\n $__unixEpochFilter(\"time\")\nORDER BY 1", + refId: "A" +}]; +const queryConfig = new QueryConfig(QueryType.GRAFANA, datasource, targets); +queryByConfig(queryConfig, GRAFANA_URL, FROM, TO, API_KEY) .then(res => { console.log(res); })