将两个 InfluxDB 行合并为一个 - Flux 查询

问题描述

嗨,我目前正在 R 中执行此操作,但想知道是否有办法在 Flux 中执行此操作:

我有一个跟踪值的时间序列,并且仅在信号打开和​​关闭时存储。问题是我正在跟踪的机器的性质只允许它以这种方式完成。这会产生一个数据表/测量结果,其中两行显示单个值(例如故障的开始和结束)。 如何使用flux查询数据以合并这两行?(以“开始”和“停止”作为标签/字段)

我目前使用 elapsed() 函数来计算我的价值的时间差/持续时间

time                      value     field          measurement    equipmentNumber    workplace    duration
2021-01-29 07:11:17.496   1         FAULT_LASER    FAULT_LASER    L5211M0855         0            188
2021-01-29 07:12:03.332   0         FAULT_LASER    FAULT_LASER    L5211M0855         0            45835
2021-01-29 07:12:19.618   1         FAULT_LASER    FAULT_LASER    L5211M0855         0            16285
2021-01-29 07:12:19.618   0         FAULT_LASER    FAULT_LASER    L5211M0855         0            161725

我目前正在 R 中执行此操作:

for(i in 1:nrow(df_f)){
  if(df_f[i,"duration"] > 0){
    df_fdur[i,"start"] <- df_f[i,"time"]
    df_fdur[i,"stop"] <- df_f[i+1,"type"] <- df_f[i,"value"]
    df_fdur[i,"duration"] <- df_f[i,"duration"]
    df_fdur[i,"workplace"] <- df_f[i,"workplace"]
    df_fdur[i,"equipmentNumber"] <- df_f[i,"equipmentNumber"]
  }
}

关于如何做到这一点的任何想法?

解决方法

这并没有直接解决问题,但解决了我正在解决的问题。也许它对其他人有用。祝您有美好的一天!

// Get all the data from the bucket filtered for FAULT_LASER
data = from(bucket: "plcview_4/autogen")
  |> range(start: 2021-01-29T00:00:00.000Z,stop: now())                // regular time range filter
  |> filter(fn: (r) => r._measurement == "FAULT_LASER")                 // filter for the measurement
  |> elapsed(unit: 1ms,timeColumn: "_time",columnName: "duration")    // calculate time difference between rows
  |> yield(name: "data")
// returns data tables for every unique set of tags (workplace and equipmentNumber)

// Filter for all "No-Fault-Values" and sum their durations
operational = data
  |> filter(fn: (r) => r._value == 0)   // filter for all rows where FAULT_LASER = 0 --> No Faults
  |> group()                            // group all data tables together
  |> sum(column: "duration")            // sum all the durations from all data tables 
  |> yield(name: "operational")

// Count the number of faults
nfaults = data
  |> filter(fn: (r) => r._value == 1)   // filter for all rows where FAULT_LASER = 1 --> Faults
  |> group()                            // group all data tables together
  |> count()                            // count the number of records
  |> yield(name: "nfaults")

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...