Browse Source

Merge pull request 'SQL: support Grafana aggregation macros' (#13) from sql-support-grafana-aggregation-macros into master

Reviewed-on: #13
master
rozetko 2 years ago
parent
commit
b771a81962
  1. 2
      package.json
  2. 57
      spec/sql.jest.ts
  3. 6
      src/connectors/utils.ts

2
package.json

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

57
spec/sql.jest.ts

@ -162,6 +162,63 @@ describe('Test sql processing', function() {
check(original, expected); 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() { it('complex sql with one select', function() {
let original = `SELECT let original = `SELECT
statistics.created_at as time, 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 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 relim = /limit [0-9]+/ig;
let reoff = /offset [0-9]+/ig; let reoff = /offset [0-9]+/ig;

Loading…
Cancel
Save