Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for template variables + handle timestamp stored as string #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions couchbase-datasource/pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ 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"
}
}

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
}

Expand Down
12 changes: 10 additions & 2 deletions couchbase-datasource/src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
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<CouchbaseQuery, CouchbaseOptions> {
annotations = {};
constructor(instanceSettings: DataSourceInstanceSettings<CouchbaseOptions>) {
super(instanceSettings);
}

// Support template variables
applyTemplateVariables(query: CouchbaseQuery, scopedVars: ScopedVars) {
return {
...query,
query: getTemplateSrv().replace(query.query, scopedVars)
};
}
}