Exercise 2: Reconstructing an Order Count Over a Threshold, Purely From the Log — Possible Solution ==================================================================== THE NEW REPLAY FUNCTION ------------------------------ def replay_orders_over(event_log, threshold): count = 0 for event_type, payload in event_log.events: if event_type == 'OrderPlaced' and payload['items_total'] > threshold: count += 1 return count Follows this chapter's own replay_total_revenue() shape exactly - a single pass over event_log.events, filtering by event_type, with no separate counter maintained anywhere outside this one function call. VERIFYING AGAINST THIS CHAPTER'S OWN THREE-EVENT LOG ------------------------------ Using the same three OrderPlaced events this chapter's own replay_total_revenue() example used (items_total of 100, 50, and 75), called with threshold=60: orders over 60: 2 manual check: 100>60 and 75>60, 50 is not -> 2 100 and 75 both exceed 60; 50 does not - the function correctly counts 2, matching a manual check of the same three values. WHY THIS CONFIRMS EVENT SOURCING'S OWN GENERALITY ------------------------------ This chapter's own replay_total_revenue() answered one specific question (what's the total?) by replaying the log. This exercise answers a COMPLETELY DIFFERENT question (how many orders exceeded a threshold?) using the exact same log, with no changes to how events were originally published or stored. This is the real payoff of event sourcing: the log doesn't need to anticipate every question that will ever be asked of it in advance - new replay functions can answer new questions against data that's already there. WHY THIS WORKS AS AN ANSWER ------------------------------ The new function mirrors this chapter's own established replay pattern exactly, and it's verified against the identical event data this chapter's own example used, with the result cross-checked by a manual count rather than only asserted correct.