将两个 InfluxDB 行合并为一个 - Flux 查询
Combine two InfluxDB rows into one - Flux query
嗨,我目前正在 R 中执行此操作,但想知道我是否可以在 Flux 中执行此操作:
我有一个跟踪值的时间序列,仅在信号打开和关闭时存储。问题是我正在跟踪的机器的性质只允许以这种方式完成。这会导致数据 table/measurement,其中两行显示单个值(例如故障的开始和结束)。 如何使用通量查询数据以合并这两行?(“开始”和“停止”为 tags/fields)
我目前使用elapsed()
-函数来计算我的值difference/duration的时间
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, "time"]
df_fdur[i, "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")
嗨,我目前正在 R 中执行此操作,但想知道我是否可以在 Flux 中执行此操作:
我有一个跟踪值的时间序列,仅在信号打开和关闭时存储。问题是我正在跟踪的机器的性质只允许以这种方式完成。这会导致数据 table/measurement,其中两行显示单个值(例如故障的开始和结束)。 如何使用通量查询数据以合并这两行?(“开始”和“停止”为 tags/fields)
我目前使用elapsed()
-函数来计算我的值difference/duration的时间
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, "time"]
df_fdur[i, "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")