From 2cc15a9165efb7d4f78e7913848b42c21cbdb7d8 Mon Sep 17 00:00:00 2001 From: Laurens Miers Date: Tue, 10 Dec 2024 22:21:44 +0100 Subject: [PATCH 1/3] chore(plugin.go): typo --- couchbase-datasource/pkg/plugin/plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/couchbase-datasource/pkg/plugin/plugin.go b/couchbase-datasource/pkg/plugin/plugin.go index 9c7befe..eb98145 100644 --- a/couchbase-datasource/pkg/plugin/plugin.go +++ b/couchbase-datasource/pkg/plugin/plugin.go @@ -191,7 +191,7 @@ func (d *CouchbaseDatasource) query(channel *string, query_data *QueryRequest) b } if timeField == nil { - response.Error = errors.New("Failed to detect time field. Pleae use time_range(fieldName) or str_time_range(fieldName) functions in WHERE clause of your query.") + response.Error = errors.New("Failed to detect time field. Please use time_range(fieldName) or str_time_range(fieldName) functions in WHERE clause of your query.") return response } From 88a3ea79665590d1123999eabde49bf9fbc539c3 Mon Sep 17 00:00:00 2001 From: Laurens Miers Date: Tue, 10 Dec 2024 22:21:55 +0100 Subject: [PATCH 2/3] fix(plugin.go): convert field to number when using time_range Some funny DB store the timestamp as a string, handle these by converting the field to a number. --- couchbase-datasource/pkg/plugin/plugin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/couchbase-datasource/pkg/plugin/plugin.go b/couchbase-datasource/pkg/plugin/plugin.go index eb98145..1a25a5e 100644 --- a/couchbase-datasource/pkg/plugin/plugin.go +++ b/couchbase-datasource/pkg/plugin/plugin.go @@ -185,8 +185,8 @@ func (d *CouchbaseDatasource) query(channel *string, query_data *QueryRequest) b return response } timeField = &match[timeRg.SubexpIndex("field")] - query_string = timeRg.ReplaceAllString(query_string, fmt.Sprintf("$1 > STR_TO_MILLIS('%s') AND $1 <= STR_TO_MILLIS('%s')", tr.From.Format(time.RFC3339), tr.To.Format(time.RFC3339))) - query_string = "SELECT * FROM (" + query_string + ") AS data ORDER by data." + *timeField + " ASC" + query_string = timeRg.ReplaceAllString(query_string, fmt.Sprintf("TO_NUMBER($1) > STR_TO_MILLIS('%s') AND TO_NUMBER($1) <= STR_TO_MILLIS('%s')", tr.From.Format(time.RFC3339), tr.To.Format(time.RFC3339))) + query_string = "SELECT * FROM (" + query_string + ") AS data ORDER by TO_NUMBER(data." + *timeField + ") ASC" } } From 297be553f414cadfefc2ef2700e24db244e26d4e Mon Sep 17 00:00:00 2001 From: Laurens Miers Date: Fri, 13 Dec 2024 14:42:35 +0100 Subject: [PATCH 3/3] feat(datasource.ts): Add support for template variables in the query Resources: - https://grafana.com/developers/plugin-tools/how-to-guides/data-source-plugins/add-support-for-variables#using-template-variables - https://community.grafana.com/t/how-to-use-template-variables-in-your-data-source/63250#backend-data-sources-3 --- couchbase-datasource/src/datasource.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/couchbase-datasource/src/datasource.ts b/couchbase-datasource/src/datasource.ts index ddcf42d..7994f09 100644 --- a/couchbase-datasource/src/datasource.ts +++ b/couchbase-datasource/src/datasource.ts @@ -1,5 +1,5 @@ -import { DataSourceInstanceSettings } from '@grafana/data'; -import { DataSourceWithBackend } from '@grafana/runtime'; +import { DataSourceInstanceSettings, ScopedVars } from '@grafana/data'; +import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime'; import { CouchbaseOptions, CouchbaseQuery } from './types'; export class Couchbase extends DataSourceWithBackend { @@ -7,4 +7,12 @@ export class Couchbase extends DataSourceWithBackend) { super(instanceSettings); } + + // Support template variables + applyTemplateVariables(query: CouchbaseQuery, scopedVars: ScopedVars) { + return { + ...query, + query: getTemplateSrv().replace(query.query, scopedVars) + }; + } }