Help me understand a little

So i am new to EventStore and events CQRS basically.

I am confused on how to use projections do some math and send a http request to a 3rd party api with updated values.

If i understood it right, basically projection lets me do calculations, change the state to updated values and emit the output of new state.

The new output will be into a different stream so another app is already listening to the stream where it will do 3rd party api calls or update local db.

Please let me know if i understood this correctly.

Secondly, how can i obtain the previous state + current state? For example, i am working on a project where i need last hour results and i need to do some calculations to last hour to make current results.

How should i get the last hour results? I assume, it will be set in initial state? If so, how do i pull the initial state from database?

I am confused on how to achieve this, any help would be really appreciated.

Thank you

oblutu,

That’s how I understand things as well.
For your state question, could you include the previous hour state as part of the current state?
For example:

{
“previous” : {} //last hour
“current” : {}
}

If that is no good, then whatever receives the state, simply persists it (for example a SQL DB) and you can simple query it.

Not knowing much about your problem makes this trickier though. If the last hour is vital to working on the next hour, it suggests it wouldn’t be done in the read model.

Does your system have an aggregate?

Hello @steven.blair, thank you for your reply.

Basically i’m working on a bidding system for PPC clicks. Based on last hour impressions, clicks, sales, i’m calculating what next value bid is. So getting previous hour metrics is important.

Reports app, I fetch the metrics report from db. I do get these reports from 3rd party (Google Ads). So whenever a 3rd party fetch happens it’s an event so a single report is emitted.

Metrics app is listening to the reports emitted. I’m not sure where i should pass the last hour report.

I do have an aggregate for reports app. Should that be the place where i fetch last hour results and then make { “previous” : {} “current” : {} }? so metrics app will have both states to work with?

Yeah, that’s kinda where I was going with the previous and current.
I have a similar setup here on a project, and it worked out really well for us holding previous and current.
From there, you cna simply emit the event you need to based on that.

1 Like

one more question, so right now my reads are done via DB.
Since i’m using cqrs and eventstores, should the reads be from events as well?

I don’t know the answer for this.
We use projections in parts of our system which is essentially for building state for querying, but in almost all cases, we emit this to some other process that writes it to a DB.
The eventstore is better suite for writing the raw data, and emitting calculations etc that would be really useful to replay if you make changes (bugs etc)

1 Like

Thanks a lot for your help!

No problem.
Hope it’s been useful