Modifying option in flux query?

Hello, need help … this simple query fails (no plot on the time series panel) when $origin is set to “3” … because of bad option usage (not using variable as they are immutable), but I don’t know how to solve it … can you help? it works when $origin is set to “1” or “2”.

$origin can be selected from “1”,“2”,“3”
$destination is constant = “3”

option lv_origin = “$origin”
option lv_destination = “$destination”
from(bucket: “simdataset”)
|> range(start: -4d)
|> filter(fn: (r) => r[“_measurement”] == “$measurement”)
|> filter(fn: (r) => r[“_field”] == “waitingTime”)
|> filter(fn: (r) => if lv_origin == lv_destination then lv_origin == “2” else lv_origin == lv_origin)
|> filter(fn: (r) => (r[“origin”] == lv_origin and r[“destination”] == lv_destination))
|> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
|> yield(name: “last”)

have a good day

in my code snippet, probably this line compiles but is functionally wrong

|> filter(fn: (r) => if lv_origin == lv_destination then lv_origin == “2” else lv_origin == lv_origin)

but this one fails at compile time

|> filter(fn: (r) => if lv_origin == lv_destination then lv_origin = “2” else lv_origin = lv_origin)

with this error

invalid: compilation failed: error @7:24-7:71: expected ELSE, got ASSIGN (=) at 7:70 error @7:72-7:75: invalid expression @7:70-7:71: = error @7:81-7:90: invalid expression @7:76-7:80: else error @7:93-7:102: invalid expression @7:91-7:92: =

I have a workaround to avoid having to modify the option …

// define function
myfunc = (origin, destination) => if origin == destination then “2” else origin
option lv_destination = “$destination”
option lv_origin = myfunc(origin: “$origin”, destination: “$destination”)

from(bucket: “simdataset”)
|> range(start: -4d)
|> filter(fn: (r) => r[“_measurement”] == “$measurement”)
|> filter(fn: (r) => r[“_field”] == “waitingTime”)
|> filter(fn: (r) => (r[“origin”] == lv_origin and r[“destination”] == lv_destination))
|> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
|> yield(name: “last”)

1 Like

yep, thanks, got it.

moved to that right now

myfunc = (origin, destination) => if origin == destination then “2” else origin
lv_destination = “$destination”

from(bucket: “simdataset”)
|> range(start: -4d)
|> filter(fn: (r) => r[“_measurement”] == “$measurement”)
|> filter(fn: (r) => r[“_field”] == “waitingTime”)
|> filter(fn: (r) => (r[“origin”] == myfunc(origin: “$origin”, destination: “$destination”) and r[“destination”] == lv_destination))
|> yield(name: “last”)

but, now, what if I want $origin to cover a range from 0 to 3 … something like this


|> filter(fn: (r) =>
(r[“origin”] == “0” and r[“destination”] == lv_destination)) or
(r[“origin”] == “1” and r[“destination”] == lv_destination)) or
(r[“origin”] == “2” and r[“destination”] == lv_destination)) or
(r[“origin”] == “3” and r[“destination”] == lv_destination))

how to implement a loop over all $origin values instead of having the repetition (mainly because I don’t know a “priori” how many values I have in the $origin range)

Is origin string or integer. If string Why is it a string? Does it xontain non “numeric” values, text values?

Look at

Greater than in lexicographic order

Right now it is string because I think this is the way it is stored in the influxdb.

But functionally speaking, origin represents the identification of a location, this identification could be A, B or C (instead of 0, 1, 2, 3) hence string seems more appropriate.

destination is also the identification of a location, and therefore I’m interested in the path from A → B, A → C, B → A … but not A → A or B-> B and C → C.

and for example, if I have

origin = [A,B,C]
destination = [A,B,D]

I will search for

A->B
A->D
B->A
B->D
C->A
C->B
C->D

Then look at

Greater than/Less than in lexicographic order

Would this work?

> "-1" and < "4"

But you are treading on dangerous grounds with the value being string when it is really not. And because of that you have to do some odd querying

yes, I’m newbee in Grafana and flux language … and I agree perhaps I’m not taking the right way, even the right way to think about the problem …

at the end instead of A, B, C and D, I’ll probably have names in the data base like “Pittsburgh”, “Houston”, “Dallas”, “Miami”.

This is the reason why I’m wondering if I can build a query iterating over several values …

While writing these lines, I’m thinking that those names comes from the database, one column for the origin and one column for the destination … would it be easier, perhaps, to directly play with the columns …

time tripduration origin destination
xx0 yyy dallas houston
xx1 yyy miami dallas
xx2 yyy dallas houston
xx3 yyy pittsburg houston
xx4 yyy miami dallas
xx5 yyy miami houston

Then explain your task in terms of requirements first. If you think implementation first it could shoe horn you into something you will need to back out of. Forget the code and tell us what you want to do in plain English what are you trying to solve. Then the solution, irregardless of language used, will come naturally

1 Like

I would like one curve for each combination (time on x coordinate) … in my example → 4 curves

dallas - houston with 2 points
miami - dallas with 2 points
pittsburg - houston with 1 point
miami - houston with 1 point

Still too techie plain english please.
Ie : i want to find the shortest distance given multiple origin and destination combinations :blush:

1 Like

Well :slight_smile: here is the description with my best english :slight_smile: or “frenglish”

I have a network of N stations called “Pittsburgh”, “Houston”, “Dallas”, “Miami”, …
I simulate 1000 successive random travels from an origin station to a destination station (different from the origin)
for each travel I store the following info in the influxdb:

  • start time (timestamp)
  • origin station (tag)
  • destination station (tag)
  • trip duration (field)

For each combination “origin-destination” from the database, i want to display the trip duration over the time.

In fact not really for each combination, as I want the user to be able to select one or several or all "origin-destination"combinations to be displayed in the panel

this does the trick for all combinations … without selection by the user :

from(bucket: “simdataset”)
|> range(start: -4d)
|> filter(fn: (r) => r[“_measurement”] == “$measurement”)
|> filter(fn: (r) => r[“_field”] == “waitingTime”)
|> filter(fn: (r) => (r[“origin”] != r[“destination”]))
|> yield(name: “last”)

1 Like

Tres bien! Ca marche :hatched_chick::baby_chick:

here is the final step that works

origin multi select variable is generated like this

import “influxdata/influxdb/v1”
v1.tagValues(
bucket: v.bucket,
tag: “origin”,
predicate: (r) => true,
start: -1d
)

destination multi select variable is generated like this

import “influxdata/influxdb/v1”
v1.tagValues(
bucket: v.bucket,
tag: “destination”,
predicate: (r) => true,
start: -1d
)

the query that allows displaying only what is selected in the origin and destination variables is this one (big thanks to this post and its contributors → Grafana + InfluxDB Flux - query for displaying multi-select variable inputs - #7 by martinfalch)

from(bucket: “simdataset”)
|> range(start: -4d)
|> filter(fn: (r) => r[“_measurement”] == “$measurement”)
|> filter(fn: (r) => r[“_field”] == “waitingTime”)
|> filter(fn: (r) => contains(value: r[“origin”], set: ${origin:json}))
|> filter(fn: (r) => contains(value: r[“destination”], set: ${destination:json}))
|> yield(name: “last”)

2 Likes

and thanks to you yosiasz for your help :slight_smile:

1 Like