2019-03-22 20:18:03 +00:00
|
|
|
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 {
|
2019-04-17 17:36:01 +00:00
|
|
|
my ( $label, $value ) = @_;
|
2019-03-22 20:18:03 +00:00
|
|
|
|
2019-04-17 17:36:01 +00:00
|
|
|
if ( defined $value ) {
|
|
|
|
printf( "%s.value %d\n", $label, $value );
|
2019-03-22 20:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run {
|
2019-04-17 17:36:01 +00:00
|
|
|
my ( $self, $filename ) = @_;
|
2019-03-22 20:18:03 +00:00
|
|
|
|
2019-04-17 17:36:01 +00:00
|
|
|
my $db = $self->app->pg->db;
|
2019-03-22 20:18:03 +00:00
|
|
|
|
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
|
|
|
|
my $checkin_window_query
|
2019-04-24 06:45:56 +00:00
|
|
|
= qq{select count(*) as count from journeys where checkin_time > to_timestamp(?);};
|
2019-03-22 20:18:03 +00:00
|
|
|
|
|
|
|
query_to_munin( 'reg_user_count',
|
2019-04-17 17:36:01 +00:00
|
|
|
$db->select( 'users', 'count(*) as count', { status => 1 } )
|
|
|
|
->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}
|
|
|
|
);
|
2019-03-22 20:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
Usage: index.pl munin
|
|
|
|
|
|
|
|
Write statistics for munin-node to stdout
|