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

Fix event timing #3954

Merged
merged 4 commits into from
Dec 3, 2023
Merged

Fix event timing #3954

merged 4 commits into from
Dec 3, 2023

Conversation

dorner
Copy link
Collaborator

@dorner dorner commented Dec 1, 2023

This updates event calculations so that it processes them by event time, but still groups them by updated at, so we are ensured to get the "latest version" of the event that happened, e.g. if it was edited after the fact.

This also includes a migration to fix the event times of the known editable objects.

Marking this as draft because I want to add a test for the times. Coming tomorrow hopefully.

@dorner dorner requested a review from cielf December 1, 2023 20:12
@dorner dorner marked this pull request as ready for review December 3, 2023 01:49
events = Event.for_organization(organization_id)
if first_event
events = events.where("id >= ?", first_event)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should these go off of one of the timestamps instead of id? It will probably work using id most of the time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Honestly I don't have a good use for this just yet, it's for the eventual "start from last snapshot" behavior. We can double check if once that's in.

inventory = EventTypes::Inventory.from(organization_id)
events.group_by { |e| [e.eventable_type, e.eventable_id] }.each do |_, event_batch|
last_event = event_batch.max_by(&:event_time)
handle(last_event, inventory)
last_grouped_event = event_batch.max_by(&:updated_at)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Conceptually, will there ever be events that have a created_at != updated_at?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope.

@@ -4,7 +4,7 @@ def self.publish(adjustment)
create(
eventable: adjustment,
organization_id: adjustment.organization_id,
event_time: Time.zone.now,
event_time: adjustment.created_at,
Copy link
Collaborator

Choose a reason for hiding this comment

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

The passed in adjustment from AdjustmentCreateService, weirdly, could be an exisiting Adjustment rather than a freshly created one. Maybe so this gets a bit weird with that case; I'm not sure if adjustment.updated_at would be better or not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adjustments are never updated, just created.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK for this one, I guess AdjustmentCreateService initialize we should sometime take out the first bit where it can be handed an Adjustment -- I verified that nothing does call it with one.

@@ -6,7 +6,7 @@ def self.publish(audit)
create(
eventable: audit,
organization_id: audit.organization_id,
event_time: Time.zone.now,
event_time: audit.updated_at,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah -- this one uses updated_at and others use created_at for the eventable it's tracking, how did you intend which is which?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because audits have a state (they have to be finalized) and they can't be edited, so updated_at is the correct thing. For the others, they can be edited but they are always "valid", so created_at is correct.

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.

We talked through some other details on the call -- looks good.

@awwaiid awwaiid merged commit 43eb795 into main Dec 3, 2023
12 checks passed
@awwaiid awwaiid deleted the event-fixes branch December 3, 2023 15:29
Copy link
Contributor

github-actions bot commented Dec 3, 2023

@dorner: Your PR Fix event timing is part of today's Human Essentials production release: 2023.12.03.
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