2019-03-22 20:18:03 +00:00
|
|
|
package Travelynx::Command::munin;
|
2021-04-05 07:38:08 +00:00
|
|
|
|
2023-02-19 08:35:38 +00:00
|
|
|
# Copyright (C) 2020-2023 Daniel Friesel
|
2020-11-27 21:12:56 +00:00
|
|
|
#
|
2021-01-29 17:32:13 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-03-22 20:18:03 +00:00
|
|
|
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 ) {
|
2020-02-10 19:06:55 +00:00
|
|
|
printf( "%s.value %f\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
|
|
|
|
2019-12-19 18:22:56 +00:00
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
2019-04-30 10:56:11 +00:00
|
|
|
my $active = $now->clone->subtract( months => 1 );
|
2019-03-22 20:18:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-05 07:45:28 +00:00
|
|
|
# DateTime's math does not like time zones: When subtracting 7 days from
|
|
|
|
# sun 2am and the previous sunday was the switch from CET to CEST (i.e.,
|
|
|
|
# the switch to daylight saving time), the resulting datetime is invalid.
|
|
|
|
# This is a fatal error. We avoid this edge case by performing date math
|
|
|
|
# on the epoch timestamp, which does not know or care about time zones and
|
|
|
|
# daylight saving time.
|
|
|
|
my $one_day = 24 * 60 * 60;
|
|
|
|
my $one_week = 7 * $one_day;
|
|
|
|
my $one_month = 30 * $one_day;
|
|
|
|
|
2021-04-20 21:10:22 +00:00
|
|
|
query_to_munin( 'pending_user_count',
|
|
|
|
$db->select( 'users', 'count(*) as count', { status => 0 } )
|
|
|
|
->hash->{count} );
|
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} );
|
2019-04-30 10:56:11 +00:00
|
|
|
query_to_munin(
|
|
|
|
'active_user_count',
|
|
|
|
$db->select(
|
|
|
|
'users',
|
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
status => 1,
|
|
|
|
last_seen => { '>', $active }
|
|
|
|
}
|
|
|
|
)->hash->{count}
|
|
|
|
);
|
2019-12-19 18:22:56 +00:00
|
|
|
query_to_munin( 'checked_in',
|
|
|
|
$db->select( 'in_transit', 'count(*) as count' )->hash->{count} );
|
2021-04-05 07:45:28 +00:00
|
|
|
query_to_munin( 'checkins_24h',
|
|
|
|
$db->query( $checkin_window_query, $now->epoch - $one_day )
|
|
|
|
->hash->{count} );
|
|
|
|
query_to_munin( 'checkins_7d',
|
|
|
|
$db->query( $checkin_window_query, $now->epoch - $one_week )
|
|
|
|
->hash->{count} );
|
|
|
|
query_to_munin( 'checkins_30d',
|
|
|
|
$db->query( $checkin_window_query, $now->epoch - $one_month )
|
|
|
|
->hash->{count} );
|
2020-01-27 19:33:35 +00:00
|
|
|
query_to_munin( 'polylines',
|
|
|
|
$db->select( 'polylines', 'count(*) as count' )->hash->{count} );
|
2022-07-09 14:04:29 +00:00
|
|
|
query_to_munin( 'traewelling_pull',
|
2022-07-09 14:16:08 +00:00
|
|
|
$db->select( 'traewelling', 'count(*) as count', { pull_sync => 1 } )
|
|
|
|
->hash->{count} );
|
|
|
|
query_to_munin( 'traewelling_push',
|
|
|
|
$db->select( 'traewelling', 'count(*) as count', { push_sync => 1 } )
|
|
|
|
->hash->{count} );
|
2020-02-10 19:06:55 +00:00
|
|
|
query_to_munin(
|
|
|
|
'polyline_ratio',
|
|
|
|
$db->query(
|
|
|
|
'select (select count(polyline_id) from journeys)::float / (select count(*) from polylines) as ratio'
|
|
|
|
)->hash->{ratio}
|
|
|
|
);
|
2019-03-22 20:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
Usage: index.pl munin
|
|
|
|
|
|
|
|
Write statistics for munin-node to stdout
|