Compute Cumulative Aggregation Across Buckets

Compute a cumulative aggregation across buckets using the accumulate() function with timeChart()

Query

logscale
timeChart(span=1000ms, function=sum(value))
| accumulate(sum(_sum, as=_accumulated_sum))

Introduction

The accumulate() function can be used to calculate running totals, averages, or other cumulative metrics over time or across a series of events. The accumulate() function applies an aggregation function cumulatively to a sequence of events. A common use case is to accumulate values across time intervals, such as days. This can be achieved by applying the accumulate() function after a bucket() or timeChart() function.

In this example, the accumulate() function is used with timeChart() to accumulate values across time intervals.

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

Example incoming data might look like this:

@timestampkeyvalue
1451606301001a5
1451606301500b6
1451606301701a1
1451606302001c2
1451606302201b6

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
    timeChart(span=1000ms, function=sum(value))

    Groups data into 1-second buckets over a 4-second period, sums the field value for each bucket and returns the results in a field named _sum. The result is displayed in a timechart.

  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
    | accumulate(sum(_sum, as=_accumulated_sum))

    Calculates a running total of the sums in the _sum field, and returns the results in a field named _accumulated_sum.

  4. Event Result set.

Summary and Results

The query is used to accumulate values across time intervals/buckets. The query is useful for tracking cumulative metrics or identifying trends in the data.

Sample output from the incoming example data:

_bucket_sum_accumulated_sum
145160630000000
14516063010001212
1451606302000820
1451606303000020

The timechart looks like this:

Timechart displaying accumulated aggregation across buckets