Compare commits

..

2 Commits

  1. 2
      package.json
  2. 57
      spec/sql.jest.ts
  3. 6
      src/connectors/utils.ts
  4. 3
      src/services/query_service/grafana.ts
  5. 30
      src/tsdb-kit/index.ts

2
package.json

@ -1,6 +1,6 @@
{
"name": "@corpglory/tsdb-kit",
"version": "2.0.4",
"version": "2.0.0",
"description": "",
"scripts": {
"build": "yarn build:lib && yarn build:bin",

57
spec/sql.jest.ts

@ -162,63 +162,6 @@ describe('Test sql processing', function() {
check(original, expected);
});
it('sql with $__timeGroup aggregation', function () {
const original = `SELECT
$__timeGroup("time", $__interval, NULL),
avg("metric") AS "Réseau"
FROM metric_values
WHERE $__timeFilter("time")
GROUP BY 1
ORDER BY 1`;
const expected = `SELECT
"time",
avg("metric") AS "Réseau"
FROM metric_values
WHERE $__timeFilter("time")
GROUP BY 1
ORDER BY 1 LIMIT ${limit} OFFSET ${offset}`;
check(original, expected);
});
it('sql with $__timeGroupAlias aggregation', function () {
const original = `SELECT
$__timeGroupAlias("time", $__interval),
avg("metric") AS "Réseau"
FROM metric_values
WHERE $__timeFilter("time")
GROUP BY 1
ORDER BY 1`;
const expected = `SELECT
"time",
avg("metric") AS "Réseau"
FROM metric_values
WHERE $__timeFilter("time")
GROUP BY 1
ORDER BY 1 LIMIT ${limit} OFFSET ${offset}`;
check(original, expected);
});
it('sql with $__timeGroupAlias aggregation and linebreaks', function () {
const original = `SELECT
$__timeGroupAlias(
any_field,
$__interval
),
avg("metric") AS "Réseau"
FROM metric_values
WHERE $__timeFilter(any_field)
GROUP BY 1
ORDER BY 1`;
const expected = `SELECT
any_field,
avg("metric") AS "Réseau"
FROM metric_values
WHERE $__timeFilter(any_field)
GROUP BY 1
ORDER BY 1 LIMIT ${limit} OFFSET ${offset}`;
check(original, expected);
});
it('complex sql with one select', function() {
let original = `SELECT
statistics.created_at as time,

6
src/connectors/utils.ts

@ -8,12 +8,6 @@ export function processSQLLimitOffset(sql: string, limit: number, offset: number
}
sql = splits[0]; // removes ";" from EOL
const reAggregation = /\$__timeGroup(?:Alias)?\(\s*([^,]+)\s*,\s*\$__interval[^\)]*\)/igm;
const occurence = reAggregation.exec(sql);
if(occurence) {
sql = sql.replace(reAggregation, occurence[1]);
}
let relim = /limit [0-9]+/ig;
let reoff = /offset [0-9]+/ig;

3
src/services/query_service/grafana.ts

@ -29,8 +29,7 @@ export class GrafanaQueryService extends QueryService {
_.defaults(axiosQuery, query.schema);
try {
const resp = await axios(axiosQuery);
return resp;
return axios(axiosQuery);
} catch (e) {
// TODO: seems like this error handler can be used for both Grafana and Direct queries
const msg = `TSDB-kit: fail while request data: ${e.message}`;

30
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);
})

Loading…
Cancel
Save