diff --git a/app/Models/StatsDAO.php b/app/Models/StatsDAO.php index cd5b0f6f4..907c9afb9 100644 --- a/app/Models/StatsDAO.php +++ b/app/Models/StatsDAO.php @@ -89,14 +89,16 @@ class FreshRSS_StatsDAO extends Minz_ModelPdo { $count = $this->initEntryCountArray(); $midnight = mktime(0, 0, 0) ?: 0; $oldest = $midnight - (self::ENTRY_COUNT_PERIOD * 86400); + $endOfDay = $midnight + 86400; - // Get stats per day for the last 30 days - $sqlDay = $this->sqlFloor("(date - $midnight) / 86400"); + // Get stats per day for the last 30 days, including today. + // The reference is the oldest timestamp so that the division is always positive (and thus floors correctly with integer division). + $sqlDay = $this->sqlFloor("(date - $oldest) / 86400"); $sql = <<= {$oldest} AND date < {$midnight} + WHERE date >= {$oldest} AND date < {$endOfDay} GROUP BY day ORDER BY day ASC SQL; @@ -108,7 +110,8 @@ class FreshRSS_StatsDAO extends Minz_ModelPdo { foreach ($res as $value) { $count[(int)($value['day'])] = (int)($value['count']); } - return $count; + // Return a list so that JSON encoding produces an array and not an object. + return array_values($count); } /** @@ -116,7 +119,7 @@ class FreshRSS_StatsDAO extends Minz_ModelPdo { * @return array */ protected function initEntryCountArray(): array { - return $this->initStatsArray(-self::ENTRY_COUNT_PERIOD, -1); + return $this->initStatsArray(0, self::ENTRY_COUNT_PERIOD); } /** @@ -141,9 +144,22 @@ class FreshRSS_StatsDAO extends Minz_ModelPdo { */ public function calculateEntryRepartitionPerFeedPerMonth(?int $feed = null): array { $monthRepartition = $this->calculateEntryRepartitionPerFeedPerPeriod('%m', $feed); - // cut out the 0th month (Jan=1, Dec=12) - \array_splice($monthRepartition, 0, 1); - return $monthRepartition; + return $this->normalizeMonthRepartition($monthRepartition); + } + + /** + * Normalize a month repartition so that it always contains 12 entries indexed from 0 (January) to 11 (December). + * @param array $repartition + * @return array + */ + protected function normalizeMonthRepartition(array $repartition): array { + $months = array_fill(0, 12, 0); + foreach ($repartition as $month => $count) { + if ($month >= 1 && $month <= 12) { + $months[$month - 1] = $count; + } + } + return $months; } /** diff --git a/app/Models/StatsDAOPGSQL.php b/app/Models/StatsDAOPGSQL.php index cade9b3d8..32422ce18 100644 --- a/app/Models/StatsDAOPGSQL.php +++ b/app/Models/StatsDAOPGSQL.php @@ -34,7 +34,9 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { */ #[\Override] public function calculateEntryRepartitionPerFeedPerDayOfWeek(?int $feed = null): array { - return $this->calculateEntryRepartitionPerFeedPerPeriod('day', $feed); + // PostgreSQL uses `dow` (0 = Sunday .. 6 = Saturday) for the day of week, + // whereas `day` would extract the day of the month. + return $this->calculateEntryRepartitionPerFeedPerPeriod('dow', $feed); } /** @@ -43,7 +45,8 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { */ #[\Override] public function calculateEntryRepartitionPerFeedPerMonth(?int $feed = null): array { - return $this->calculateEntryRepartitionPerFeedPerPeriod('month', $feed); + $monthRepartition = $this->calculateEntryRepartitionPerFeedPerPeriod('month', $feed); + return $this->normalizeMonthRepartition($monthRepartition); } /** @@ -73,7 +76,7 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { $periodMax = match ($period) { 'hour' => 24, - 'day' => 7, + 'dow' => 7, 'month' => 12, default => 30, }; diff --git a/p/scripts/statsWithChartjs.js b/p/scripts/statsWithChartjs.js index 7282f7f9b..30cf86377 100644 --- a/p/scripts/statsWithChartjs.js +++ b/p/scripts/statsWithChartjs.js @@ -148,6 +148,7 @@ function jsonChartBarWithAverage(labelBarChart, dataBarChart, labelAverage, data return { type: 'bar', data: { + labels: xAxisLabels, datasets: [ { // bar chart layout @@ -164,10 +165,7 @@ function jsonChartBarWithAverage(labelBarChart, dataBarChart, labelAverage, data type: 'line', label: labelAverage, // Todo: i18n borderColor: 'rgb(192,216,0)', - data: { - '-30': dataAverage, - '-1': dataAverage, - }, + data: dataBarChart.map(() => dataAverage), order: 1, }, ], @@ -180,9 +178,9 @@ function jsonChartBarWithAverage(labelBarChart, dataBarChart, labelAverage, data }, x: { ticks: { - callback: function (val) { + callback: function (val, index) { if (xAxisLabels.length > 0) { - return xAxisLabels[val]; + return xAxisLabels[index] ?? val; } else { return val; } diff --git a/tests/app/Models/StatsDAOTest.php b/tests/app/Models/StatsDAOTest.php index b53257a66..d474eeccc 100644 --- a/tests/app/Models/StatsDAOTest.php +++ b/tests/app/Models/StatsDAOTest.php @@ -4,6 +4,80 @@ use PHPUnit\Framework\TestCase; final class StatsDAOTest extends TestCase { + /** + * @return array{0:FreshRSS_StatsDAOSQLite,1:Minz_PdoSqlite} + */ + private static function createSQLiteDao(): array { + $pdo = new Minz_PdoSqlite('sqlite::memory:'); + $pdo->setPrefix(''); + $pdo->exec('CREATE TABLE entry (id INTEGER PRIMARY KEY, date INTEGER)'); + $dao = new FreshRSS_StatsDAOSQLite('test', $pdo); + return [$dao, $pdo]; + } + + public static function testCalculateEntryCountIncludesTodayWithSQLite(): void { + [$dao, $pdo] = self::createSQLiteDao(); + $midnight = mktime(0, 0, 0) ?: 0; + for ($day = 0; $day <= 30; $day++) { + $pdo->exec('INSERT INTO entry(date) VALUES (' . ($midnight - $day * 86400 + 12 * 3600) . ')'); + } + self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount()); + } + + public static function testCalculateDayOfWeekRepartitionWithSQLite(): void { + [$dao, $pdo] = self::createSQLiteDao(); + $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday + $dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek(); + self::assertCount(7, $dayOfWeek); + self::assertSame(1, $dayOfWeek[0]); + self::assertSame(1, array_sum($dayOfWeek)); + } + + public static function testCalculateMonthRepartitionWithSQLite(): void { + [$dao, $pdo] = self::createSQLiteDao(); + $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 15, 2024) . ')'); + $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 12, 15, 2024) . ')'); + $months = $dao->calculateEntryRepartitionPerFeedPerMonth(); + self::assertCount(12, $months); + self::assertSame(1, $months[0]); // January + self::assertSame(1, $months[11]); // December + self::assertSame(2, array_sum($months)); + } + + public static function testPostgreSQLRepartitions(): void { + $dsn = getenv('FRESHRSS_TEST_PGSQL_DSN'); + if ($dsn === false || $dsn === '') { + self::markTestSkipped('FRESHRSS_TEST_PGSQL_DSN is not configured.'); + } + $pdo = new Minz_PdoPgsql($dsn, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); + $prefix = 'stats_test_' . getmypid() . '_'; + $pdo->setPrefix($prefix); + $pdo->exec("CREATE TABLE {$prefix}entry (id BIGSERIAL PRIMARY KEY, date BIGINT)"); + try { + $dao = new FreshRSS_StatsDAOPGSQL('test', $pdo); + $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday + $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 12, 15, 2024) . ')'); + + $dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek(); + self::assertCount(7, $dayOfWeek); + self::assertSame(2, $dayOfWeek[0]); // Both 2024-01-07 and 2024-12-15 are Sundays + self::assertSame(2, array_sum($dayOfWeek)); + + $months = $dao->calculateEntryRepartitionPerFeedPerMonth(); + self::assertCount(12, $months); + self::assertSame(1, $months[0]); // January + self::assertSame(1, $months[11]); // December + + $midnight = mktime(0, 0, 0) ?: 0; + for ($day = 0; $day <= 30; $day++) { + $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . ($midnight - $day * 86400 + 12 * 3600) . ')'); + } + self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount()); + } finally { + $pdo->exec("DROP TABLE {$prefix}entry"); + } + } + public static function testCalculateEntryAverageFromRepartition(): void { self::assertSame(2.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition([0, 2, 4])); }