Compare commits

...

9 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

2
package.json

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

57
spec/sql.jest.ts

@ -162,6 +162,63 @@ 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,6 +8,12 @@ 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,7 +29,8 @@ export class GrafanaQueryService extends QueryService {
_.defaults(axiosQuery, query.schema);
try {
return axios(axiosQuery);
const resp = await axios(axiosQuery);
return resp;
} 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}`;

Loading…
Cancel
Save