summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command/munin.pm
blob: 01f5a0ee72b05b55c60d5f432d7e00f04de1aaf4 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package Travelynx::Command::munin;
use Mojo::Base 'Mojolicious::Command';

use DateTime;

has description => 'Generate statistics for munin-node';

has usage => sub { shift->extract_usage };

sub query_to_munin {
	my ( $label, $value ) = @_;

	if ( defined $value ) {
		printf( "%s.value %f\n", $label, $value );
	}
}

sub run {
	my ( $self, $filename ) = @_;

	my $db = $self->app->pg->db;

	my $now    = DateTime->now( time_zone => 'Europe/Berlin' );
	my $active = $now->clone->subtract( months => 1 );

	my $checkin_window_query
	  = qq{select count(*) as count from journeys where checkin_time > to_timestamp(?);};

	query_to_munin( 'reg_user_count',
		$db->select( 'users', 'count(*) as count', { status => 1 } )
		  ->hash->{count} );
	query_to_munin(
		'active_user_count',
		$db->select(
			'users',
			'count(*) as count',
			{
				status    => 1,
				last_seen => { '>', $active }
			}
		)->hash->{count}
	);
	query_to_munin( 'checked_in',
		$db->select( 'in_transit', 'count(*) as count' )->hash->{count} );
	query_to_munin(
		'checkins_24h',
		$db->query( $checkin_window_query,
			$now->subtract( hours => 24 )->epoch )->hash->{count}
	);
	query_to_munin( 'checkins_7d',
		$db->query( $checkin_window_query, $now->subtract( days => 7 )->epoch )
		  ->hash->{count} );
	query_to_munin(
		'checkins_30d',
		$db->query(
			$checkin_window_query, $now->subtract( days => 30 )->epoch
		)->hash->{count}
	);
	query_to_munin( 'polylines',
		$db->select( 'polylines', 'count(*) as count' )->hash->{count} );
	query_to_munin(
		'polyline_ratio',
		$db->query(
'select (select count(polyline_id) from journeys)::float / (select count(*) from polylines) as ratio'
		)->hash->{ratio}
	);
}

1;

__END__

=head1 SYNOPSIS

  Usage: index.pl munin

  Write statistics for munin-node to stdout