options(); $this->debug = $tmp['d'] ?? 0; if ($this->debug) { Notice::note('Update Player Season', 'g'); } if ($this->debug) { $playerCount = $this->selectPlayer(); } else { $playerCount = 1000000; } if ($this->debug) { $this->startBenchmark(); } try { $this->updatePlayers($playerCount); } catch (\Exception $e) { $this->error(get_class($e).' '.Str::of($e->getMessage())->limit(100)->value()); } if ($this->debug) { $this->endBenchmark(); } } // Flight::upsert([ // ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99], // ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150] // ], uniqueBy: ['departure', 'destination'], update: ['price']); protected function selectPlayer(): string { $file = select( label: 'How many players to update?', options: ['100 Player', '1k Player', '10k Player', '100k Player', '1m Player'] ); return match ($file) { '100 Player' => 100, '1k Player' => 1000, '10k Player' => 10000, '100k Player' => 100000, '1m Player' => 1000000, }; } protected function startBenchmark(string $table = 'ss_player_seasons'): void { $this->startRowCount = DB::table($table)->count(); $this->benchmarkStartTime = microtime(true); $this->benchmarkStartMemory = memory_get_usage(); DB::enableQueryLog(); $this->startQueries = DB::select("SHOW SESSION STATUS LIKE 'Questions'")[0]->Value; } protected function endBenchmark(string $table = 'ss_player_seasons'): void { $executionTime = microtime(true) - $this->benchmarkStartTime; $memoryUsage = round((memory_get_usage() - $this->benchmarkStartMemory) / 1024 / 1024, 2); $queriesCount = DB::select("SHOW SESSION STATUS LIKE 'Questions'")[0]->Value - $this->startQueries - 1; // Subtract the Questions query itself // Get row count after we've stopped tracking queries $rowDiff = DB::table($table)->count() - $this->startRowCount; $formattedTime = match (true) { $executionTime >= 60 => sprintf('%dm %ds', floor($executionTime / 60), $executionTime % 60), $executionTime >= 1 => round($executionTime, 2).'s', default => round($executionTime * 1000).'ms', }; $this->newLine(); $this->line(sprintf( '⚡ TIME: %s MEM: %sMB SQL: %s ROWS: %s ', $formattedTime, $memoryUsage, number_format($queriesCount), number_format($rowDiff) )); $this->newLine(); } protected function updatePlayers(int $playerCount) { $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) { $collection[$season][$ss_player_id]['ss_player_id'] = 0; $collection[$season][$ss_player_id]['ss_team_id'] = 0; $collection[$season][$ss_player_id]['season'] = ''; $collection[$season][$ss_player_id]['total_events'] = 0; $collection[$season][$ss_player_id]['total_min'] = 0; $collection[$season][$ss_player_id]['average_min'] = 0; $collection[$season][$ss_player_id]['events_played_home'] = 0; $collection[$season][$ss_player_id]['events_played_away'] = 0; $collection[$season][$ss_player_id]['minutes_played_home'] = 0; $collection[$season][$ss_player_id]['minutes_played_away'] = 0; $collection[$season][$ss_player_id]['goals'] = 0; $collection[$season][$ss_player_id]['fouls'] = 0; $collection[$season][$ss_player_id]['offsides'] = 0; $collection[$season][$ss_player_id]['shots_on_target'] = 0; $collection[$season][$ss_player_id]['shots_off_target'] = 0; $collection[$season][$ss_player_id]['shots_blocked'] = 0; $collection[$season][$ss_player_id]['total_shots'] = 0; $collection[$season][$ss_player_id]['cards'] = 0; } }