From 244278dc755c0149fc85800974d17624b202c9ac Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Tue, 23 Jan 2024 10:48:56 +0100 Subject: [PATCH] tec: Improve output of the system stats command --- src/cli/Help.php | 3 +++ src/cli/System.php | 2 ++ src/models/dao/User.php | 10 +++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cli/Help.php b/src/cli/Help.php index 40bf3f3a..81a1ff2f 100644 --- a/src/cli/Help.php +++ b/src/cli/Help.php @@ -71,6 +71,9 @@ public function show(): Response $usage .= "\n"; $usage .= " system Show information about the system\n"; $usage .= " system secret Generate a secure key to be used as APP_SECRET_KEY\n"; + $usage .= " system stats Show statistics about the system\n"; + $usage .= " [--format=TEXT] where TEXT is either `csv` or `plain` (default)\n"; + $usage .= " [--year=INT] where INT is the year to output (only for CSV format)\n"; $usage .= "\n"; $usage .= " topics List the topics\n"; $usage .= " topics create Create a topic\n"; diff --git a/src/cli/System.php b/src/cli/System.php index 29dbc303..161186d6 100644 --- a/src/cli/System.php +++ b/src/cli/System.php @@ -115,6 +115,8 @@ public function stats(Request $request): Response ]; } + ksort($stats_per_date); + return Response::ok('cli/system/stats.csv.txt', [ 'stats_per_date' => $stats_per_date, ]); diff --git a/src/models/dao/User.php b/src/models/dao/User.php index f5be885c..8679d763 100644 --- a/src/models/dao/User.php +++ b/src/models/dao/User.php @@ -50,6 +50,8 @@ public static function countSince(\DateTimeImmutable $since): int /** * Return the number of new users per month for the given year. * + * It excludes users that are not validated. + * * @return array */ public static function countPerMonth(int $year): array @@ -59,6 +61,7 @@ public static function countPerMonth(int $year): array WHERE id != :support_user_id AND created_at >= :since AND created_at <= :until + AND validated_at IS NOT NULL GROUP BY date SQL; @@ -84,6 +87,7 @@ public static function countPerMonth(int $year): array * Return the number of active users per month for the given year. * * An active user is a user that created a link during a given month. + * It excludes users that are not validated. * * @return array */ @@ -91,7 +95,11 @@ public static function countActivePerMonth(int $year): array { $sql = <<<'SQL' SELECT to_char(created_at, 'YYYY-MM') AS date, COUNT(DISTINCT user_id) FROM links - WHERE user_id != :support_user_id + WHERE user_id IN ( + SELECT id FROM users + WHERE id != :support_user_id + AND validated_at IS NOT NULL + ) AND created_at >= :since AND created_at <= :until GROUP BY date