Learn how to aggregate data from the series.

# Aggregators

Aggregators allows you to perform calculations on top of the whole series. They plug seamelessly into `Series`

and can be used through `dot`

notation.

For example you want to know the number of events that happened during the specific timeframe. Of course you can calculate that manually with some form of reduce, but you can also use build in `Trifle::Stats`

methods to calculate this.

```
series = Trifle::Stats.series(...)
=> #<Trifle::Stats::Series:0x0000ffffa14256e8 @series={:at=>[2024-03-22 19:38:00 +0000, 2024-03-22 19:39:00 +0000], :values=>[{events: {count: 42, sum: 2184}}, {events: {count: 33, sum: 1553}}]}>
series.aggregate.sum(path: 'events.count')
=> 75
```

Note:

`path`

is a list of keys joined by dot. Ie`orders.shipped.count`

would represent value at`{orders: { shipped: { count: ... } } }`

.

## Custom Aggregators

You can use one of predefined aggregators or write your own. Aggregator needs to implement at least one method that accepts key parameters `series:`

and `path:`

and returns *something*. The `series:`

is passed in automatically.

```
class MyAggregator
def aggregate(series:, path:)
# perform some calculation on top of series
rand(1000)
end
end
```

If you write your own aggregator and would like to access it through `dot`

notation, you need to register it through `Trifle::Stats::Series.register_aggregator(NAME, self)`

inside of your aggregator class.

```
class MyAggregator
Trifle::Stats::Series.register_aggregator(:rand, self)
def aggregate(series:, path:)
# perform some calculation on top of series
rand(1000)
end
end
```

And from there you can access it through `series.aggregate.rand(path: 'a.count')`