【问题标题】:Ternary operator in Influx fluxInflux Flux 中的三元算子
【发布时间】:2019-05-14 09:25:07
【问题描述】:

我在 influx 中做一个连接来获取一个区间的第一个和最后一个值,然后得到差异。

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)

这工作正常,直到时间差返回一个正数。

但是如果 join 返回的第一个值小于第二个值,我想用 perset 值更新它。

例如:

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
                }))

【问题讨论】:

  • 您的问题不清楚。您在寻找 Flux 或 InfluxQL 的解决方案吗?
  • @JörgWMittag,我正在寻找 Flux 中的解决方案。

标签: influxdb fluxlang


【解决方案1】:

条件逻辑对于 Flux 来说是相当新的,但它确实存在。目前只有ifelse ifelse

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

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

但这会随着时间的推移而扩大。

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

【讨论】:

  • 谢谢,我们使用的是 influx db 1.7.6。不热衷于在生产中更新到 2.0 ,因为它处于 alpha 阶段。
  • 你可以在 1.7 中使用 Flux
  • If and else with Flux 1.7 结果错误消息,“错误:无法编译查询:类型错误 5:1-5:3: 未定义标识符“if”“
  • 嗯,这是最近添加到 Flux 中的;但我认为它在 1.7.6 中可用。请允许我与内部人员联系并找出答案:)
猜你喜欢
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2016-07-19
  • 2016-06-04
  • 2018-11-14
相关资源
最近更新 更多