summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2025-10-19 16:49:38 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2025-10-19 16:49:38 +0200
commit40417bc2821db3e824b6ac00eaa1a01ed83a9ea4 (patch)
tree26af3a3b3de4a4f04f36333b46a11bea630f6aa0 /lib
parent71240fd267d4ffdbc98add04dad38f76e8769389 (diff)
add command for periodic stats refresh
Diffstat (limited to 'lib')
-rw-r--r--lib/Travelynx/Command/stats.pm58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/Travelynx/Command/stats.pm b/lib/Travelynx/Command/stats.pm
new file mode 100644
index 0000000..c6522da
--- /dev/null
+++ b/lib/Travelynx/Command/stats.pm
@@ -0,0 +1,58 @@
+package Travelynx::Command::stats;
+
+# Copyright (C) 2020-2023 Birte Kristina Friesel
+#
+# SPDX-License-Identifier: AGPL-3.0-or-later
+use Mojo::Base 'Mojolicious::Command';
+
+use DateTime;
+
+has description => 'Deal with monthly and yearly statistics';
+
+has usage => sub { shift->extract_usage };
+
+sub refresh_all {
+ my ($self) = @_;
+
+ my $db = $self->app->pg->db;
+ my $now = DateTime->now( time_zone => 'Europe/Berlin' );
+
+ say 'Refreshing all stats, this may take a while ...';
+
+ my $total = $db->select( 'users', 'count(*) as count', { status => 1 } )->hash->{count};
+ my $i = 1;
+
+ for
+ my $user ( $db->select( 'users', ['id'], { status => 1 } )->hashes->each )
+ {
+ my $tx = $db->begin;
+ $self->app->journeys->generate_missing_stats( uid => $user->{id} );
+ $self->app->journeys->get_stats(
+ uid => $user->{id},
+ year => $now->year
+ );
+ $tx->commit;
+ if ($i == $total or ($i % 100) == 0) {
+ printf("%.f%% complete", $i * 100 / $total);
+ }
+ }
+}
+
+sub run {
+ my ( $self, $cmd, @arg ) = @_;
+
+ if ($cmd eq 'refresh-all') {
+ $self->refresh_all(@arg);
+ }
+
+}
+
+1;
+
+__END__
+
+=head1 SYNOPSIS
+
+ Usage: index.pl stats refresh-all
+
+ Refreshes all stats