From 79a161b0e609281024ece87575ad58f91c421a29 Mon Sep 17 00:00:00 2001 From: Greg Sarjeant <1686767+gsarjeant@users.noreply.github.com> Date: Tue, 3 Jun 2025 08:39:46 -0400 Subject: [PATCH] Add Tick model. Move Tick management functions there. --- public/index.php | 10 ++++- src/Controller/Home/Home.php | 79 +----------------------------------- src/Model/Tick/Tick.php | 77 +++++++++++++++++++++++++++++++++++ src/lib/session.php | 16 -------- templates/home.php | 2 +- 5 files changed, 89 insertions(+), 95 deletions(-) create mode 100644 src/Model/Tick/Tick.php delete mode 100644 src/lib/session.php diff --git a/public/index.php b/public/index.php index 71d6e5a..11b2a89 100644 --- a/public/index.php +++ b/public/index.php @@ -1,6 +1,7 @@ itemsPerPage; $offset = ($page - 1) * $limit; - $ticks = iterator_to_array($this->stream_ticks($limit, $offset)); + $ticks = iterator_to_array(Tick::streamTicks($limit, $offset)); $view = new HomeView(); $tickList = $view->renderTicksSection($config->siteDescription, $ticks, $page, $limit); @@ -36,7 +36,7 @@ class HomeController{ } // save the tick - $this->save_tick($_POST['tick']); + Tick::save($_POST['tick']); } // get the config @@ -47,79 +47,4 @@ class HomeController{ exit; } - // TODO - move to a Tick model - private function stream_ticks(int $limit, int $offset = 0): Generator { - $tick_files = glob(TICKS_DIR . '/*/*/*.txt'); - usort($tick_files, fn($a, $b) => strcmp($b, $a)); // sort filenames in reverse chronological order - - $count = 0; - foreach ($tick_files as $file) { - // read all the ticks from the current file and reverse the order - // so the most recent ones are first - // - // each file is a single day, so we never hold more than - // one day's ticks in memory - $lines = array_reverse( - file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) - ); - - // split the path to the current file into the date components - $pathParts = explode('/', str_replace('\\', '/', $file)); - - // assign the different components to the appropriate part of the date - $year = $pathParts[count($pathParts) - 3]; - $month = $pathParts[count($pathParts) - 2]; - $day = pathinfo($pathParts[count($pathParts) - 1], PATHINFO_FILENAME); - - foreach ($lines as $line) { - // just keep skipping ticks until we get to the starting point - if ($offset > 0) { - $offset--; - continue; - } - - // Ticks are pipe-delimited: timestamp|text - // But just in case a tick contains a pipe, only split on the first one that occurs - $tickParts = explode('|', $line, 2); - $time = $tickParts[0]; - $tick = $tickParts[1]; - - // Build the timestamp from the date and time - // Ticks are always stored in UTC - $timestampUTC = "$year-$month-$day $time"; - yield [ - 'timestamp' => $timestampUTC, - 'tick' => $tick, - ]; - - if (++$count >= $limit) { - return; - } - } - } - } - - // TODO - move to a Tick model - private function save_tick(string $tick): void { - // build the tick path and filename from the current time - $now = new DateTime('now', new DateTimeZone('UTC')); - - $year = $now->format('Y'); - $month = $now->format('m'); - $day = $now->format('d'); - $time = $now->format('H:i:s'); - - // build the full path to the tick file - $dir = TICKS_DIR . "/$year/$month"; - $filename = "$dir/$day.txt"; - - // create the directory if it doesn't exist - if (!is_dir($dir)) { - mkdir($dir, 0770, true); - } - - // write the tick to the file (the file will be created if it doesn't exist) - $content = $time . "|" . $tick . "\n"; - file_put_contents($filename, $content, FILE_APPEND); - } } \ No newline at end of file diff --git a/src/Model/Tick/Tick.php b/src/Model/Tick/Tick.php new file mode 100644 index 0000000..7ceb537 --- /dev/null +++ b/src/Model/Tick/Tick.php @@ -0,0 +1,77 @@ + strcmp($b, $a)); // sort filenames in reverse chronological order + + $count = 0; + foreach ($tick_files as $file) { + // read all the ticks from the current file and reverse the order + // so the most recent ones are first + // + // each file is a single day, so we never hold more than + // one day's ticks in memory + $lines = array_reverse( + file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) + ); + + // split the path to the current file into the date components + $pathParts = explode('/', str_replace('\\', '/', $file)); + + // assign the different components to the appropriate part of the date + $year = $pathParts[count($pathParts) - 3]; + $month = $pathParts[count($pathParts) - 2]; + $day = pathinfo($pathParts[count($pathParts) - 1], PATHINFO_FILENAME); + + foreach ($lines as $line) { + // just keep skipping ticks until we get to the starting point + if ($offset > 0) { + $offset--; + continue; + } + + // Ticks are pipe-delimited: timestamp|text + // But just in case a tick contains a pipe, only split on the first one that occurs + $tickParts = explode('|', $line, 2); + $time = $tickParts[0]; + $tick = $tickParts[1]; + + // Build the timestamp from the date and time + // Ticks are always stored in UTC + $timestampUTC = "$year-$month-$day $time"; + yield [ + 'timestamp' => $timestampUTC, + 'tick' => $tick, + ]; + + if (++$count >= $limit) { + return; + } + } + } + } + + public static function save(string $tick): void { + // build the tick path and filename from the current time + $now = new DateTime('now', new DateTimeZone('UTC')); + + $year = $now->format('Y'); + $month = $now->format('m'); + $day = $now->format('d'); + $time = $now->format('H:i:s'); + + // build the full path to the tick file + $dir = TICKS_DIR . "/$year/$month"; + $filename = "$dir/$day.txt"; + + // create the directory if it doesn't exist + if (!is_dir($dir)) { + mkdir($dir, 0770, true); + } + + // write the tick to the file (the file will be created if it doesn't exist) + $content = $time . "|" . $tick . "\n"; + file_put_contents($filename, $content, FILE_APPEND); + } +} diff --git a/src/lib/session.php b/src/lib/session.php deleted file mode 100644 index abadb43..0000000 --- a/src/lib/session.php +++ /dev/null @@ -1,16 +0,0 @@ -= $config->siteTitle ?> - +