From 6acadf8c0f5baeacb2ff659491b00b98f3c1740d Mon Sep 17 00:00:00 2001 From: Philipp Paulweber Date: Thu, 8 Feb 2024 20:02:58 +0100 Subject: [PATCH 1/4] Types: updated DEFAULT_QUERY export --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 683d9c7..cc6de3a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -25,7 +25,7 @@ export const DEFAULT_QUERY: Partial = { mode: "raw" , surql: "info for database" , requery: true -}; +} /** * These are options configured for each DataSource instance From f9d338e70bf74ad80461d5b95a447b0f68595122 Mon Sep 17 00:00:00 2001 From: Philipp Paulweber Date: Thu, 8 Feb 2024 20:08:47 +0100 Subject: [PATCH 2/4] Deployment: updated SurrealDB configuration to enforce authentication --- docker-compose.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yaml b/docker-compose.yaml index ccbe68d..80a49b2 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -27,6 +27,7 @@ services: - ./.attic/surrealdb:/surreal.db command: | start + --auth --log trace --user root --pass root From 1428e5f89e8ddb0a3a15b479ee30d4fb87311f76 Mon Sep 17 00:00:00 2001 From: Philipp Paulweber Date: Thu, 8 Feb 2024 20:15:06 +0100 Subject: [PATCH 3/4] Datasource: provided dashboard variable query functionality in the frontend --- src/datasource.ts | 85 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/src/datasource.ts b/src/datasource.ts index 6eff3cd..87a08ca 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -1,8 +1,11 @@ import -{ DataSourceInstanceSettings +{ CoreApp +, DataSourceInstanceSettings , DataQueryRequest , DataQueryResponse -, CoreApp +, MetricFindValue +, ScopedVars +, TimeRange } from '@grafana/data'; import @@ -33,12 +36,84 @@ export class DataSource extends DataSourceWithBackend { + let now = new Date(); + let range = options?.range; + let scopedVars = options.scopedVars; + let variableId = options.variable.id; + let interval = scopedVars?.__interval?.text || "1s" + let intervalMs = scopedVars?.__intervalMs?.value || 1000 + + let query: MyQuery[] = + [ { refId: variableId + , mode: "raw" + , surql: surql + , requery: false + } + ]; + + let request: DataQueryRequest = + { requestId: variableId + , app: "dashboard" + , timezone: "browser" + , interval: interval + , intervalMs: intervalMs + , range: range + , startTime: now.getTime() + , scopedVars: scopedVars + , targets: query }; + + let observable = super.query(request) + return new Promise( + (myResolve, myReject) => { + let response: any = {} + + observable.subscribe({ + next(element) { + response = element + }, + error(err) { + myReject([ { "text": "Query failed: " + err } ]) + }, + complete() { + if( response.state === 'Done' ) { + let values: any[] = [] + + response.data[0].fields[0].values.forEach( + (element: any) => { + let text = element + if( typeof text !== "string" ) { + text = JSON.stringify(text) + } + values.push( { "text": text } ) + } + ); + myResolve(values) + } else if( response.state === 'Error' ) { + myReject([{ "text": response.error.message }]) + } else { + myReject([{ "text": "Query failed: internal error" }]) + } + }, + }); + } + ); } } From 9c5ccb4c373c9ac57d9713e259ad3b4bc3fb9090 Mon Sep 17 00:00:00 2001 From: Philipp Paulweber Date: Thu, 8 Feb 2024 20:16:18 +0100 Subject: [PATCH 4/4] Provisioning: updated default dashboard setup with variable-based log level query --- .../dashboards/Default/default.json | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/etc/provisioning/dashboards/Default/default.json b/etc/provisioning/dashboards/Default/default.json index 35ec192..9aace85 100644 --- a/etc/provisioning/dashboards/Default/default.json +++ b/etc/provisioning/dashboards/Default/default.json @@ -162,7 +162,7 @@ "mode": "raw", "refId": "A", "requery": true, - "surql": "select * from timeseries:[$from]..[$to]" + "surql": "select * from timeseries:[$from]..[$to] where level in \"$variable\"" } ], "title": "Timeseries Data Table", @@ -257,7 +257,7 @@ "mode": "raw", "refId": "A", "requery": true, - "surql": "select * from timeseries:..[$to]" + "surql": "select * from timeseries:..[$to] where level in \"$variable\"" } ], "title": "Timeseries Value", @@ -359,7 +359,7 @@ "rateZero": true, "refId": "A", "requery": true, - "surql": "select * from timeseries:[$from]..[$to]" + "surql": "select * from timeseries:[$from]..[$to] where level in \"$variable\"" } ], "title": "Timeseries Rate Grouped By Level", @@ -471,7 +471,7 @@ "rateZero": true, "refId": "A", "requery": true, - "surql": "select * from timeseries:[$from]..[$to]", + "surql": "select * from timeseries:[$from]..[$to] where level in \"$variable\"", "timestamp": "" } ], @@ -483,7 +483,38 @@ "schemaVersion": 38, "tags": [], "templating": { - "list": [] + "list": [ + { + "allValue": "", + "current": { + "selected": false, + "text": [ + "All" + ], + "value": [ + "$__all" + ] + }, + "datasource": { + "type": "fiskaly-surrealdb-datasource", + "uid": "fiskaly-surrealdb-datasource" + }, + "definition": "select level from timeseries", + "description": "Variable-based query to filter data by log level.", + "hide": 0, + "includeAll": true, + "label": "Log Level", + "multi": true, + "name": "variable", + "options": [], + "query": "select level from timeseries", + "refresh": 1, + "regex": "", + "skipUrlSync": false, + "sort": 0, + "type": "query" + } + ] }, "time": { "from": "now-2m",