From 8cd0e7ee370d9b70e8db874c19422514a8399b78 Mon Sep 17 00:00:00 2001 From: Mathias Grimm Date: Fri, 13 Mar 2026 19:10:53 -0300 Subject: [PATCH 1/5] mathiasgrimm attempt #1 --- app/Parser.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/app/Parser.php b/app/Parser.php index b74cf7b9f7..556e062b80 100644 --- a/app/Parser.php +++ b/app/Parser.php @@ -2,12 +2,86 @@ namespace App; -use Exception; - final class Parser { public function parse(string $inputPath, string $outputPath): void { - throw new Exception('TODO'); + $data = $this->parseRows($inputPath); + + $chunks = array_chunk($data, floor(count($data) / 8), preserve_keys: true); + + $fp = fopen($outputPath, 'w'); + fwrite($fp, "{\n"); + fclose($fp); + + $childPids = []; + + foreach ($chunks as $chunk) { + $pid = pcntl_fork(); + + if ($pid === -1) { + throw new \RuntimeException('Failed to fork process'); + } + + if ($pid === 0) { + $this->processChunk($chunk, $outputPath); + exit(0); + } + + $childPids[] = $pid; + usleep(250); + } + + foreach ($childPids as $pid) { + pcntl_waitpid($pid, $status); + } + + $fp = fopen($outputPath, 'r+'); + fseek($fp, -2, SEEK_END); + fwrite($fp, "\n}"); + fclose($fp); + } + + private function processChunk(array $chunk, string $outputPath): void + { + $fragment = ''; + $padding0 = ' ';; + $padding1 = ' '; + + foreach ($chunk as $key => $rows) { + $fragment .= "{$padding0}\"\/blog\/{$key}\": {\n"; + + foreach ($rows as $date => $count) { + $fragment .= "{$padding1}\"{$date}\": {$count},\n"; + } + + $fragment = substr($fragment, 0, -2); + $fragment .= "\n{$padding0}},\n"; + } + + $fp = fopen($outputPath, 'a'); + flock($fp, LOCK_EX); + fwrite($fp, $fragment); + flock($fp, LOCK_UN); + fclose($fp); + } + + private function parseRows(string $inputPath): array + { + $data = []; + $rows = file($inputPath); + + foreach ($rows as $row) { + $tmp = explode(',', $row); + $key = substr($tmp[0], 25); + $date = substr($tmp[1], 0, 10); + $data[$key][$date] = ($data[$key][$date] ?? 0) + 1; + } + + foreach ($data as &$values) { + ksort($values); + } + + return $data; } -} \ No newline at end of file +} From e47d16064096728a1ba0d64565b25655cb6172c6 Mon Sep 17 00:00:00 2001 From: Mathias Grimm Date: Fri, 13 Mar 2026 19:14:12 -0300 Subject: [PATCH 2/5] mathiasgrimm attempt #1 --- app/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Parser.php b/app/Parser.php index 556e062b80..a926de351b 100644 --- a/app/Parser.php +++ b/app/Parser.php @@ -45,7 +45,7 @@ public function parse(string $inputPath, string $outputPath): void private function processChunk(array $chunk, string $outputPath): void { $fragment = ''; - $padding0 = ' ';; + $padding0 = ' '; $padding1 = ' '; foreach ($chunk as $key => $rows) { From 285bdd4f1268b63412aab3341e0f83e29d237527 Mon Sep 17 00:00:00 2001 From: Mathias Grimm Date: Fri, 13 Mar 2026 19:18:47 -0300 Subject: [PATCH 3/5] mathiasgrimm attempt #2 --- app/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Parser.php b/app/Parser.php index a926de351b..34c0ff86fa 100644 --- a/app/Parser.php +++ b/app/Parser.php @@ -29,7 +29,7 @@ public function parse(string $inputPath, string $outputPath): void } $childPids[] = $pid; - usleep(250); + usleep(1000); } foreach ($childPids as $pid) { From 14eee5601ac8f0d10d647ec390350421eeec2e44 Mon Sep 17 00:00:00 2001 From: Mathias Grimm Date: Fri, 13 Mar 2026 19:40:12 -0300 Subject: [PATCH 4/5] mathiasgrimm attempt #3 --- app/Parser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Parser.php b/app/Parser.php index 34c0ff86fa..36c39a498f 100644 --- a/app/Parser.php +++ b/app/Parser.php @@ -8,7 +8,7 @@ public function parse(string $inputPath, string $outputPath): void { $data = $this->parseRows($inputPath); - $chunks = array_chunk($data, floor(count($data) / 8), preserve_keys: true); + $chunks = array_chunk($data, floor(count($data) / 2), preserve_keys: true); $fp = fopen($outputPath, 'w'); fwrite($fp, "{\n"); @@ -29,7 +29,7 @@ public function parse(string $inputPath, string $outputPath): void } $childPids[] = $pid; - usleep(1000); + usleep(10000); } foreach ($childPids as $pid) { From acad16a74c2e764ce9b9864aa58f9dd2ef099241 Mon Sep 17 00:00:00 2001 From: Mathias Grimm Date: Fri, 13 Mar 2026 19:53:04 -0300 Subject: [PATCH 5/5] mathiasgrimm attempt #4 --- app/Parser.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Parser.php b/app/Parser.php index 36c39a498f..057c02bf81 100644 --- a/app/Parser.php +++ b/app/Parser.php @@ -6,6 +6,8 @@ final class Parser { public function parse(string $inputPath, string $outputPath): void { + // error_reporting(E_ALL); + ini_set('memory_limit', '4096M'); $data = $this->parseRows($inputPath); $chunks = array_chunk($data, floor(count($data) / 2), preserve_keys: true); @@ -40,6 +42,8 @@ public function parse(string $inputPath, string $outputPath): void fseek($fp, -2, SEEK_END); fwrite($fp, "\n}"); fclose($fp); + + // echo memory_get_peak_usage(true) / 1024 / 1024 . 'M'.PHP_EOL; } private function processChunk(array $chunk, string $outputPath): void