Custom Aggregate Window function that preserves timestamp from Selector


I am trying to create an aggregate flux query that windows data based on a timestamp and selects the min and max values of each window. While I understand that this is done by the aggregateWindow function and the min(), max() selectors, I would also like to preserve the timestamp of each of the min, max values I collect.

To this end, I have created a customAggregateWindow function defined as:

customAggregateWindow = (every, fn, column="_value", timeSrc="_time", timeDst="_time", tables=<-) =>
    |> window(every:every,  createEmpty:true)
    |> fn(column:column)
    |> keep(columns: [timeSrc, column])
    |> window(every: inf)

That I believe does exactly what I want.
The problem is that for smaller values of the every argument my window funtion is significantly slower than the base aggregateWindow function.

So my question is. Is there a more efficient way to do this that takes advantage of the inner workings of Influx better or this is my only solution?

Thank you very much

Hello @billstam12 ,
I love your approach but I don’t know if there is.

You could maybe run a task to periodically calculate these selector functions and save them in a new bucket? So you don’t have to query for different ranges/multiple window periods or use a custom agg window function?