From abf9a913e009341076ea893af87d810f2bbd7c4d Mon Sep 17 00:00:00 2001 From: salam Date: Sat, 12 Sep 2026 11:46:36 +0200 Subject: [PATCH] wip --- app/Models/SofaScorePlayerPerSeason.php | 1 + app/Traits/UpdateHelper.php | 431 +++++++++--------- ...te_sofa_score_player_per_seasons_table.php | 1 + 3 files changed, 218 insertions(+), 215 deletions(-) diff --git a/app/Models/SofaScorePlayerPerSeason.php b/app/Models/SofaScorePlayerPerSeason.php index f1fed7f7..59f22f4f 100644 --- a/app/Models/SofaScorePlayerPerSeason.php +++ b/app/Models/SofaScorePlayerPerSeason.php @@ -25,6 +25,7 @@ class SofaScorePlayerPerSeason extends Model 'offsides', 'shots_on_target', 'shots_off_target', + 'total_shots', ]; public function details() diff --git a/app/Traits/UpdateHelper.php b/app/Traits/UpdateHelper.php index 7d22f262..0d50224c 100644 --- a/app/Traits/UpdateHelper.php +++ b/app/Traits/UpdateHelper.php @@ -6,11 +6,8 @@ use App\Models\SofaScorePlayerPerSeason; use App\Models\StatistikoDetail; use App\Models\StatistikoPlayer; use App\Utilities\Notice; -use Illuminate\Support\Facades\Concurrency; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -use PDO; -use PDOStatement; use function Laravel\Prompts\select; @@ -172,6 +169,7 @@ trait UpdateHelper $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]['total_shots'] += $val->shots_off_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; @@ -185,6 +183,7 @@ trait UpdateHelper $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]['total_shots'] += $val->shots_off_target + $val->shots_off_target + $val->shots_blocked; } } } @@ -216,6 +215,7 @@ trait UpdateHelper 'offsides', 'shots_on_target', 'shots_off_target', + 'total_shots', 'cards', ] ); @@ -239,254 +239,255 @@ trait UpdateHelper $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]['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(); + // 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, - ]; + // 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)), ','); + // 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; + // $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)); - } + // // 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 = []; - } - } + // $pdo->prepare($sql)->execute($values); + // $data = []; + // } + // } - if (! empty($data)) { - $columns = array_keys($data[0]); - $placeholders = rtrim(str_repeat('(?,?,?,?,?,?,?,?,?),', count($data)), ','); + // if (! empty($data)) { + // $columns = array_keys($data[0]); + // $placeholders = rtrim(str_repeat('(?,?,?,?,?,?,?,?,?),', count($data)), ','); - $sql = 'INSERT INTO customers ('.implode(',', $columns).') VALUES '.$placeholders; + // $sql = 'INSERT INTO customers ('.implode(',', $columns).') VALUES '.$placeholders; - $values = []; - foreach ($data as $row) { - $values = array_merge($values, array_values($row)); - } + // $values = []; + // foreach ($data as $row) { + // $values = array_merge($values, array_values($row)); + // } - $pdo->prepare($sql)->execute($values); - } + // $pdo->prepare($sql)->execute($values); + // } - fclose($handle); - } + // 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 + // 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 (?, ?, ?, ?, ?, ?, ?, ?, ?) - '); + // 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); - } - } + // 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 = []; + // 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); + // 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, - ]); + // 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 = []; - } - } + // 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); - } - } + // // 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; + // 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(); + // $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 = []; + // $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; - } + // 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, - ]; + // $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 (count($customers) === 1000) { + // DB::table('customers')->insert($customers); + // $customers = []; + // } + // } - if (! empty($customers)) { - DB::table('customers')->insert($customers); - } + // if (! empty($customers)) { + // DB::table('customers')->insert($customers); + // } - fclose($handle); + // fclose($handle); - return true; - }; - } + // return true; + // }; + // } - Concurrency::run($tasks); + // 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); + // 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); + // $filepath = str_replace('\\', '/', $filePath); - $query = <<exec($query); - } + // $pdo->exec($query); + // } - private function prepareChunkedStatement($chunkSize): PDOStatement - { - $rowPlaceholders = '(?, ?, ?, ?, ?, ?, ?, ?, ?)'; - $placeholders = implode(',', array_fill(0, $chunkSize, $rowPlaceholders)); + // 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} - "); - } + // return DB::connection()->getPdo()->prepare(" + // INSERT INTO customers (custom_id, name, email, company, city, country, birthday, created_at, updated_at) + // VALUES {$placeholders} + // "); + // } } diff --git a/database/migrations/2026_06_27_091506_create_sofa_score_player_per_seasons_table.php b/database/migrations/2026_06_27_091506_create_sofa_score_player_per_seasons_table.php index 98de4172..bffb15d7 100644 --- a/database/migrations/2026_06_27_091506_create_sofa_score_player_per_seasons_table.php +++ b/database/migrations/2026_06_27_091506_create_sofa_score_player_per_seasons_table.php @@ -29,6 +29,7 @@ return new class extends Migration $table->integer('offsides')->default(0)->nullable(); $table->integer('shots_on_target')->default(0)->nullable(); $table->integer('shots_off_target')->default(0)->nullable(); + $table->integer('total_shots')->default(0)->nullable(); $table->unique(['ss_player_id', 'ss_team_id', 'season']); $table->timestamps(); });