0

I have a function which takes the 1st value from a masurement every 5mins.

ever5Mins1st = from(bucket: "Historian/oneday")
  |> range(start: dashboardTime)
  |> filter(fn: (r) => 
    r._measurement == "InventoryStock" and
    r._field =="Value"  and
    r.Location=="NYC"
  )
 |>window(every:5m)
 |>first()
 |>window(every:inf)

Now If I want to do a difference of two consecutive points , I can do by using difference function

 ever5Mins1st
 |>difference()

But what If I do I want a sum of every consecutive points or every 5 points.

I know I can write custom functions which recieves piped data. But do I need to write a for loop? Are there any examples of for loops and conditional statements?

// Function definition
add = (tables=<-) =>
  tables
    |> map(fn: (r) => //What should I do here, for loop?)
Simsons
  • 12,295
  • 42
  • 153
  • 269

2 Answers2

0

I don't believe this is possible at the moment. This would require a chunk function, to split a list into chunks of lists.

I'll add an issue to Flux and start to implement this functionality :+1:

Rawkode
  • 21,990
  • 5
  • 38
  • 45
0

I can't help you solve this problem in flux, but it's possible to solve using Kaskada.

With Kaskada, your original query would look something like this:

Historian 
| when(Historian._measurement = "InventoryStock" and
       Historian._field = "value" and 
       Historial.Location = "NYC")
| first(window=since(minutely()))

This produces a timeline containing the first Historian event matching the three predicates each minute.

To compute the difference of consecutive events:

let first_each_minute = Historian 
| when(Historian._measurement = "InventoryStock" and
       Historian._field = "value" and 
       Historial.Location = "NYC")
| first(window=since(minutely()))

in first_each_minute - lag(1, first_each_minute)

To compute a cumulative sum:

let first_each_minute = Historian 
| when(Historian._measurement = "InventoryStock" and
       Historian._field = "value" and 
       Historial.Location = "NYC")
| first(window=since(minutely()))
| sum()

To compute a sum of 5 consecutive points

let first_each_minute = Historian 
| when(Historian._measurement = "InventoryStock" and
       Historian._field = "value" and 
       Historial.Location = "NYC")
| first(window=since(minutely()))
| sum(window=sliding(5))

Kaskada is designed to reason about time, so this type of problem is what it's ideally suited for.

Disclaimer: I am a contributor to the Kaskada project.

Kerinin
  • 171
  • 8