簡體   English   中英

InfluxDB 2.0 - Flux 查詢:如何對列求和並使用總和進行進一步計算

[英]InfluxDB 2.0 - Flux query: How to sum a column and use the sum for further calculations

我是通量查詢語言的新手(使用 Influx DB 2)並且找不到以下問題的解決方案:

我有改變真假值的數據: 在此處輸入圖像描述

通過使用 events.duration function,我能夠以秒為單位計算下一次更改的時間: 在此處輸入圖像描述

現在我想計算所有“錯誤”事件的總時間和時間,然后我想計算所有錯誤事件的百分比。 我嘗試了以下

import "contrib/tomhollingworth/events"

total = from(bucket: "********")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "********")
  |> filter(fn: (r) => r["Server"] == "********")
  |> filter(fn: (r) => r["_field"] == "********")
  |> filter(fn: (r) => r["DataNode"] == "********")

  |> events.duration(
    unit: 1s,
    columnName: "duration",
    timeColumn: "_time",
    stopColumn: "_stop"
)
  |> sum(column: "duration")

  |> yield(name: "total")

downtime = from(bucket: "********")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "********")
  |> filter(fn: (r) => r["Server"] == "********")
  |> filter(fn: (r) => r["_field"] == "********")
  |> filter(fn: (r) => r["DataNode"] == "********")

  |> events.duration(
    unit: 1s,
    columnName: "duration",
    timeColumn: "_time",
    stopColumn: "_stop"
  )
  |> pivot(rowKey:["_time"], columnKey: ["_value"], valueColumn: "duration")
  |> drop(columns: ["true"])
  |> sum(column: "false")
  |> yield(name: "downtime")

downtime_percentage = downtime.false / total.duration

有了這個我得到以下錯誤錯誤@44:23-44:31:預期{A with false:B}但發現[C]我也嘗試了一些變化,但無法讓它工作。

我想我弄錯了一些基本的東西,但我還想不通。 如果您需要更多信息,請與我們聯系。

我找到了解決我的問題的方法。 雖然我確信有一個更優雅的解決方案,但我在這里記錄了我的方式,也許它可以幫助某人,我們可以一起改進它。

 import "contrib/tomhollingworth/events"

//Set time window in seconds (based on selected time)
time_window = int(v:  v.timeRangeStart)/-1000000000

//Filter (IoT-)Data
data= from(bucket: "*******")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "*******")
  |> filter(fn: (r) => r["Server"] == "*******")
  |> filter(fn: (r) => r["Equipment"] == "*******")
  |> filter(fn: (r) => r["DataNode"] == "******")
  
  //Use events.duration to calculate the duration in seconds of each true/false event. 
  |> events.duration(
    unit: 1s,
    columnName: "duration",
    timeColumn: "_time",
    stopColumn: "_stop"
  )    

//Sum up the event times via "sum()" and save them as an array variable via "findColumn()". This is the only way to access the value later (As far as I know. please let me know if you know other ways!).
total_array = data
    |> sum(column: "duration")
    |> findColumn(
        fn: (key) => key._field == "*******",
        column: "duration",
    )

//Calculate "missing time" in seconds in the time window, because the first event in the time window is missing.
missing_time = time_window - total_array[0]

//Create an array with the first event to determine if it is true or false
first_value_in_window = data
    |> first()
    |> findColumn(
        fn: (key) => key._field == "*******",
        column: "_value",
    )

//Calculate the downtime by creating columns with the true and false values via pivot. Then sum up the column with the false values
downtime = data
    |> map(fn: (r) => ({ r with duration_percentage: float(v: r.duration)/float(v: time_window) }))
     |> pivot(rowKey:["_time"], columnKey: ["_value"], valueColumn: "duration_percentage")
    |> map( fn: (r) => ({r with 
    downtime: if exists r.false then 
            r.false
        else
            0.0    
    }))
    |> sum(column: "downtime")
   
//Create an array with the downtime so that this value can be accessed later on
downtime_array = downtime
    |> findColumn(
        fn: (key) => key._field == "PLS_Antrieb_laeuft",
        column: "downtime",
    )

//If the first value in the considered time window is true, then the remaining time in the time window (missing_time) was downtime. Write this value in the column "false_percentage_before_window".
//The total downtime is calculated from the previously calculated sum(downtime_array) and, if applicable, the downtime of the remaining time in the time window if the first value is true (first_value_in_window[0])
data
    |> map( fn: (r) => ({r with 
     false_percentage_before_window: if first_value_in_window[0] then 
            float(v: missing_time)/float(v: time_window)
         else
            0.0   
     }))
     |> map(fn: (r) => ({ r with _value:  (downtime_array[0] + r.false_percentage_before_window) * 100.00 }))
     |> first()
     |> keep(columns: ["_value"])
     |> yield(name: "Total Downtime")

此解決方案假定真/假事件僅交替發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM