diff --git a/app/Filament/Widgets/PlayerPropsDrop.php b/app/Filament/Widgets/PlayerPropsDrop.php index 1cf82403..6ccfab9b 100644 --- a/app/Filament/Widgets/PlayerPropsDrop.php +++ b/app/Filament/Widgets/PlayerPropsDrop.php @@ -389,7 +389,7 @@ class PlayerPropsDrop extends Widget $team_home_away = 'away'; } - $player_totals = SofaScorePlayerPerSeason::with('details')->where('total_min', '>', 0)->where('season', 'total')->where('ss_team_id', $team_id)->get(); + $player_totals = SofaScorePlayerPerSeason::with('details')->where('total_min', '>', 0)->where('season', 'last_180_days')->where('ss_team_id', $team_id)->get(); $count_player = 0; foreach ($player_totals as $key => $value) { diff --git a/app/Traits/UpdateHelper.php b/app/Traits/UpdateHelper.php index 06a5a8d7..6d95071e 100644 --- a/app/Traits/UpdateHelper.php +++ b/app/Traits/UpdateHelper.php @@ -134,6 +134,13 @@ trait UpdateHelper $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); @@ -141,6 +148,9 @@ trait UpdateHelper 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) { @@ -148,11 +158,20 @@ trait UpdateHelper $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; @@ -186,6 +205,23 @@ trait UpdateHelper $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; + } } } } @@ -246,252 +282,4 @@ trait UpdateHelper $collection[$season][$ss_player_id]['total_shots'] = 0; $collection[$season][$ss_player_id]['cards'] = 0; } - - // private function import08ManualStreamingWithPdo(string $filePath): void - // { - // // 100 7ms / 0MB - // // 1K 78ms / 0MB - // // 10K 328ms / 0MB - // // 100K 2.9s / 0MB - // // 1M 28s / 0MB - // $data = []; - // $handle = fopen($filePath, 'rb'); - // fgetcsv($handle); // skip header - // $now = now()->format('Y-m-d H:i:s'); - // $pdo = DB::connection()->getPdo(); - - // while (($row = fgetcsv($handle)) !== false) { - // $data[] = [ - // 'custom_id' => $row[0], - // 'name' => $row[1], - // 'email' => $row[2], - // 'company' => $row[3], - // 'city' => $row[4], - // 'country' => $row[5], - // 'birthday' => $row[6], - // 'created_at' => $now, - // 'updated_at' => $now, - // ]; - - // if (count($data) === 1000) { - // // Build the SQL query for the chunk - // $columns = array_keys($data[0]); - // $placeholders = rtrim(str_repeat('(?,?,?,?,?,?,?,?,?),', count($data)), ','); - - // $sql = 'INSERT INTO customers ('.implode(',', $columns).') VALUES '.$placeholders; - - // // Flatten the data array for the query - // $values = []; - // foreach ($data as $row) { - // $values = array_merge($values, array_values($row)); - // } - - // $pdo->prepare($sql)->execute($values); - // $data = []; - // } - // } - - // if (! empty($data)) { - // $columns = array_keys($data[0]); - // $placeholders = rtrim(str_repeat('(?,?,?,?,?,?,?,?,?),', count($data)), ','); - - // $sql = 'INSERT INTO customers ('.implode(',', $columns).') VALUES '.$placeholders; - - // $values = []; - // foreach ($data as $row) { - // $values = array_merge($values, array_values($row)); - // } - - // $pdo->prepare($sql)->execute($values); - // } - - // fclose($handle); - // } - - // private function import09PDOPrepared(string $filePath): void - // { - // // Direct database connection with prepared statements - // // 100 41ms / 0 MB - // // 1K 237ms / - // // 10K 2.21s - // // 100K 25.27s - // // 1M 4m43s - // // 2M - // $now = now()->format('Y-m-d H:i:s'); - // $handle = fopen($filePath, 'r'); - // fgets($handle); // skip header - - // try { - // $pdo = DB::connection()->getPdo(); - // $stmt = $pdo->prepare(' - // INSERT INTO customers (custom_id, name, email, company, city, country, birthday, created_at, updated_at) - // VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) - // '); - - // while (($row = fgetcsv($handle)) !== false) { - // $stmt->execute([ - // $row[0], - // $row[1], - // $row[2], - // $row[3], - // $row[4], - // $row[5], - // $row[6], - // $now, - // $now, - // ]); - // } - // } finally { - // fclose($handle); - // } - // } - - // private function import10PDOPreparedChunked(string $filePath): void - // { - // // Direct database connection with prepared statements - // // 100 12ms / 0.15MB - // // 1K 49ms / 0.74MB - // // 10K 222ms / 0.74MB - // // 100K 1.5s / 0.74MB - // // 1M 15.3s / 0.74MB - // // 2M 24s / 0.74MN - // $now = now()->format('Y-m-d H:i:s'); - // $handle = fopen($filePath, 'r'); - // fgetcsv($handle); // skip header - // $chunkSize = 500; - // $chunks = []; - - // try { - // $stmt = $this->prepareChunkedStatement($chunkSize); - - // while (($row = fgetcsv($handle)) !== false) { - // $chunks = array_merge($chunks, [ - // $row[0], $row[1], $row[2], $row[3], $row[4], - // $row[5], $row[6], $now, $now, - // ]); - - // if (count($chunks) === $chunkSize * 9) { // 9 columns - // $stmt->execute($chunks); - // $chunks = []; - // } - // } - - // // Handle remaining records - // if (! empty($chunks)) { - // $remainingRows = count($chunks) / 9; - // $stmt = $this->prepareChunkedStatement($remainingRows); - // $stmt->execute($chunks); - // } - // } finally { - // fclose($handle); - // } - // } - - // private function import11Concurrent(string $filePath): void - // { - // // 100 168ms - // // 1K 172ms - // // 10K 234ms - // // 100K 595ms - // // 1M 4.36s - // // 2M 8.8s - // $now = now()->format('Y-m-d H:i:s'); - // $numberOfProcesses = 10; - - // $tasks = []; - // for ($i = 0; $i < $numberOfProcesses; $i++) { - // $tasks[] = function () use ($filePath, $i, $numberOfProcesses, $now) { - // DB::reconnect(); - - // $handle = fopen($filePath, 'r'); - // fgets($handle); // Skip header - // $currentLine = 0; - // $customers = []; - - // while (($line = fgets($handle)) !== false) { - // // Each process takes every Nth line - // if ($currentLine++ % $numberOfProcesses !== $i) { - // continue; - // } - - // $row = str_getcsv($line); - // $customers[] = [ - // 'custom_id' => $row[0], - // 'name' => $row[1], - // 'email' => $row[2], - // 'company' => $row[3], - // 'city' => $row[4], - // 'country' => $row[5], - // 'birthday' => $row[6], - // 'created_at' => $now, - // 'updated_at' => $now, - // ]; - - // if (count($customers) === 1000) { - // DB::table('customers')->insert($customers); - // $customers = []; - // } - // } - - // if (! empty($customers)) { - // DB::table('customers')->insert($customers); - // } - - // fclose($handle); - - // return true; - // }; - // } - - // Concurrency::run($tasks); - - // } - - // private function import12LoadDataInfile(string $filePath): void - // { - // // MySQL specific, fastest approach - // // 100 10ms / 0MB - // // 1K 29ms / 0MB - // // 10K 115ms / 0MB - // // 100K 567ms / 0MB - // // 1M 5s / 0MB - // // 2M 11s / 0MB - // $pdo = DB::connection()->getPdo(); - // $pdo->setAttribute(PDO::MYSQL_ATTR_LOCAL_INFILE, true); - - // $filepath = str_replace('\\', '/', $filePath); - - // $query = <<exec($query); - // } - - // private function prepareChunkedStatement($chunkSize): PDOStatement - // { - // $rowPlaceholders = '(?, ?, ?, ?, ?, ?, ?, ?, ?)'; - // $placeholders = implode(',', array_fill(0, $chunkSize, $rowPlaceholders)); - - // return DB::connection()->getPdo()->prepare(" - // INSERT INTO customers (custom_id, name, email, company, city, country, birthday, created_at, updated_at) - // VALUES {$placeholders} - // "); - // } }