Calculate Sum of Field Values Over Sliding Window

Calculate a sum of values in a dataset over a sliding window of a number of events using the slidingWindow() function

Query

logscale
head()
| slidingWindow(sum(value), events=3)

Introduction

The slidingWindow() function can be used to calculate cumulative metrics over a fixed number of recent events, allowing for trend analysis and smoothing of data. The slidingWindow() function applies an aggregation function to a moving window of a specified number of events in a sequence. As new data arrives, the slidingWindow() function adds the newest data point to its analysis and removes the oldest data point from its analysis, recalculating results based on this updated set of data points.

In this example, the slidingWindow() function is used with the sum() function to calculate the sum of the field value over a sliding window of 3 events.

Note that the slidingWindow() function must be used after an aggregator function to ensure event ordering.

Example incoming data might look like this:

value
1
4
11
2
5
1

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
    | slidingWindow(sum(value), events=3)

    Computes the running sum of the field value in the three most recent events using the slidingWindow() function with the sum() aggregator. It adds the values of these three data points together and updates the calculation as new data points arrive, always using the latest three. It is also possible to exclude the current value, if adding current="exclude": slidingWindow(sum(value), events=3, current="exclude")

  4. Event Result set.

Summary and Results

This query calculates the sum of fields over a sliding window of events. The query is useful to identify trends in most recent data.

Sample output from the incoming example data:

_sumvalue
11
54
1611
172
185
81

Sample output from the incoming example data if excluded:

_sumvalue
01
14
511
162
175
181

You could, for example, use slidingWindow() to calculate the average response time for the last 100 customer service requests, updating this average as each new request comes in. To analyze data based on time periods instead of a set number of data points, use the slidingTimeWindow() function.