diff --git a/package.json b/package.json index b8e463c..7064cc4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@corpglory/tsdb-kit", - "version": "2.0.2", + "version": "2.0.3", "description": "", "scripts": { "build": "yarn build:lib && yarn build:bin", diff --git a/spec/sql.jest.ts b/spec/sql.jest.ts index 6c27003..f932626 100644 --- a/spec/sql.jest.ts +++ b/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, diff --git a/src/connectors/utils.ts b/src/connectors/utils.ts index 3892437..9d4aec5 100644 --- a/src/connectors/utils.ts +++ b/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;