From 7a01a86ddb68f7e595213a36533a57b457b77b5c Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 13 Jan 2023 20:06:27 +0300 Subject: [PATCH 1/3] SQL: support Grafana aggregation macros ($__timeGroup and $__timeGroupAlias) --- src/connectors/utils.ts | 6 ++++++ 1 file changed, 6 insertions(+) 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; -- 2.34.1 From 3c768d4d92e4de25a0901e1e994e76f1aeba90d8 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 13 Jan 2023 20:07:10 +0300 Subject: [PATCH 2/3] tests for Grafana SQL aggregation macros --- spec/sql.jest.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) 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, -- 2.34.1 From 32425542ae4531fa99407e14d943536ee88cdb4b Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 13 Jan 2023 20:07:44 +0300 Subject: [PATCH 3/3] 2.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", -- 2.34.1