Skip to content

Save synced Live Results in LiveResults model#13261

Open
FinnIckler wants to merge 13 commits intothewca:mainfrom
FinnIckler:sync-to-live-results
Open

Save synced Live Results in LiveResults model#13261
FinnIckler wants to merge 13 commits intothewca:mainfrom
FinnIckler:sync-to-live-results

Conversation

@FinnIckler
Copy link
Member

opening in draft so I can check it out from my laptop

@FinnIckler FinnIckler marked this pull request as ready for review January 27, 2026 12:58
@FinnIckler
Copy link
Member Author

Fixing the second test threw some questions at me. What it does is create "wrong" live results, meaning the rankings are just not correct. That's why I had to LiveResult.skip_callback(:create, :after, :recompute_local_pos) to not overwrite the columns again.

Really not sure what the best way to handle that is

@FinnIckler
Copy link
Member Author

I do have a solution but it won't be super pretty

@FinnIckler FinnIckler force-pushed the sync-to-live-results branch from 2d993cb to 4b5def2 Compare January 27, 2026 15:08
@FinnIckler
Copy link
Member Author

This approach also now handles cases with empty attempts

@FinnIckler
Copy link
Member Author

I wonder, should we just create these results as locked and then we don't have to bother with this complicated insertion

{ event_id: event_id, round_number: round_number }
end

def load_live_results(results_wcif)
Copy link
Member

Choose a reason for hiding this comment

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

Per the fact that this uses insert_all! internally, you should also call this method load_live_results!

Copy link
Member

Choose a reason for hiding this comment

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

As far as I can tell, the ! has not made it into the method name yet

Copy link
Member Author

Choose a reason for hiding this comment

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

the error is eaten by the transaction block though?

Copy link
Member

Choose a reason for hiding this comment

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

If it is, then that's probably not good... At least not for this specific scenario.

"advancementCondition" => advancement_condition&.to_wcif,
"scrambleSetCount" => self.scramble_set_count,
"results" => round_results.map(&:to_wcif),
"results" => live_results.map(&:to_wcif),
Copy link
Member

Choose a reason for hiding this comment

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

Since you're now accessing an actual table relation (instead of just a field that was arbitrarily serialized into one column) you should probably look out for includes calls around the code, at least in the API controller that directly serves WCIF, and add live_results at the appropriate spot in there

{ event_id: event_id, round_number: round_number }
end

def load_live_results(results_wcif)
Copy link
Member

Choose a reason for hiding this comment

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

As far as I can tell, the ! has not made it into the method name yet

def load_live_results(results_wcif)
self.transaction do
live_results.destroy_all
registrations_by_wcif_id = competition.registrations.index_by(&:registrant_id)
Copy link
Member

Choose a reason for hiding this comment

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

Are the registrations prt of the includes in the controller method of the PATCH endpoint?

Comment on lines +401 to +402
LiveAttempt.build_with_history_entry(attempt["result"], attempt_number, 1)
.attributes
Copy link
Member

Choose a reason for hiding this comment

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

I am 99% certain that calling attributes here entirely discards the _with_history_entry part. You're building a proper AR model class with a history entry correctly attached as a child, but calling attributes turns the parent LiveAttempt record into a pure hash which (obviously) does not understand anything about the concept of "associations".

Comment on lines 405 to 406
created_at: now,
updated_at: now,
Copy link
Member

Choose a reason for hiding this comment

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

See my other comment about time not needing to be passed in

}
end

LiveResult.insert_all!(results_to_load)
Copy link
Member

Choose a reason for hiding this comment

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

Should this really be an insert? Feels more like an upsert...

Copy link
Member Author

Choose a reason for hiding this comment

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

we just destroyed all live results, so this should be an insert

Copy link
Member

Choose a reason for hiding this comment

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

Okay then (a) Is there a reason why we destroy + insert instead of upserting? (b) If the answer to A is Yes, then move the destroy_all directly towards the insert_all along with a comment explaining your thinking around question A

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