summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Model/Journeys.pm
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-12-03 21:42:17 +0100
committerDaniel Friesel <derf@finalrewind.org>2020-12-03 21:42:17 +0100
commita5ab4fa6a8a41c2fd50cfa2ad8bee1c985ffd593 (patch)
tree1d09587f97ac8ecb53edce559b378768b6991e0b /lib/Travelynx/Model/Journeys.pm
parent77ecd6d034440d542ecba8e56c6d6917d73bf834 (diff)
regularly generate statistics in the background1.18.11
Diffstat (limited to 'lib/Travelynx/Model/Journeys.pm')
-rwxr-xr-xlib/Travelynx/Model/Journeys.pm59
1 files changed, 58 insertions, 1 deletions
diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm
index f5f5424..e09ca7d 100755
--- a/lib/Travelynx/Model/Journeys.pm
+++ b/lib/Travelynx/Model/Journeys.pm
@@ -763,6 +763,62 @@ sub get_months_for_year {
return @ret;
}
+sub get_yyyymm_having_journeys {
+ my ( $self, %opt ) = @_;
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $res = $db->select(
+ 'journeys',
+ "distinct to_char(real_departure, 'YYYY.MM') as yearmonth",
+ { user_id => $uid },
+ { order_by => { -asc => 'yearmonth' } }
+ );
+
+ my @ret;
+ for my $row ( $res->hashes->each ) {
+ push( @ret, [ split( qr{[.]}, $row->{yearmonth} ) ] );
+ }
+
+ return @ret;
+}
+
+sub generate_missing_stats {
+ my ( $self, %opt ) = @_;
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my @journey_months = $self->get_yyyymm_having_journeys(
+ uid => $uid,
+ db => $db
+ );
+ my @stats_months = $self->stats_cache->get_yyyymm_having_stats(
+ uid => $uid,
+ $db => $db
+ );
+
+ my $stats_index = 0;
+
+ for my $journey_index ( 0 .. $#journey_months ) {
+ if ( $stats_index < @stats_months
+ and $journey_months[$journey_index][0]
+ == $stats_months[$stats_index][0]
+ and $journey_months[$journey_index][1]
+ == $stats_months[$stats_index][1] )
+ {
+ $stats_index++;
+ }
+ else {
+ my ( $year, $month ) = @{ $journey_months[$journey_index] };
+ $self->get_stats(
+ uid => $uid,
+ db => $db,
+ year => $year,
+ month => $month,
+ write_only => 1
+ );
+ }
+ }
+}
+
sub get_nav_months {
my ( $self, %opt ) = @_;
@@ -1048,7 +1104,8 @@ sub get_stats {
# checks out of a train or manually edits/adds a journey.
if (
- my $stats = $self->stats_cache->get(
+ not $opt{write_only}
+ and my $stats = $self->stats_cache->get(
uid => $uid,
db => $db,
year => $year,