Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Source pt. 2: Inventory Discrepancies #3917

Merged
merged 2 commits into from
Nov 5, 2023
Merged

Event Source pt. 2: Inventory Discrepancies #3917

merged 2 commits into from
Nov 5, 2023

Conversation

dorner
Copy link
Collaborator

@dorner dorner commented Oct 22, 2023

See #3911 (this needs to be split up into additional issues).

Description

This adds a new model, InventoryDiscrepancy. Whenever an event is published, the calculated event sourced inventory is checked against the current database-backed inventory. If there are any differences, these are saved in an InventoryDiscrepancy object. For now this will live in a table, which we can pull down and inspect manually. We might want to add a UI in the superadmin area of the app to inspect this.

As part of this PR, we are also adding user_ids to events, piggybacking off of PaperTrail to fetch this information.

belongs_to :user, optional: true

before_create do
self.user_id = PaperTrail.request&.whodunnit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, as a mechanism to get the current user, I see. Seems fine.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there are other things to tie to, like a request id that goes into the logs. Other stuff to make tracing of what went wrong easier

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking into that... I can try to add more metadata to PaperTrail, perhaps. Otherwise you need to mess with thread variables and it gets messy. The truth is that the operation itself should give us most if not everything that we need - there are very few ways (usually one) that any individual event can be created. If we find some pain dealing with the current way, we can always add more info later.

# Used to indicate that a storage location exists in one source but not the other.
class LocationDiff < Dry::Struct
attribute :storage_location_id, Types::Integer
attribute :database, Types::Bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there is a different or more explicit name for this. Like... in_transactional_model or in_stateful_model or something. Food for thought

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm definitely down with brainstorming better names... the most explicit thing I could think of is "this is the thing that lives in the database" and "this is the thing that gets built via an aggregate". I'm not sure about something like "stateful" (an aggregate is state, it's just derived state) or transactional (things can live in the database without relying on transactions).

class LocationDiff < Dry::Struct
attribute :storage_location_id, Types::Integer
attribute :database, Types::Bool
attribute :aggregate, Types::Bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar for these, maybe in_event_aggregate

self.user_id = PaperTrail.request&.whodunnit
end

after_create_commit do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is appropriate for this to immediately identify and record issues when they happen, but flagging they we have to beware of the resulting slowdown. For individual actions I don't think we'd ever notice, but we should be on the lookout for batch operations. Also once there is a diff then we'll always have one (for the org/location) and this'll find things every time unless the problems cancel each other out (a finance friend of mine calls this "The God of Offsets").

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we definitely should monitor if there's a huge drop in efficiency. I do want to put in a followup PR that relies more heavily on snapshots (which should be done more often) which should speed this up.

And yeah I know that the diffs will keep piling on. The goal is not for this to be a permanent feature, but for us to monitor over time and verify that the events are the accurate interpretation (and to do fixes until they are). Once we're confident in that, we switch over to using them as the source of truth and the diffs no longer need to happen.

Copy link
Collaborator

@awwaiid awwaiid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GREAT!!!!!!!

@awwaiid awwaiid merged commit 9e5f776 into main Nov 5, 2023
12 checks passed
@awwaiid awwaiid deleted the event-source-pt2 branch November 5, 2023 15:42
Copy link
Contributor

github-actions bot commented Nov 5, 2023

@dorner: Your PR/Issue Event Source pt. 2: Inventory Discrepancies is part of today's Human Essentials production release: 2023.11.05.
Thank you very much for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants