Hello,
My goal is as follows:
To combine two queries (corresponding to consumption data that don’t occur at the same time of year (one consumes the whole year, the other only part of it)), into a single query by adding up the values so that, in grafana, you can use this query to recalculate an expression.
The aim is to obtain a global value for use in an ultimate calculation.
Initially, in Grafana, I wanted to bring my two queries separately and add them together. But one of the queries has no value during a period. During this period, the addition cannot take place.
Here’s what I tried:
import "timezone"
option location = timezone.location(name: "Europe/Paris")
value1 = from(bucket: "GIN")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "Consumption")
|> filter(fn: (r) => r["_field"] == "value1")
|> group(columns: ["_measurement", "_field"])
|> aggregateWindow(every: 1d, fn: sum, createEmpty: true, offset: -1s)
|> fill(value: 0.0)
|> rename(columns: {_value: "value1_value"})
value2 = from(bucket: "GIN")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "Consumption")
|> filter(fn: (r) => r["_field"] == "value2")
|> group(columns: ["_measurement", "_field"])
|> aggregateWindow(every: 1d, fn: sum, createEmpty: true, offset: -1s)
|> rename(columns: {_value: "value2_value"})
joined = join(
tables: {value1: value1, value2: value2},
on: ["_time"],
method: "inner"
)
result = joined
|> map(fn: (r) => ({
_time: r._time,
combinedValue: r.value1_value + r.value2_value
}))
result
Unfortunately, two things went wrong:
1 :
- On “value1”, I only have values at the beginning of the year.
- On “value2”, I have values throughout the year
If I set a range of 6 months, the join is done correctly, if I set 30 days, the join is no longer done.
2 :
The values added are not correct.
Also, I need to multiply value1 by 0.6.
Thanks for your help