You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
2.0 KiB

import { queryByConfig, QueryConfig } from '..';
import { DatasourceType, QueryType } from '../connectors';
const { version } = require('../../package.json')
import { ArgumentParser } from 'argparse';
import * as _ from 'lodash';
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('-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 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.POSTGRES,
// TODO: remove GRAFANA_URL from here
url: `${GRAFANA_URL}/api/ds/query`,
auth,
};
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);
})
.catch(err => {
console.error('Query error: ', err);
});