From 5503dc7ba0c27a46066a5e560a262e1ed0e3c7f4 Mon Sep 17 00:00:00 2001 From: salam Date: Sun, 11 Oct 2026 20:18:24 +0200 Subject: [PATCH] run precalculation via action puntton with quer:work, send mail when finished --- .../Pages/ListStatistikoImports.php | 7 +- app/Traits/UpdateHelper.php | 410 ++++++++++++------ 2 files changed, 280 insertions(+), 137 deletions(-) diff --git a/app/Filament/Clusters/StaticData/Resources/StatistikoImports/Pages/ListStatistikoImports.php b/app/Filament/Clusters/StaticData/Resources/StatistikoImports/Pages/ListStatistikoImports.php index a14518b6..0e7e85c1 100644 --- a/app/Filament/Clusters/StaticData/Resources/StatistikoImports/Pages/ListStatistikoImports.php +++ b/app/Filament/Clusters/StaticData/Resources/StatistikoImports/Pages/ListStatistikoImports.php @@ -10,7 +10,6 @@ use Filament\Actions\CreateAction; use Filament\Notifications\Notification; use Filament\Resources\Pages\ListRecords; use Illuminate\Support\Facades\Artisan; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\File; class ListStatistikoImports extends ListRecords @@ -25,12 +24,8 @@ class ListStatistikoImports extends ListRecords ->requiresConfirmation() // Optional: Asks user before running ->action(function () { // Run the console command - /** @var User|null $user */ - $user = Auth::user(); - Artisan::queue('update:player', [ - '--userId' => $user->id(), - ]); + Artisan::queue('update:player'); // Notify the user of success Notification::make() diff --git a/app/Traits/UpdateHelper.php b/app/Traits/UpdateHelper.php index deb3096c..0772e751 100644 --- a/app/Traits/UpdateHelper.php +++ b/app/Traits/UpdateHelper.php @@ -2,11 +2,13 @@ namespace App\Traits; +use App\Mail\SimpleMail; use App\Models\SofaScorePlayerPerSeason; use App\Models\StatistikoDetail; use App\Models\StatistikoPlayer; use App\Utilities\Notice; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Mail; use Illuminate\Support\Str; use function Laravel\Prompts\select; @@ -114,151 +116,297 @@ trait UpdateHelper protected function updatePlayers(int $playerCount) { - $res_player = StatistikoDetail::query()->where('is_active', 1)->limit($playerCount)->pluck('ss_player_id')->toArray(); + StatistikoDetail::chunk(500, function ($details) { + foreach ($details as $chunk) { + $res = StatistikoPlayer::with('event')->with('details')->whereIn('ss_player_id', $chunk)->orderBy('ss_player_id')->get(); - $chunks = array_chunk($res_player, 500); + $collection = []; + foreach ($res as $val) { - foreach ($chunks as $chunk) { + if (isset($val->event)) { - $res = StatistikoPlayer::with('event')->with('details')->whereIn('ss_player_id', $chunk)->orderBy('ss_player_id')->get(); - - $collection = []; - foreach ($res as $val) { - - if (isset($val->event)) { - - $ss_player_id = (int) $val->ss_player_id; - $activeteam_id = (int) $val->activeteam_id; - $minutes_played = (int) $val->minutes_played; - // $ss_team_id = (int) $val->details->ss_team_id; - $season = $val->event->season; - $home_id = (int) $val->event->home_id; - $away_id = (int) $val->event->away_id; - $startime = $val->event->starttime; - - if ($startime->greaterThan(now()->subDays(180))) { - $last_180_days = true; - } else { - $last_180_days = false; - } - - if (! isset($collection[$season][$ss_player_id])) { - $this->array_template($collection, $ss_player_id, $season); - } - if (! isset($collection['total'][$ss_player_id])) { - $this->array_template($collection, $ss_player_id, 'total'); - } - if (! isset($collection['last_180_days'][$ss_player_id])) { - $this->array_template($collection, $ss_player_id, 'last_180_days'); - } - - if ($minutes_played > 0) { - if ($activeteam_id == $home_id) { - $collection[$season][$ss_player_id]['events_played_home'] += 1; - $collection[$season][$ss_player_id]['minutes_played_home'] += $minutes_played; - $collection['total'][$ss_player_id]['events_played_home'] += 1; - $collection['total'][$ss_player_id]['minutes_played_home'] += $minutes_played; - if ($last_180_days) { - $collection['last_180_days'][$ss_player_id]['events_played_home'] += 1; - $collection['last_180_days'][$ss_player_id]['minutes_played_home'] += $minutes_played; - } - } elseif ($activeteam_id == $away_id) { - $collection[$season][$ss_player_id]['events_played_away'] += 1; - $collection[$season][$ss_player_id]['minutes_played_away'] += $minutes_played; - $collection['total'][$ss_player_id]['events_played_away'] += 1; - $collection['total'][$ss_player_id]['minutes_played_away'] += $minutes_played; - if ($last_180_days) { - $collection['last_180_days'][$ss_player_id]['events_played_away'] += 1; - $collection['last_180_days'][$ss_player_id]['minutes_played_away'] += $minutes_played; - } + $ss_player_id = (int) $val->ss_player_id; + $activeteam_id = (int) $val->activeteam_id; + $minutes_played = (int) $val->minutes_played; + // $ss_team_id = (int) $val->details->ss_team_id; + $season = $val->event->season; + $home_id = (int) $val->event->home_id; + $away_id = (int) $val->event->away_id; + $startime = $val->event->starttime; + if ($startime->greaterThan(now()->subDays(180))) { + $last_180_days = true; + } else { + $last_180_days = false; } - // ubaldo 18. Sep 26 intanto vado avanti, appena trovo qwuaqlcosa ti scrivo, tu togli i red cards dal conteggio - $cards = $val->card_yellow + $val->card_yellowred; - $collection[$season][$ss_player_id]['ss_player_id'] = $ss_player_id; - // $collection[$season][$ss_player_id]['ss_team_id'] = $ss_team_id; - $collection[$season][$ss_player_id]['season'] = $season; - $collection[$season][$ss_player_id]['total_events']++; - $collection[$season][$ss_player_id]['total_min'] += $minutes_played; - $collection[$season][$ss_player_id]['average_min'] = round($collection[$season][$ss_player_id]['total_min'] / $collection[$season][$ss_player_id]['total_events']); - $collection[$season][$ss_player_id]['goals'] += $val->goals; - $collection[$season][$ss_player_id]['fouls'] += $val->fouls; - $collection[$season][$ss_player_id]['cards'] += $cards; - $collection[$season][$ss_player_id]['offsides'] += $val->offsides; - $collection[$season][$ss_player_id]['shots_on_target'] += $val->shots_on_target; - $collection[$season][$ss_player_id]['shots_off_target'] += $val->shots_off_target; - $collection[$season][$ss_player_id]['shots_blocked'] += $val->shots_blocked; - $collection[$season][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + if (! isset($collection[$season][$ss_player_id])) { + $this->array_template($collection, $ss_player_id, $season); + } + if (! isset($collection['total'][$ss_player_id])) { + $this->array_template($collection, $ss_player_id, 'total'); + } + if (! isset($collection['last_180_days'][$ss_player_id])) { + $this->array_template($collection, $ss_player_id, 'last_180_days'); + } - $collection['total'][$ss_player_id]['ss_player_id'] = $ss_player_id; - // $collection['total'][$ss_player_id]['ss_team_id'] = $ss_team_id; - $collection['total'][$ss_player_id]['season'] = 'total'; - $collection['total'][$ss_player_id]['total_events']++; - $collection['total'][$ss_player_id]['total_min'] += $minutes_played; - $collection['total'][$ss_player_id]['average_min'] = round($collection['total'][$ss_player_id]['total_min'] / $collection['total'][$ss_player_id]['total_events']); - $collection['total'][$ss_player_id]['goals'] += $val->goals; - $collection['total'][$ss_player_id]['fouls'] += $val->fouls; - $collection['total'][$ss_player_id]['cards'] += $cards; - $collection['total'][$ss_player_id]['offsides'] += $val->offsides; - $collection['total'][$ss_player_id]['shots_on_target'] += $val->shots_on_target; - $collection['total'][$ss_player_id]['shots_off_target'] += $val->shots_off_target; - $collection['total'][$ss_player_id]['shots_blocked'] += $val->shots_blocked; - $collection['total'][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + if ($minutes_played > 0) { + if ($activeteam_id == $home_id) { + $collection[$season][$ss_player_id]['events_played_home'] += 1; + $collection[$season][$ss_player_id]['minutes_played_home'] += $minutes_played; + $collection['total'][$ss_player_id]['events_played_home'] += 1; + $collection['total'][$ss_player_id]['minutes_played_home'] += $minutes_played; + if ($last_180_days) { + $collection['last_180_days'][$ss_player_id]['events_played_home'] += 1; + $collection['last_180_days'][$ss_player_id]['minutes_played_home'] += $minutes_played; + } + } elseif ($activeteam_id == $away_id) { + $collection[$season][$ss_player_id]['events_played_away'] += 1; + $collection[$season][$ss_player_id]['minutes_played_away'] += $minutes_played; + $collection['total'][$ss_player_id]['events_played_away'] += 1; + $collection['total'][$ss_player_id]['minutes_played_away'] += $minutes_played; + if ($last_180_days) { + $collection['last_180_days'][$ss_player_id]['events_played_away'] += 1; + $collection['last_180_days'][$ss_player_id]['minutes_played_away'] += $minutes_played; + } - if ($last_180_days) { - $collection['last_180_days'][$ss_player_id]['ss_player_id'] = $ss_player_id; - // $collection['last_180_days'][$ss_player_id]['ss_team_id'] = $ss_team_id; - $collection['last_180_days'][$ss_player_id]['season'] = 'last_180_days'; - $collection['last_180_days'][$ss_player_id]['total_events']++; - $collection['last_180_days'][$ss_player_id]['total_min'] += $minutes_played; - $collection['last_180_days'][$ss_player_id]['average_min'] = round($collection['last_180_days'][$ss_player_id]['total_min'] / $collection['last_180_days'][$ss_player_id]['total_events']); - $collection['last_180_days'][$ss_player_id]['goals'] += $val->goals; - $collection['last_180_days'][$ss_player_id]['fouls'] += $val->fouls; - $collection['last_180_days'][$ss_player_id]['cards'] += $cards; - $collection['last_180_days'][$ss_player_id]['offsides'] += $val->offsides; - $collection['last_180_days'][$ss_player_id]['shots_on_target'] += $val->shots_on_target; - $collection['last_180_days'][$ss_player_id]['shots_off_target'] += $val->shots_off_target; - $collection['last_180_days'][$ss_player_id]['shots_blocked'] += $val->shots_blocked; - $collection['last_180_days'][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + } + // ubaldo 18. Sep 26 intanto vado avanti, appena trovo qwuaqlcosa ti scrivo, tu togli i red cards dal conteggio + $cards = $val->card_yellow + $val->card_yellowred; + + $collection[$season][$ss_player_id]['ss_player_id'] = $ss_player_id; + // $collection[$season][$ss_player_id]['ss_team_id'] = $ss_team_id; + $collection[$season][$ss_player_id]['season'] = $season; + $collection[$season][$ss_player_id]['total_events']++; + $collection[$season][$ss_player_id]['total_min'] += $minutes_played; + $collection[$season][$ss_player_id]['average_min'] = round($collection[$season][$ss_player_id]['total_min'] / $collection[$season][$ss_player_id]['total_events']); + $collection[$season][$ss_player_id]['goals'] += $val->goals; + $collection[$season][$ss_player_id]['fouls'] += $val->fouls; + $collection[$season][$ss_player_id]['cards'] += $cards; + $collection[$season][$ss_player_id]['offsides'] += $val->offsides; + $collection[$season][$ss_player_id]['shots_on_target'] += $val->shots_on_target; + $collection[$season][$ss_player_id]['shots_off_target'] += $val->shots_off_target; + $collection[$season][$ss_player_id]['shots_blocked'] += $val->shots_blocked; + $collection[$season][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + + $collection['total'][$ss_player_id]['ss_player_id'] = $ss_player_id; + // $collection['total'][$ss_player_id]['ss_team_id'] = $ss_team_id; + $collection['total'][$ss_player_id]['season'] = 'total'; + $collection['total'][$ss_player_id]['total_events']++; + $collection['total'][$ss_player_id]['total_min'] += $minutes_played; + $collection['total'][$ss_player_id]['average_min'] = round($collection['total'][$ss_player_id]['total_min'] / $collection['total'][$ss_player_id]['total_events']); + $collection['total'][$ss_player_id]['goals'] += $val->goals; + $collection['total'][$ss_player_id]['fouls'] += $val->fouls; + $collection['total'][$ss_player_id]['cards'] += $cards; + $collection['total'][$ss_player_id]['offsides'] += $val->offsides; + $collection['total'][$ss_player_id]['shots_on_target'] += $val->shots_on_target; + $collection['total'][$ss_player_id]['shots_off_target'] += $val->shots_off_target; + $collection['total'][$ss_player_id]['shots_blocked'] += $val->shots_blocked; + $collection['total'][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + + if ($last_180_days) { + $collection['last_180_days'][$ss_player_id]['ss_player_id'] = $ss_player_id; + // $collection['last_180_days'][$ss_player_id]['ss_team_id'] = $ss_team_id; + $collection['last_180_days'][$ss_player_id]['season'] = 'last_180_days'; + $collection['last_180_days'][$ss_player_id]['total_events']++; + $collection['last_180_days'][$ss_player_id]['total_min'] += $minutes_played; + $collection['last_180_days'][$ss_player_id]['average_min'] = round($collection['last_180_days'][$ss_player_id]['total_min'] / $collection['last_180_days'][$ss_player_id]['total_events']); + $collection['last_180_days'][$ss_player_id]['goals'] += $val->goals; + $collection['last_180_days'][$ss_player_id]['fouls'] += $val->fouls; + $collection['last_180_days'][$ss_player_id]['cards'] += $cards; + $collection['last_180_days'][$ss_player_id]['offsides'] += $val->offsides; + $collection['last_180_days'][$ss_player_id]['shots_on_target'] += $val->shots_on_target; + $collection['last_180_days'][$ss_player_id]['shots_off_target'] += $val->shots_off_target; + $collection['last_180_days'][$ss_player_id]['shots_blocked'] += $val->shots_blocked; + $collection['last_180_days'][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + } } } } - } - $collection_1d = []; - foreach ($collection as $arr) { - foreach ($arr as $row) { - $collection_1d[] = $row; + $collection_1d = []; + foreach ($collection as $arr) { + foreach ($arr as $row) { + $collection_1d[] = $row; + } } - } - SofaScorePlayerPerSeason::upsert( - $collection_1d, - uniqueBy: [ - 'ss_player_id', - // 'ss_team_id', - 'season', - ], - update: [ - 'total_events', - 'total_min', - 'average_min', - 'events_played_home', - 'events_played_away', - 'minutes_played_home', - 'minutes_played_away', - 'goals', - 'fouls', - 'offsides', - 'shots_on_target', - 'shots_off_target', - 'shots_blocked', - 'total_shots', - 'cards', - ] - ); - } + SofaScorePlayerPerSeason::upsert( + $collection_1d, + uniqueBy: [ + 'ss_player_id', + // 'ss_team_id', + 'season', + ], + update: [ + 'total_events', + 'total_min', + 'average_min', + 'events_played_home', + 'events_played_away', + 'minutes_played_home', + 'minutes_played_away', + 'goals', + 'fouls', + 'offsides', + 'shots_on_target', + 'shots_off_target', + 'shots_blocked', + 'total_shots', + 'cards', + ] + ); + } + }); + + Mail::to(['hermann.telser@gmail.com', 'ubaldo.perrucci@gmail.com'])->send(new SimpleMail('STATISTIKO', 'Pre Calculation Completed')); + + // ###################### + // $res_player = StatistikoDetail::query()->where('is_active', 1)->limit($playerCount)->pluck('ss_player_id')->toArray(); + + // $chunks = array_chunk($res_player, 500); + + // foreach ($chunks as $chunk) { + + // $res = StatistikoPlayer::with('event')->with('details')->whereIn('ss_player_id', $chunk)->orderBy('ss_player_id')->get(); + + // $collection = []; + // foreach ($res as $val) { + + // if (isset($val->event)) { + + // $ss_player_id = (int) $val->ss_player_id; + // $activeteam_id = (int) $val->activeteam_id; + // $minutes_played = (int) $val->minutes_played; + // // $ss_team_id = (int) $val->details->ss_team_id; + // $season = $val->event->season; + // $home_id = (int) $val->event->home_id; + // $away_id = (int) $val->event->away_id; + // $startime = $val->event->starttime; + + // if ($startime->greaterThan(now()->subDays(180))) { + // $last_180_days = true; + // } else { + // $last_180_days = false; + // } + + // if (! isset($collection[$season][$ss_player_id])) { + // $this->array_template($collection, $ss_player_id, $season); + // } + // if (! isset($collection['total'][$ss_player_id])) { + // $this->array_template($collection, $ss_player_id, 'total'); + // } + // if (! isset($collection['last_180_days'][$ss_player_id])) { + // $this->array_template($collection, $ss_player_id, 'last_180_days'); + // } + + // if ($minutes_played > 0) { + // if ($activeteam_id == $home_id) { + // $collection[$season][$ss_player_id]['events_played_home'] += 1; + // $collection[$season][$ss_player_id]['minutes_played_home'] += $minutes_played; + // $collection['total'][$ss_player_id]['events_played_home'] += 1; + // $collection['total'][$ss_player_id]['minutes_played_home'] += $minutes_played; + // if ($last_180_days) { + // $collection['last_180_days'][$ss_player_id]['events_played_home'] += 1; + // $collection['last_180_days'][$ss_player_id]['minutes_played_home'] += $minutes_played; + // } + // } elseif ($activeteam_id == $away_id) { + // $collection[$season][$ss_player_id]['events_played_away'] += 1; + // $collection[$season][$ss_player_id]['minutes_played_away'] += $minutes_played; + // $collection['total'][$ss_player_id]['events_played_away'] += 1; + // $collection['total'][$ss_player_id]['minutes_played_away'] += $minutes_played; + // if ($last_180_days) { + // $collection['last_180_days'][$ss_player_id]['events_played_away'] += 1; + // $collection['last_180_days'][$ss_player_id]['minutes_played_away'] += $minutes_played; + // } + + // } + // // ubaldo 18. Sep 26 intanto vado avanti, appena trovo qwuaqlcosa ti scrivo, tu togli i red cards dal conteggio + // $cards = $val->card_yellow + $val->card_yellowred; + + // $collection[$season][$ss_player_id]['ss_player_id'] = $ss_player_id; + // // $collection[$season][$ss_player_id]['ss_team_id'] = $ss_team_id; + // $collection[$season][$ss_player_id]['season'] = $season; + // $collection[$season][$ss_player_id]['total_events']++; + // $collection[$season][$ss_player_id]['total_min'] += $minutes_played; + // $collection[$season][$ss_player_id]['average_min'] = round($collection[$season][$ss_player_id]['total_min'] / $collection[$season][$ss_player_id]['total_events']); + // $collection[$season][$ss_player_id]['goals'] += $val->goals; + // $collection[$season][$ss_player_id]['fouls'] += $val->fouls; + // $collection[$season][$ss_player_id]['cards'] += $cards; + // $collection[$season][$ss_player_id]['offsides'] += $val->offsides; + // $collection[$season][$ss_player_id]['shots_on_target'] += $val->shots_on_target; + // $collection[$season][$ss_player_id]['shots_off_target'] += $val->shots_off_target; + // $collection[$season][$ss_player_id]['shots_blocked'] += $val->shots_blocked; + // $collection[$season][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + + // $collection['total'][$ss_player_id]['ss_player_id'] = $ss_player_id; + // // $collection['total'][$ss_player_id]['ss_team_id'] = $ss_team_id; + // $collection['total'][$ss_player_id]['season'] = 'total'; + // $collection['total'][$ss_player_id]['total_events']++; + // $collection['total'][$ss_player_id]['total_min'] += $minutes_played; + // $collection['total'][$ss_player_id]['average_min'] = round($collection['total'][$ss_player_id]['total_min'] / $collection['total'][$ss_player_id]['total_events']); + // $collection['total'][$ss_player_id]['goals'] += $val->goals; + // $collection['total'][$ss_player_id]['fouls'] += $val->fouls; + // $collection['total'][$ss_player_id]['cards'] += $cards; + // $collection['total'][$ss_player_id]['offsides'] += $val->offsides; + // $collection['total'][$ss_player_id]['shots_on_target'] += $val->shots_on_target; + // $collection['total'][$ss_player_id]['shots_off_target'] += $val->shots_off_target; + // $collection['total'][$ss_player_id]['shots_blocked'] += $val->shots_blocked; + // $collection['total'][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + + // if ($last_180_days) { + // $collection['last_180_days'][$ss_player_id]['ss_player_id'] = $ss_player_id; + // // $collection['last_180_days'][$ss_player_id]['ss_team_id'] = $ss_team_id; + // $collection['last_180_days'][$ss_player_id]['season'] = 'last_180_days'; + // $collection['last_180_days'][$ss_player_id]['total_events']++; + // $collection['last_180_days'][$ss_player_id]['total_min'] += $minutes_played; + // $collection['last_180_days'][$ss_player_id]['average_min'] = round($collection['last_180_days'][$ss_player_id]['total_min'] / $collection['last_180_days'][$ss_player_id]['total_events']); + // $collection['last_180_days'][$ss_player_id]['goals'] += $val->goals; + // $collection['last_180_days'][$ss_player_id]['fouls'] += $val->fouls; + // $collection['last_180_days'][$ss_player_id]['cards'] += $cards; + // $collection['last_180_days'][$ss_player_id]['offsides'] += $val->offsides; + // $collection['last_180_days'][$ss_player_id]['shots_on_target'] += $val->shots_on_target; + // $collection['last_180_days'][$ss_player_id]['shots_off_target'] += $val->shots_off_target; + // $collection['last_180_days'][$ss_player_id]['shots_blocked'] += $val->shots_blocked; + // $collection['last_180_days'][$ss_player_id]['total_shots'] += $val->shots_on_target + $val->shots_off_target + $val->shots_blocked; + // } + // } + // } + // } + + // $collection_1d = []; + // foreach ($collection as $arr) { + // foreach ($arr as $row) { + // $collection_1d[] = $row; + // } + // } + + // SofaScorePlayerPerSeason::upsert( + // $collection_1d, + // uniqueBy: [ + // 'ss_player_id', + // // 'ss_team_id', + // 'season', + // ], + // update: [ + // 'total_events', + // 'total_min', + // 'average_min', + // 'events_played_home', + // 'events_played_away', + // 'minutes_played_home', + // 'minutes_played_away', + // 'goals', + // 'fouls', + // 'offsides', + // 'shots_on_target', + // 'shots_off_target', + // 'shots_blocked', + // 'total_shots', + // 'cards', + // ] + // ); + // } } private function array_template(array &$collection, int $ss_player_id, string $season)