Loading lib/Travelynx/Command/stats.pm +0 −31 Original line number Diff line number Diff line Loading @@ -51,43 +51,12 @@ sub purge_cache { $db->query('truncate table journey_stats;'); } 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 ) { $self->app->journeys->generate_missing_stats( uid => $user->{id} ); $self->app->journeys->get_stats( uid => $user->{id}, year => $now->year, write_only => 1, ); if ( $i == $total or ( $i % 10 ) == 0 ) { printf( "%.f%% complete\n", $i * 100 / $total ); } $i++; } } sub run { my ( $self, $cmd, @arg ) = @_; if ( $cmd eq 'compute-distances' ) { $self->compute_distances(@arg); } elsif ( $cmd eq 'refresh-all' ) { $self->refresh_all(@arg); } elsif ( $cmd eq 'purge-cache' ) { $self->purge_cache(@arg); } Loading lib/Travelynx/Model/JourneyStatsCache.pm +0 −21 Original line number Diff line number Diff line Loading @@ -98,25 +98,4 @@ sub invalidate { ); } sub get_yyyymm_having_stats { my ( $self, %opt ) = @_; my $uid = $opt{uid}; my $db = $opt{db} // $self->{pg}->db; my $res = $db->select( 'journey_stats', [ 'year', 'month' ], { user_id => $uid }, { order_by => { -asc => [ 'year', 'month' ] } } ); my @ret; for my $row ( $res->hashes->each ) { if ( $row->{month} != 0 ) { push( @ret, [ $row->{year}, $row->{month} ] ); } } return @ret; } 1; lib/Travelynx/Model/Journeys.pm +1 −69 Original line number Diff line number Diff line Loading @@ -1187,73 +1187,6 @@ 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; my %need_year; 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] }; $need_year{$year} = 1; $self->get_stats( uid => $uid, db => $db, year => $year, month => $month, write_only => 1 ); } } for my $year ( keys %need_year ) { $self->get_stats( uid => $uid, db => $db, year => $year, write_only => 1 ); } } sub get_nav_months { my ( $self, %opt ) = @_; Loading Loading @@ -2020,8 +1953,7 @@ sub get_stats { # checks out of a train or manually edits/adds a journey. if ( not $opt{write_only} and not $opt{review} not $opt{review} and my $stats = $self->stats_cache->get( uid => $uid, db => $db, Loading Loading
lib/Travelynx/Command/stats.pm +0 −31 Original line number Diff line number Diff line Loading @@ -51,43 +51,12 @@ sub purge_cache { $db->query('truncate table journey_stats;'); } 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 ) { $self->app->journeys->generate_missing_stats( uid => $user->{id} ); $self->app->journeys->get_stats( uid => $user->{id}, year => $now->year, write_only => 1, ); if ( $i == $total or ( $i % 10 ) == 0 ) { printf( "%.f%% complete\n", $i * 100 / $total ); } $i++; } } sub run { my ( $self, $cmd, @arg ) = @_; if ( $cmd eq 'compute-distances' ) { $self->compute_distances(@arg); } elsif ( $cmd eq 'refresh-all' ) { $self->refresh_all(@arg); } elsif ( $cmd eq 'purge-cache' ) { $self->purge_cache(@arg); } Loading
lib/Travelynx/Model/JourneyStatsCache.pm +0 −21 Original line number Diff line number Diff line Loading @@ -98,25 +98,4 @@ sub invalidate { ); } sub get_yyyymm_having_stats { my ( $self, %opt ) = @_; my $uid = $opt{uid}; my $db = $opt{db} // $self->{pg}->db; my $res = $db->select( 'journey_stats', [ 'year', 'month' ], { user_id => $uid }, { order_by => { -asc => [ 'year', 'month' ] } } ); my @ret; for my $row ( $res->hashes->each ) { if ( $row->{month} != 0 ) { push( @ret, [ $row->{year}, $row->{month} ] ); } } return @ret; } 1;
lib/Travelynx/Model/Journeys.pm +1 −69 Original line number Diff line number Diff line Loading @@ -1187,73 +1187,6 @@ 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; my %need_year; 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] }; $need_year{$year} = 1; $self->get_stats( uid => $uid, db => $db, year => $year, month => $month, write_only => 1 ); } } for my $year ( keys %need_year ) { $self->get_stats( uid => $uid, db => $db, year => $year, write_only => 1 ); } } sub get_nav_months { my ( $self, %opt ) = @_; Loading Loading @@ -2020,8 +1953,7 @@ sub get_stats { # checks out of a train or manually edits/adds a journey. if ( not $opt{write_only} and not $opt{review} not $opt{review} and my $stats = $self->stats_cache->get( uid => $uid, db => $db, Loading