Phillipp

Phillipp

Complex Rating system with Commanded (CQRS/ES)

Hey,

I am actually interested in Event Sourcing for literally years but haven’t used it yet. Last night, I read through the guides in the Commanded documentation and watched the linked 20 minutes talk on Youtube.
The provided examples start to make sense and the overall picture becomes more clear.

Now, I have a project idea in mind for a very long time, which involves a rating system. I knew right from the start that this rating system would be a good opportunity to use Event Sourcing. However, this rating system will be a little bit more complicated in terms of aggregation and business rules.

Let me show you the concept:

The following will be my logical structure:

- parent
--- child
------category_a
------category_b
------category_c
--- category_1
--- category_2
--- category_3
  • parent and child are the actual things I want to be rated.
  • a child has a dynamic list of categories to be rated on, that can change over time, so they have just an identifier, e.g. usability (%RateCategory{subject: "id-of-child", name: "usability", score: 8})
  • a parent also has a dynamic list of categories to be rated, same as child
  • some parents are not ratable, depending on their type
  • the overall rating of a child is a combination of all category ratings for it
  • if a parent is ratable, it has two overall ratings
    • one overall rating as a result of combining its category ratings
    • another rating that combines the overall rating of its children

Here comes the tricky part:

I want to have the concept of rating periods. I am not sure yet how long a period should be, but as soon as one period ends, another starts immediately. This is to track “progress” over time.
In each rating period, a user can rate only once.
Let’s say, there is a rating period every month. In January, a user can rate all categories of a parent and child. Then, in February, the ratings will be “reset” and the user can rate for everything again.

The goal is to track the ratings of a parent and child over time to see if things got better or worse.


So, that was my overall idea for the rating system. Everything else in the system will be done the “usual” way.

What I would like to get from you are some strategies to achieve this. Since I am just starting to get into the ES mindset, I am a bit lost as to how to implement this right now.

First Post!

factoryd

factoryd

I’m a bit drunk right now. And I’m probably not reading this right. lol

And since I’m drunk and bored, I threw up a start app for you. I had all aspirations of understanding what you’re after. But nope.

I hope this sets you off onto the right path.

https://drive.google.com/file/d/1iiY6A1uHfCeoZiCxLVGm2wo_lJF9jwaQ/view?usp=sharing

Most Liked

slashdotdash

slashdotdash

It sounds like you want to implement your entities’ lifecycle to be within a time period. You can take inspiration from accounting where a ledger is for a given period (e.g. one year) and at the end of each year the ledger is closed. When this occurs the ending balances from the ledger are copied across into a new ledger as the starting balances.

You could design your entities’ event streams to be monthly and at the end of each month have a scheduled command end the rating period and raise a corresponding domain event. This can be used to start the next period:

  1. Whenever a rating period is started, it schedules an EndRatingPeriod command to be executed at the end of the month.
  2. The scheduled EndRatingPeriod command gets automatically dispatched at the end of the month.
  3. This produces a RatingPeriodEnded event.
  4. Which triggers a StartRatingPeriod command to begin the next month’s ratings

Include whatever data you need from the rating period in the period ended event so that it can be included in the command to be copied into the new period. For example you might just copy the average rating per category, not each individual rating.

Commanded scheduler can be used to do the above command scheduling (either using an event handler or process manager).

By splitting the entities into time-delimited event streams (e.g. monthly) you can use read model projections to build aggregated views of the ratings over time. The benefit of this approach is you won’t have unbounded event streams for your entities and limit the number of events within each stream.

slashdotdash

slashdotdash

Yves Reynhout gave a good talk at Explore DDD in 2017 which covers splitting aggregate event streams by time (appointment scheduling product for healthcare).

Last Post!

Phillipp

Phillipp

Using an extra RatingPeriod aggregrate was also one thing I had in my mind, I just wasn’t sure if this was the correct thing to do, but it gives me the most flexibility I think, because I can end a rating period whenever I want and seamlessly move from 1 month to 1 week if I want and still retain the old 1 month cycles for history.

So, let me dump my thoughts in here:

  1. I have a RatingPeriod aggregate with a minimum of id, start_date, end_date, is_active fields.
  2. I have Parent and Child aggregates whose identity is a compound value consisting of the parent/child id and the RatingPeriod id. Probably using a custom struct like in the documentation example for custom identities.
  3. When I want to commit a new rating, I first fetch the current active rating period from my read model and include its id in the rating command and event.

That should give me all I need to properly structure a read model. I aim at having one table row per parent/child entity and rating period with a json column for the categories and its rating counts.

I will also watch the talk you linked. Maybe I missed something but right now it starts to make sense and takes shape in my head.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement