From 3de0539c1be656311e7d867d13d3940e2e0872f4 Mon Sep 17 00:00:00 2001 From: gaurav Date: Thu, 31 Oct 2019 22:23:47 -0700 Subject: [PATCH 1/3] added support for weekly aggregates with period origin --- dist/datasource.js | 28 ++++++++++++++++++++++++---- dist/partials/config.html | 7 +++++++ src/datasource.js | 28 ++++++++++++++++++++++++---- src/partials/config.html | 7 +++++++ 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/dist/datasource.js b/dist/datasource.js index 8cb3b404..059992a8 100644 --- a/dist/datasource.js +++ b/dist/datasource.js @@ -31,6 +31,7 @@ function (angular, _, dateMath, moment) { instanceSettings.jsonData = instanceSettings.jsonData || {}; this.supportMetrics = true; this.periodGranularity = instanceSettings.jsonData.periodGranularity; + this.periodOrigin = instanceSettings.jsonData.periodOrigin; function replaceTemplateValues(obj,scopedVars, attrList) { if (obj.type === 'in') { @@ -81,11 +82,22 @@ function (angular, _, dateMath, moment) { }; this.testDatasource = function() { + if(this.dataSource.periodOrigin!=""){ + if(!this.isIsoDate(this.dataSource.periodOrigin)){ + return { status: "error", message: "Origin time must be in ISO8601 format", title: "Success" }; + } + } return this._get('/druid/v2/datasources').then(function () { return { status: "success", message: "Druid Data source is working", title: "Success" }; }); }; + this.isIsoDate = function(str) { + if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false; + var d = new Date(str); + return d.toISOString()===str; + }; + //Get list of available datasources this.getDataSources = function() { return this._get('/druid/v2/datasources').then(function (response) { @@ -159,10 +171,18 @@ function (angular, _, dateMath, moment) { //Round up to start of an interval //Width of bar chars in Grafana is determined by size of the smallest interval var roundedFrom = granularity === "all" ? from : roundUpStartTime(from, granularity); - if(dataSource.periodGranularity!=""){ - if(granularity==='day'){ - granularity = {"type": "period", "period": "P1D", "timeZone": dataSource.periodGranularity} - } + if (dataSource.periodGranularity != "") { + granularity = {"type": "period", "timeZone": dataSource.periodGranularity} + if (granularity === 'day') { + granularity["period"] = "P1D" + } else if (granularity === 'week') { + granularity["period"] = "P1W" + } else if (granularity === 'month') { + granularity["period"] = "P1M" + } + if (dataSource.periodOrigin != "") { + granularity["origin"] = dataSource.periodOrigin + } } return dataSource._doQuery(roundedFrom, to, granularity, target, options.scopedVars); }); diff --git a/dist/partials/config.html b/dist/partials/config.html index 3cdfb4fc..dd82195e 100644 --- a/dist/partials/config.html +++ b/dist/partials/config.html @@ -119,6 +119,13 @@

Druid Settings

This enables the to query hourly data in different timezone. +
+ Period Origin +
+ + This enables the to query to compute from a different epoch start, useful to start week from Sunday. +
+
diff --git a/src/datasource.js b/src/datasource.js index 8cb3b404..059992a8 100644 --- a/src/datasource.js +++ b/src/datasource.js @@ -31,6 +31,7 @@ function (angular, _, dateMath, moment) { instanceSettings.jsonData = instanceSettings.jsonData || {}; this.supportMetrics = true; this.periodGranularity = instanceSettings.jsonData.periodGranularity; + this.periodOrigin = instanceSettings.jsonData.periodOrigin; function replaceTemplateValues(obj,scopedVars, attrList) { if (obj.type === 'in') { @@ -81,11 +82,22 @@ function (angular, _, dateMath, moment) { }; this.testDatasource = function() { + if(this.dataSource.periodOrigin!=""){ + if(!this.isIsoDate(this.dataSource.periodOrigin)){ + return { status: "error", message: "Origin time must be in ISO8601 format", title: "Success" }; + } + } return this._get('/druid/v2/datasources').then(function () { return { status: "success", message: "Druid Data source is working", title: "Success" }; }); }; + this.isIsoDate = function(str) { + if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false; + var d = new Date(str); + return d.toISOString()===str; + }; + //Get list of available datasources this.getDataSources = function() { return this._get('/druid/v2/datasources').then(function (response) { @@ -159,10 +171,18 @@ function (angular, _, dateMath, moment) { //Round up to start of an interval //Width of bar chars in Grafana is determined by size of the smallest interval var roundedFrom = granularity === "all" ? from : roundUpStartTime(from, granularity); - if(dataSource.periodGranularity!=""){ - if(granularity==='day'){ - granularity = {"type": "period", "period": "P1D", "timeZone": dataSource.periodGranularity} - } + if (dataSource.periodGranularity != "") { + granularity = {"type": "period", "timeZone": dataSource.periodGranularity} + if (granularity === 'day') { + granularity["period"] = "P1D" + } else if (granularity === 'week') { + granularity["period"] = "P1W" + } else if (granularity === 'month') { + granularity["period"] = "P1M" + } + if (dataSource.periodOrigin != "") { + granularity["origin"] = dataSource.periodOrigin + } } return dataSource._doQuery(roundedFrom, to, granularity, target, options.scopedVars); }); diff --git a/src/partials/config.html b/src/partials/config.html index 3cdfb4fc..dd82195e 100644 --- a/src/partials/config.html +++ b/src/partials/config.html @@ -119,6 +119,13 @@

Druid Settings

This enables the to query hourly data in different timezone. +
+ Period Origin +
+ + This enables the to query to compute from a different epoch start, useful to start week from Sunday. +
+
From 0384d5508e60d7d5fc099bb7b95b9dba14682223 Mon Sep 17 00:00:00 2001 From: gaurav Date: Fri, 1 Nov 2019 03:26:03 -0700 Subject: [PATCH 2/3] added support for weekly aggregates with period origin --- dist/datasource.js | 33 +++++++++++++++++++++------------ dist/partials/config.html | 17 ++++++++++------- src/datasource.js | 33 +++++++++++++++++++++------------ src/partials/config.html | 17 ++++++++++------- 4 files changed, 62 insertions(+), 38 deletions(-) diff --git a/dist/datasource.js b/dist/datasource.js index 059992a8..ed4ae886 100644 --- a/dist/datasource.js +++ b/dist/datasource.js @@ -61,6 +61,20 @@ function (angular, _, dateMath, moment) { ['year', moment.duration(1, 'year')] ]; + var GRANULARITIESPERIODLOOKUP = { + 'second': '1S', + 'minute': '0H1M', + 'fifteen_minute': '0H15M', + 'thirty_minute': '0H30M', + 'hour': '1H', + 'day': '1D', + 'week': '7D', + 'month': '1M', + 'quarter': '3M', + 'year': '1Y' + }; + + var filterTemplateExpanders = { "selector": _.partialRight(replaceTemplateValues, ['value']), "regex": _.partialRight(replaceTemplateValues, ['pattern']), @@ -82,9 +96,9 @@ function (angular, _, dateMath, moment) { }; this.testDatasource = function() { - if(this.dataSource.periodOrigin!=""){ - if(!this.isIsoDate(this.dataSource.periodOrigin)){ - return { status: "error", message: "Origin time must be in ISO8601 format", title: "Success" }; + if(this.periodOrigin!=""){ + if(!this.isIsoDate(this.periodOrigin)){ + return { status: "error", message: "Origin time must be in ISO8601 (YYYY-MM-DDTHH:MM:SS.sssZ) format", title: "Error" }; } } return this._get('/druid/v2/datasources').then(function () { @@ -172,17 +186,12 @@ function (angular, _, dateMath, moment) { //Width of bar chars in Grafana is determined by size of the smallest interval var roundedFrom = granularity === "all" ? from : roundUpStartTime(from, granularity); if (dataSource.periodGranularity != "") { - granularity = {"type": "period", "timeZone": dataSource.periodGranularity} - if (granularity === 'day') { - granularity["period"] = "P1D" - } else if (granularity === 'week') { - granularity["period"] = "P1W" - } else if (granularity === 'month') { - granularity["period"] = "P1M" - } + if(granularity in GRANULARITIESPERIODLOOKUP){ + granularity = {"type": "period", "period": GRANULARITIESPERIODLOOKUP[granularity], "timeZone": dataSource.periodGranularity} if (dataSource.periodOrigin != "") { - granularity["origin"] = dataSource.periodOrigin + granularity["origin"] = dataSource.periodOrigin } + } } return dataSource._doQuery(roundedFrom, to, granularity, target, options.scopedVars); }); diff --git a/dist/partials/config.html b/dist/partials/config.html index dd82195e..514643b7 100644 --- a/dist/partials/config.html +++ b/dist/partials/config.html @@ -2,7 +2,7 @@

Druid Settings

- Period Granularity + Period Granularity
- This enables the to query to compute from a different epoch start, useful to start week from Sunday. -
+
+ + + This enables the to query to compute from a different epoch start, useful to + start week from Sunday. + +
diff --git a/src/datasource.js b/src/datasource.js index 059992a8..ed4ae886 100644 --- a/src/datasource.js +++ b/src/datasource.js @@ -61,6 +61,20 @@ function (angular, _, dateMath, moment) { ['year', moment.duration(1, 'year')] ]; + var GRANULARITIESPERIODLOOKUP = { + 'second': '1S', + 'minute': '0H1M', + 'fifteen_minute': '0H15M', + 'thirty_minute': '0H30M', + 'hour': '1H', + 'day': '1D', + 'week': '7D', + 'month': '1M', + 'quarter': '3M', + 'year': '1Y' + }; + + var filterTemplateExpanders = { "selector": _.partialRight(replaceTemplateValues, ['value']), "regex": _.partialRight(replaceTemplateValues, ['pattern']), @@ -82,9 +96,9 @@ function (angular, _, dateMath, moment) { }; this.testDatasource = function() { - if(this.dataSource.periodOrigin!=""){ - if(!this.isIsoDate(this.dataSource.periodOrigin)){ - return { status: "error", message: "Origin time must be in ISO8601 format", title: "Success" }; + if(this.periodOrigin!=""){ + if(!this.isIsoDate(this.periodOrigin)){ + return { status: "error", message: "Origin time must be in ISO8601 (YYYY-MM-DDTHH:MM:SS.sssZ) format", title: "Error" }; } } return this._get('/druid/v2/datasources').then(function () { @@ -172,17 +186,12 @@ function (angular, _, dateMath, moment) { //Width of bar chars in Grafana is determined by size of the smallest interval var roundedFrom = granularity === "all" ? from : roundUpStartTime(from, granularity); if (dataSource.periodGranularity != "") { - granularity = {"type": "period", "timeZone": dataSource.periodGranularity} - if (granularity === 'day') { - granularity["period"] = "P1D" - } else if (granularity === 'week') { - granularity["period"] = "P1W" - } else if (granularity === 'month') { - granularity["period"] = "P1M" - } + if(granularity in GRANULARITIESPERIODLOOKUP){ + granularity = {"type": "period", "period": GRANULARITIESPERIODLOOKUP[granularity], "timeZone": dataSource.periodGranularity} if (dataSource.periodOrigin != "") { - granularity["origin"] = dataSource.periodOrigin + granularity["origin"] = dataSource.periodOrigin } + } } return dataSource._doQuery(roundedFrom, to, granularity, target, options.scopedVars); }); diff --git a/src/partials/config.html b/src/partials/config.html index dd82195e..514643b7 100644 --- a/src/partials/config.html +++ b/src/partials/config.html @@ -2,7 +2,7 @@

Druid Settings

- Period Granularity + Period Granularity
- This enables the to query to compute from a different epoch start, useful to start week from Sunday. -
+
+ + + This enables the to query to compute from a different epoch start, useful to + start week from Sunday. + +
From 6d435b42fb8c2c646dd7b17a6d0750226e39f4a8 Mon Sep 17 00:00:00 2001 From: gaurav Date: Sat, 2 Nov 2019 04:00:40 -0700 Subject: [PATCH 3/3] added support for weekly aggregates with period origin - fixed adding `P` prefix --- dist/datasource.js | 20 ++++++++++---------- src/datasource.js | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/dist/datasource.js b/dist/datasource.js index ed4ae886..80a41300 100644 --- a/dist/datasource.js +++ b/dist/datasource.js @@ -62,16 +62,16 @@ function (angular, _, dateMath, moment) { ]; var GRANULARITIESPERIODLOOKUP = { - 'second': '1S', - 'minute': '0H1M', - 'fifteen_minute': '0H15M', - 'thirty_minute': '0H30M', - 'hour': '1H', - 'day': '1D', - 'week': '7D', - 'month': '1M', - 'quarter': '3M', - 'year': '1Y' + 'second': 'P1S', + 'minute': 'P0H1M', + 'fifteen_minute': 'P0H15M', + 'thirty_minute': 'P0H30M', + 'hour': 'P1H', + 'day': 'P1D', + 'week': 'P7D', + 'month': 'P1M', + 'quarter': 'P3M', + 'year': 'P1Y' }; diff --git a/src/datasource.js b/src/datasource.js index ed4ae886..80a41300 100644 --- a/src/datasource.js +++ b/src/datasource.js @@ -62,16 +62,16 @@ function (angular, _, dateMath, moment) { ]; var GRANULARITIESPERIODLOOKUP = { - 'second': '1S', - 'minute': '0H1M', - 'fifteen_minute': '0H15M', - 'thirty_minute': '0H30M', - 'hour': '1H', - 'day': '1D', - 'week': '7D', - 'month': '1M', - 'quarter': '3M', - 'year': '1Y' + 'second': 'P1S', + 'minute': 'P0H1M', + 'fifteen_minute': 'P0H15M', + 'thirty_minute': 'P0H30M', + 'hour': 'P1H', + 'day': 'P1D', + 'week': 'P7D', + 'month': 'P1M', + 'quarter': 'P3M', + 'year': 'P1Y' };