summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command/stats.pm
blob: 953c75d5266532065b4834f7bf7a51306dccfd77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 )
	{
		$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 'refresh-all' ) {
		$self->refresh_all(@arg);
	}

}

1;

__END__

=head1 SYNOPSIS

  Usage: index.pl stats refresh-all

  Refreshes all stats