How to create a view which aggregates data from different (all) entities?

Let’s say I have an entity which generates event which contains numeric data (temperatures).
I want to create a view which shows the maximum/minimum of those values, of all entities (not per entity).
If I make a view like this:

rpc ProcessTemperatureAdded(TemperaturesRegistered) returns (Records) {
    option (akkaserverless.method) = {
      event_sourced_entity: "temperature"
    option(akkaserverless.method).view.update = {
      table: "temperatureRecords"
      transform_updates: true

And a query:

rpc GetRecords(google.protobuf.Empty) returns (stream Records) {
    option (akkaserverless.method).view.query = {
      query: "SELECT * from temperatureRecords"
    option (google.api.http) = {
      get: "/temperature/records"

A Records message looks like this:

message Records {
  TemperatureRecord maxTemperature = 1;
  TemperatureRecord minTemperature = 2;
message TemperatureRecord {
  string entity_id = 1;
  double measuredTemperature = 2;
  google.protobuf.Timestamp measurementTime = 3;
  TemperatureRecord previousRecord = 4;

The result is a list of Records, containing the min/max per entity, while I want a single ‘Records’ which contains the minimum and maximum over all entities.
The events are, as it seems, scoped per entity-id, while I actually want to create a aggregation accross all entities.
How should I proceed to implement this?

My suggestion would be to separate aggregation functionality to a separate service - Aggregation service.

Aggregation service would model entities around aggregation and would build views based on that. Entity key would be used to identify aggregation.

Process temperature service would then publish events and the aggregation service would subscribe.

Would that fit your use case?

I understand, thanks. This would fit the usecase.
I have considered introducing a separate entity for this, but I hoped to be able to do it with a view. All things considered, such ‘max’ and ‘min’ aggregation is in fact nothing more than a projection of the temperature-events (of all entities). I think in Lagom it can be done with a single readside-processor on the same events.
But anyway thanks for the suggestion!

I agree, with Lagom using single readside processor (1 event shard - tag), this would work but if you want to have multiple event shards then consistency becomes a challenge with parallel processing of events.
In AkkaServerless i would assume multiple event shards (under the hood).

1 Like