1

I am doing a join in influx to get the first and last values for a interval and then getting the difference.

Preset = 600

FirstValues = from(bucket: "Historian/oneday")
  |> range(start: dashboardTime)
  |> filter(fn: (r) => 
    r._measurement == "TestMeasurement" and
    r._field =="Value"  and
    r.Loc=="TXS"
  )
 |>window(every:15m)
 |>first()


 LastValues = from(bucket: "Historian/oneday")
  |> range(start: dashboardTime)
  |> filter(fn: (r) => 
    r._measurement == "TestMeasurement" and
    r._field =="Value"  and
    r.Loc=="TXS"
  )
 |>window(every:15m)
 |>last()

 CombinedValues = join (
    tables:{first:FirstValues,last:LastValues},
    on:["_stop","_start"]
 )
 totaliser = CombinedValues
                |>map(fn: (r) => ({   
                    _time: r._start,
                    //Want to do this, r._value_first < r._value_last ? Preset : r._value_first
                    _value: r._value_first - r._value_last
                }))
 totaliser
 |>window(every:inf)

This works fine till the time difference returns a positive number.

But if the 1st value from join returns is less than 2nd value , I want to update that with a perset value.

Ex:

Preset = 600

totaliser = CombinedValues
                |>map(fn: (r) => ({   
                    _time: r._start,

                    //Want to do this, r._value_first < r._value_last ? Preset : r._value_first

                    _value: r._value_first - r._value_last
                }))
Simsons
  • 12,295
  • 42
  • 153
  • 269

1 Answers1

-2

Conditional logic is pretty new to Flux, but it does exist. Currently there is only if, else if, and else:

// Pattern
if <condition> then <action> else <alternative-action>

// Example
if color == "green" then "008000" else "ffffff"

But this will be expanded with time.

https://v2.docs.influxdata.com/v2.0/query-data/guides/conditional-logic/

Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • Thanks, we are using influx db 1.7.6. Not keen to update to 2.0 , on production, as it's in alpha stage. – Simsons May 21 '19 at 00:40
  • You can use Flux with 1.7 – Rawkode May 28 '19 at 14:38
  • 1
    If and else with flux 1.7 results error message, "Error: failed to compile query:type error 5:1-5:3: undefined identifier "if" " – Simsons May 28 '19 at 23:25
  • 1
    Hmm, this was added to Flux recently; but I thought it was available in 1.7.6. Please allow me to reach out to someone internally and find out :) – Rawkode May 31 '19 at 09:58