Calculate Sum of Field Values Over Sliding Time-Based Window

Calculate a sum of values in a dataset over a sliding time-based window using the slidingTimeWindow() function

Query

logscale
head()
| slidingTimeWindow(sum(value), span=3s)

Introduction

The slidingTimeWindow() function can be used to calculate metrics over a specific time period. The slidingTimeWindow() function analyzes data within a moving time frame and performs calculation on the data within that time period. As time progresses, it updates the calculations, always using the most recent time period.

In this example, the slidingTimeWindow() function is used with the head() function to calculate the sum of the field value over a sliding time-based window of 3 seconds.

Note that the slidingTimeWindow() function must be used after an aggregator function to ensure event ordering by time. Also note that the events must be sorted in order by timestamp to prevent errors when running the query. It is possible to select any field to use as a timestamp.

Example incoming data might look like this:

value@timestamp
11451606301001
41451606301002
111451606302400
21451606304001
51451606304003
11451606305300

Step-by-Step

  1. Starting with the source repository events.

  2. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0{{Aggregate}} 1{{Aggregate}} result{{Result Set}} repo --> 0 0 --> 1 1 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    head()

    Selects the oldest events ordered by time.

  3. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0{{Aggregate}} 1{{Aggregate}} result{{Result Set}} repo --> 0 0 --> 1 1 --> result style 1 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    | slidingTimeWindow(sum(value), span=3s)

    Computes the running sum of the field value over a 3-second window using the slidingTimeWindow() function with the sum() aggregator. It adds the values of this 3-second window together and updates the sum as new data points arrive, always considering only the last 3 seconds.

    It is also possible to exclude the current value, if adding current="exclude": slidingTimeWindow(sum(value), span="3s", current="exclude")

    Note that it is possible to select any field to use as a timestamp, which can be useful after the bucket function.

  4. Event Result set.

Summary and Results

The query is used to calculate the sum of fields over a sliding time-based window using a 3 second time window. The query is useful to identify trends in most recent data.

Sample output from the incoming example data:

_sum@timestampvalue
114516063010011
514516063010024
16145160630240011
1714516063040012
1814516063040035
1914516063053001

Sample output from the incoming example data if the current event value is excluded:

_sum@timestampvalue
014516063010011
114516063010024
5145160630240011
1514516063040012
1314516063040035
1814516063053001

To analyze data based on a set number of data points instead of time periods, use the slidingWindow() function.