Changing datasource programmatically

Hey guys,

I’m trying to work out a way to automatically change a datasource rather than having to have a user select from a dropdown.

I have a query variable which is happily deducing the correct data source name depending on timescale, as an arbitrary string, but there seems to be a functionality gap from having that as a “normal” variable to being able to use it as a datasource.

I know if I were to instead return the UUID of the datasource then I can hack that variable in to the dashboard JSON file and it works, but that seems way too off the wall a solution to use in a professional, multiuser environment.

Can anyone suggest any only half mad ways that I change the value of a datasource dashboard variable without any direct user input?

If I could trigger a reload of the page with a hand-crafted url which includes a datasource variable then I see that would probably, however I doubt there’s a viable to way to get from “user zoomed out past 30 days” to “reload this page”.

I am running with HTML sanitize disabled, so actually, maybe some sort of javascript solution coooould work?

That sounds as overengineered solution. Why you just don’t query all datasources and just merge their results into ingle timeserie? Then you don’t need to care about timescales and datasources. Keep it simple.

Because I’d be doing vastly more query work for everything I do?

The point of the timescale is to switch to a downsampled database when I’m after old data or over a long time period. I don’t want to query a 10000 10s interval records if I can instead just query 333 5m interval records.

Your suggesting of “just” querying BOTH everytime seems… odd…? There is no “simple” benefit here as far as I can tell, just significantly increased system load.

what are your data sources? mssql? mysql? or …

It’s all influxdb v3. I know it’s not possible to do something like referencing different databases inside the queries, it has to be a datasource change, so (as I see it) data source type agnostic.

absolutely doable if it was mssql and different databases even across differrebt servers via linked server but not sure about influxdb

No, it’s not possible at the moment. It is in the v1 and v2 platforms but not in v3, so gotta switch the datasource. I have this working so simply in v2 with a Flux query, but elsewhere, it’s not proving as simple.

1 Like

But why you need raw data, when you can have time aggregated data based on current dashboard timerange?

E.g. InfluxQL (InfluxDB v1):

SELECT mean("availability") 
FROM "tests" 
WHERE $timeFilter 
GROUP BY time($__interval)

That $__interval is magical variable - it depends on selected timerange, e.g. you select 1 year time range, so it can be 1month, you select 1 day timerange - it can be 1hour

Because we downsample our data after 30 days, so we have a month of 10s data and over a year of 5m data.

That GROUP BY is still expensive, it still takes time and load. More time the wider the time range. Grouping (which I’m absolutely already doing) 330 rows is quicker than grouping 10000.

So if we’re past that, what options do we have to change the data source without user interaction?

In theory yes, but it can be a few ms in absolute value - do you have numbers how much slower will it be in your worst case? IMHO it’s not worth to have datasource switching just to save <500 ms - standard users won’t notice any significant difference (unless you have case for it).

Again, we downsample data after 30 days for storage reasons, so we need to use a different data source for older ranges. I’m not interested to merging two databases together and massively increasing system load and complexity of 50 panels. I’m really confused why you are going back to this again rather than answering the question I’m asking?

How can I change the datasource without user interaction?

so what would trigger the change if not the user?

I think the OP is trying to get Grafana to use data source A if the timescale
for the query is within the last 30 days, and data source B if the timescale
is older than that.

Antony.

2 Likes

Yep, that’s it. Various things trigger on timescale changes, so I can get the right UUID or something, I just can’t, say, use a UUID that’s stored a variable as the datasource in a panel, as it’s not a “datasource” variable, and I presume there’s no way to make it one.

1 Like

this might be possible but you will have to use a custom time picker with key/value

the key would be datasource_uid, value would be the datetime

the wheels for this approach will quickly fall off if/when you have a new datasource in the mix.

question for you @RocketSurgeon, are you using flux query language or influxql, if already mentioned above sorry to ask again.

I’m using FlightSQL / SQL principally in this situation, annoyingly if Flux worked on v3 then it would be very simple, but it’s deprecated.

1 Like

in mssql you can call a table in another database (B) from database A

Is that possible in your sql language?

No, they need to be specific datasources by design.

I am talking about within the datasource query space provided to add your query, can you query another database?

why was there a need to separate databases for older data? what is the technical reason behind this decision?

I’m really surprised that it’s deemed relevant to dig into this minutiae. We have a 10s database and 5m database. InfluxDB, as a time series database, has a concept of a retention period and automatically purges “old” data, so it’s standard to separate data by how long you want to retain it for.

So yet again, no, I can not query another database. Hence why I posted in the first place.