Grafana API - Setting variables with a query

109 views
Skip to first unread message

JerLucid

unread,
Mar 27, 2023, 3:20:17 AM3/27/23
to AquaQ kdb+/TorQ
I am currently using the Grafana kdb+ API, and need help
setting variables using the "query" option, if anyone is familiar?

What I want to do is populate the symbol list drop down
with values returned from a query, as opposed to hard-coding them.

Currently, under settings->variables, I set "select variable type" to Query
and run this function, getSymbols[], on a relay process (not the Gateway)

which is defined as
getSymbols:{[] "ABC,CDE,FED,AIB"}

but there is an error in Grafana when I do this, saying 
Uncaught Error: Check Query. Syntax error with: [ "ABC","CDE","FED","AIB" ]
So it doesnt like the CSV file format, which I what I thought it needed
going by UserDocs

Anyone know what I am doing wrong here?
I have also checked what .z.ws is returning and looks fine

Thanks
Jer












JerLucid

unread,
Mar 27, 2023, 3:31:40 AM3/27/23
to AquaQ kdb+/TorQ
The solution here was simply to enlist each element in the list being returned.
So enlist each ("ABC";"DEF";"GHI" ....)

Daniel - AQ

unread,
Mar 27, 2023, 8:00:06 AM3/27/23
to AquaQ kdb+/TorQ
For the WebSocket-based Grafana plugin it will automatically concatenate any multi-value variables with a comma ','. When using Query variables if you select a single column from a table (or build one) it should parse each entry correctly, e.g.
select distinct sym from tablename
([] variables:`ABC`DEF`GHI)
([] variables:("ABC";"DEF";"GHI"))
As you have found, 'enlist each' on a vector will also work, although I wouldn't recommend this.

When running free-form queries using variables, the best way to handle these is to pass the variable inside a string and use kdb's 'vs' operator to split this string, then cast to the required type, e.g.:
select from tablename where sym in `$"," vs "${sym_variable}"

For the newer backend Grafana plugin, the variable injection is done through Grafana's newer Templating API so it can support advanced variable formats:
select from tablename where sym in `$"," vs "${sym_variable:csv}"
select from tablename where date in `$"+" vs "${date_variable:text}"

The backend plugin also supports Grafana's 'Query Inspector' which provides visibility of the built query after variable injection which will hit kdb+:
QUERY_INSPECTOR.PNG

Daniel

JerLucid

unread,
Mar 30, 2023, 3:55:22 AM3/30/23
to AquaQ kdb+/TorQ
Thanks Daniel, I am now using the backend Grafana plugin, and its working very well.
Querying the Gateway directly to populate variables dynamically. 
Reply all
Reply to author
Forward
0 new messages