-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor region validation scope from topology #44
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,7 @@ sub validate_region_payload { | |
| my ($pick_map, $parse_errors) = _extract_pick_map($params); | ||
| return { ok => 0, errors => $parse_errors } if @{$parse_errors}; | ||
|
|
||
| my $min_game = 1 + 15 * ($region_id - 1); | ||
| my $max_game = 15 + 15 * ($region_id - 1); | ||
| my $region_game_ids = _region_game_ids_for_region($schema, $region_id); | ||
|
|
||
| my @errors; | ||
| my %existing = map { $_->game->id => $_->pick->id } | ||
|
|
@@ -24,7 +23,7 @@ sub validate_region_payload { | |
|
|
||
| my @changed_games; | ||
| foreach my $game_id (sort { $a <=> $b } keys %{$pick_map}) { | ||
| if ($game_id < $min_game || $game_id > $max_game) { | ||
| if (!$region_game_ids->{$game_id}) { | ||
| push @errors, "Game ${game_id} is outside region ${region_id}"; | ||
| next; | ||
| } | ||
|
|
@@ -36,7 +35,7 @@ sub validate_region_payload { | |
| \@changed_games, | ||
| sub { | ||
| my ($game_id) = @_; | ||
| return $game_id >= $min_game && $game_id <= $max_game; | ||
| return $region_game_ids->{$game_id}; | ||
| } | ||
| ); | ||
|
|
||
|
|
@@ -129,6 +128,41 @@ sub _extract_pick_map { | |
| return (\%pick_map, \@errors); | ||
| } | ||
|
|
||
| sub _region_game_ids_for_region { | ||
| my ($schema, $region_id) = @_; | ||
|
|
||
| my $region_winner_games = Bracket::Service::BracketStructure->region_winner_games_by_region($schema); | ||
| my $region_winner_game_id = $region_winner_games->{$region_id}; | ||
| return _fallback_region_game_ids($region_id) if !$region_winner_game_id; | ||
|
|
||
| my %parents_by_game; | ||
| foreach my $edge ($schema->resultset('GameGraph')->search({})->all) { | ||
| my $game_id = $edge->game; | ||
| my $parent_game_id = $edge->parent_game; | ||
| next if !defined $game_id || !defined $parent_game_id; | ||
|
|
||
| push @{$parents_by_game{$game_id}}, $parent_game_id; | ||
| } | ||
|
|
||
| my %allowed_games; | ||
| my @queue = ($region_winner_game_id); | ||
| while (@queue) { | ||
| my $game_id = shift @queue; | ||
| next if $allowed_games{$game_id}++; | ||
| push @queue, @{$parents_by_game{$game_id} || []}; | ||
| } | ||
|
|
||
| return \%allowed_games; | ||
| } | ||
|
|
||
| sub _fallback_region_game_ids { | ||
| my ($region_id) = @_; | ||
|
|
||
| my $min_game = 1 + 15 * ($region_id - 1); | ||
| my $max_game = 15 + 15 * ($region_id - 1); | ||
| return { map { $_ => 1 } ($min_game .. $max_game) }; | ||
| } | ||
|
Comment on lines
+158
to
+164
|
||
|
|
||
| sub _affected_games_in_scope { | ||
| my ($schema, $changed_games, $is_in_scope) = @_; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validate_region_payload now triggers a full
GameGraph->search({})->allscan in _region_game_ids_for_region, and then _affected_games_in_scope performs another full scan of GameGraph. Consider reusing a single precomputed edge list / parent-child maps (e.g., pass it into _affected_games_in_scope or cache per call) to avoid duplicate DB reads on each validation request.