Flux Query : join.inner() returns nothing if I don't limit my stream used - influxdb

I get an issue understanding how to use the join.inner() function.
It seems I can only have a result (and the correct one) if I use the limit() function to the stream I want to use the join.inner function with.
If don't limit this left stream, I don't get any error but just no result.
It is because of how I get my left stream ?
Do you have any idea what I am doing wrong here ?
I am pretty new using InfluxDB therefore the flux language so it must be me.
Thank you all for your answers !
import "array"
import "join"
left =
from(bucket: "TestBucket")
|> range(start: 0)
|> filter(fn: (r) => r["_measurement"] == "TestMeasurement")
|> limit(n : 1000000000000000000)
|> group()
//|> yield(name: "LEFT")
right =
array.from(
rows: [
{arrayValue: "123", _time: 2023-02-07T12:00:00.000Z}, //This timestamp exists in the left stream
],
)
//|> yield(name: "RIGHT")
result = join.inner(
left: left,
right: right,
on: (l, r) => l._time == r._time, // I made sure that there is indeed a common time
as: (l, r) => ({l with rightValue: r.arrayValue}),
)
|> yield(name: "RESULT")

Ok, the solution was to group by _time column the stream AND the table :
|> group(columns: ["_time"])

Related

Dynamic Filtering using variable in filter function in Flux

Using the quantile function, I was able to get 95 % percentile value in a stream.
Now, i want to filter records which lie below the 95% percentile.
hence, I loop over my recods and filter records which lie below the percentile.
However, at this topic I get error –
Please find code below –
percentile = totalTimeByDoc
|> filter(fn: (r) => r["documentType"] == "PurchaseOrder")
|> group(columns:["documentType"])
// |> yield()
|> quantile(column: "processTime", q: 0.95, method: "estimate_tdigest", compression: 9999.0)
|> limit(n: 1)
|> rename(columns: {processTime: "pt"})
Gives me data – >
0 PurchaseOrder 999
Now, I try to loop over my records and filter -
percentile_filered = totalTimeByDoc
|> filter(fn: (r) => r["documentType"] == "PurchaseOrder")
|> filter(fn: (r) => r.processTime < percentile[0]["pt"])
|> yield()
Where, totalTimeByDoc is like below –
|0|PurchaseOrder|testpass22PID230207222747-1|1200|
|1|PurchaseOrder|testpass22PID230207222747-2|807|
|2|PurchaseOrder|testpass22PID230207222934-1|671|
|3|PurchaseOrder|testpass22PID230207222934-2|670|
I get following error from above query –
error #116:41-116:51: expected [{A with pt: B}] (array) but found stream[{A with pt: B}]
You are only missing column extraction from percentile stream. Have a look at Extract scalar values. In this very case, you could do
percentile = totalTimeByDoc
|> ...
|> rename(columns: {processTime: "pt"})
|> findColumn(fn: (key) => true, column: "pt")
percentile_filtered = totalTimeByDoc
|> filter(fn: (r) => r["documentType"] == "PurchaseOrder")
|> filter(fn: (r) => r.processTime < percentile[0])
|> yield()

Flux still has 2 tables after union and group function

I am using flux to sum 5 minute data per day for the past 7 days. I use aggregateWindow on 2 columns and then union to join them back together. I use sort and fill to get the data on each time stamp together. This bit works fine. The issue I have is that the original table is still there.
Code
data = from(bucket: "home")
|> range(start: -7d)
|> filter(fn: (r) => r["_field"] == "BATTERY_CHARGE" or r["_field"] == "BATTERY_DISCHARGE")
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({r with "Battery_Discharge": r.BATTERY_DISCHARGE / 12.0}))
|> map(fn: (r) => ({r with "Battery_Charge": r.BATTERY_CHARGE / 12.0}))
|> keep(columns: ["_time", "Battery_Charge", "Battery_Discharge"])
Discharge_Sum = data
|> aggregateWindow(column: "Battery_Discharge", every: 1d, offset: -8h, fn: sum)
Charge_Sum = data
|> aggregateWindow(column: "Battery_Charge", every: 1d, offset: -8h, fn: sum)
union(tables: [Discharge_Sum, Charge_Sum])
|> yield(name: "result")
|> group(columns: ["_time"], mode: "by")
|> sort(columns: ["Battery_Charge"])
|> fill(column: "Battery_Discharge", usePrevious: true)
|> tail(n: 1)
|> group()
|> tail(n: 7)
|> drop(fn: (column) => column =~ /^(_start|_stop|_measurement|location|region)/)
|> rename(columns: {Battery_Discharge: "Battery Discharge", Battery_Charge: "Battery Charge"})
The table data is as follows.
The bottom table is what I want to keep.
I have been unable to find anything that allows a table to be deleted. I have tried to filter out the first table value but after the union the filter functions only work on the output table.
any ideas?

What does `|>` mean in TICKscript

Trying to write my fist TICKscript to work out when two sensor values cross: if the outside temperature has changed from lower to higher than the inside temperature then I need to close the windows (and conversely).
Using the query builder in InfluxDB I'm getting this for the meadian of the temperature values inside the house over the last 15 minutes:
from(bucket: "zigbee")
|> range(start: -15m, stop: now())
|> filter(fn: (r) => r["room"] == "Kitchen" or r["room"] == "DiningRoom" or r["room"] == "Bed3" or r["room"] == "Bed1")
|> filter(fn: (r) => r["_field"] == "temperature")
|> group(columns: ["_measurement"])
|> aggregateWindow(every: 15m, fn: mean, createEmpty: false)
|> yield(name:"inside")
The syntax |> appears to undocumented -- can you provide a reference?
Replacing |> with | breaks it.
It seems that group and aggregateWindow do not commute?
Presumably because aggregateWindow is forced to choose a single representative _time value for each window?
I think the plan is to
assign this to a stream,
copy and edit to creata a second stream shifted by 15 minutes,
create a second pair of streams for the outside temperature.
join all four streams and caluclate a value indicating whether the inside and outside temperatures have crossed over.
Unless you have a better idea?
(Right now it's looking easier to import the data into SQL.)
Check InfluxDB Flux language documentation for |>:
InfluxDB Pipe-forward operator
According to your flux syntax query:
from(bucket: "zigbee")
|> range(start: -15m, stop: now())
|> filter(fn: (r) => r["room"] == "Kitchen" or r["room"] == "DiningRoom" or r["room"] == "Bed3" or r["room"] == "Bed1")
|> filter(fn: (r) => r["_field"] == "temperature")
|> group(columns: ["_measurement"])
|> aggregateWindow(every: 15m, fn: mean, createEmpty: false)
|> yield(name:"inside")
You are taking data from bucket "zigbee"
Data from source are passed to range filter function with pipe-forward |> operator
Results from range filter data are passed to next filter function with another pipe-forward operator
Etc.
So all data flows as a result from one function to another.
You can group by but in your case columns are "room" key values if I understand your intentions correctly, so try:
|> group(columns: ["room"])
There is a difference between key values and measurement names - you should check InfluxDB documentation for understatnding data structure.
Flux data model documentation
I'ts not TICKscript, it's something do to with InfluxDB that might be called flux.
mean = from(bucket: "zigbee")
|> range(start: -5d, stop: now())
|> filter(fn: (r) => r["room"] == "Outside")
|> filter(fn: (r) => r["_measurement"] == "temperature")
|> aggregateWindow(every: 30m, fn: mean, createEmpty: false)
shift = mean
|> timeShift(duration: -3h)
j = join(tables: {mean: mean, shift: shift}, on: ["_time"])
|> map(fn: (r) => ({ r with diff: float(v: r._value_mean) - float( v: r._value_shift) }))
// yield contains 1 table with the required columns, but the UI doesn't understand it.
// The UI requires 1 table for each series.
j |> map(fn: (r) => ({_time: r._time, _value: r._value_mean})) |> yield(name: "mean")
j |> map(fn: (r) => ({_time: r._time, _value: r._value_shift})) |> yield(name: "shift")
j |> map(fn: (r) => ({_time: r._time, _value: r.diff})) |> yield(name: "diff")
The |> in TickScript "Declares a chaining method call which creates an instance of a new node and chains it to the node above it." as said in the official documentation

Work with non-table values, aka "A is not subtractable"

I see many similar questions but couldn't find a good match.
If we define a query and the result aught to be single value, is there a flux way to store as such? Example:
total = from(bucket: "xxx")
|> range(start: 0)
|> filter(fn: (r) => ...)
|> keep(columns: ["_value"])
|> sum()
consumed = from(bucket: "xxx")
|> range(start: 0)
|> filter(fn: (r) => ...)
|> keep(columns: ["_value"])
|> last()
total - consumed
Results in
invalid: error #18:1-18:40: [A] is not Subtractable
I can think of other ways to solve similar issues, but this example made me question whether flux actually supports easy working with single values or 1x1 relations.
Thanks
Not answering my original question but I want to provide the workaround I went with to solve this. I would still be interested in a more direct solution.
I've introduced a second column, then joined the two tables on that column:
total = from(bucket: "xxx")
|> range(start: 0)
|> filter(fn: (r) => ...)
|> keep(columns: ["_value"])
|> sum()
// Added:
|> map(fn: (r) => ({ age: "latest", _value:r._value }))
consumed = from(bucket: "xxx")
|> range(start: 0)
|> filter(fn: (r) => ...)
|> keep(columns: ["_value"])
|> last()
// Added:
|> map(fn: (r) => ({ age: "latest", _value:r._value }))
join(tables: {total: total, consumed: consumed}, on: ["age"])
|> map(fn: (r) => ({_value: r._value_total - r._value_consumed}))
In the query, total and consumed are tables. For how to extract and use scalar values, please see Extract scalar values in Flux

Append calculated field (percentage) and combine with results from different datasets, in Influx Flux

I'm struggling with an Influx 2 query in Flux on how to join and map data from two differents sets (tables) into a specific desired output.
My current Flux query is this:
data = from(bucket: "foo")
|> range(start:-1d)
|> filter(fn: (r) => r._measurement == "io")
|> filter(fn: (r) => r["device_id"] == "12345")
|> filter(fn: (r) => r._field == "status_id" )
# count the total points
totals = data
|> count(column: "_value")
|> toFloat()
|> set(key: "_field", value: "total_count")
# calculate the amount of onlines points (e.g. status = '1')
onlines = data
|> filter(fn: (r) => r._value == 1)
|> count(column: "_value")
|> toFloat()
|> set(key: "_field", value: "online_count")
union(tables: [totals, onlines])
This returns as output:
[{'online_count': 58.0}, {'total_count': 60.0}]
I would like to have appended to this output a percentage calculated from this. Something like:
[{'online_count': 58.0}, {'total_count': 60.0}, {'availability': 0.96666667}]
I've tried combining this using .map(), but to no avail:
# It feels like the map() is what I need, but can't find the right
# combination with .join/union(), .map(), .set()., .keep() etc.
union(tables: [totals, onlines])
|> map(fn: (r) => ({ r with percentage_online: r.onlines.online_count / r.totals.total_count * 100 }))
How can I append the (calculated) percentage as new field 'availability' in this Flux query?
Or, alternatively, is there a different Flux query approach to achieve this outcome?
N.B. I am aware of the Calculate percentages with Flux article from the docs, which I can't get working into this specific scenario. But it's close.

Resources