Skip to content

Commit

Permalink
tec: Improve output of the system stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Jan 23, 2024
1 parent 578a848 commit 244278d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cli/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions src/cli/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand Down
10 changes: 9 additions & 1 deletion src/models/dao/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int>
*/
public static function countPerMonth(int $year): array
Expand All @@ -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;

Expand All @@ -84,14 +87,19 @@ 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<string, int>
*/
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
Expand Down

0 comments on commit 244278d

Please sign in to comment.