2022-12-28 15:04:45 +00:00
|
|
|
package Travelynx::Command::influxdb;
|
|
|
|
|
2023-07-03 15:59:25 +00:00
|
|
|
# Copyright (C) 2022 Birte Kristina Friesel
|
2022-12-28 15:04:45 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use Mojo::Base 'Mojolicious::Command';
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
|
|
|
|
has description => 'Generate statistics for InfluxDB';
|
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage };
|
|
|
|
|
|
|
|
sub query_to_influx {
|
|
|
|
my ( $label, $value ) = @_;
|
|
|
|
|
|
|
|
if ( defined $value ) {
|
|
|
|
return sprintf( '%s=%f', $label, $value );
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $db = $self->app->pg->db;
|
|
|
|
|
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
my $active = $now->clone->subtract( months => 1 );
|
|
|
|
|
2023-01-02 05:20:07 +00:00
|
|
|
my @stats;
|
2024-07-26 17:20:50 +00:00
|
|
|
my @backend_stats;
|
2023-01-02 05:20:07 +00:00
|
|
|
my @traewelling;
|
2022-12-28 15:04:45 +00:00
|
|
|
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@stats,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
|
|
|
'pending_user_count',
|
|
|
|
$db->select( 'users', 'count(*) as count', { status => 0 } )
|
|
|
|
->hash->{count}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@stats,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
|
|
|
'reg_user_count',
|
|
|
|
$db->select( 'users', 'count(*) as count', { status => 1 } )
|
|
|
|
->hash->{count}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@stats,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
|
|
|
'active_user_count',
|
|
|
|
$db->select(
|
|
|
|
'users',
|
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
status => 1,
|
|
|
|
last_seen => { '>', $active }
|
|
|
|
}
|
|
|
|
)->hash->{count}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@stats,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
|
|
|
'checked_in_count',
|
|
|
|
$db->select( 'in_transit', 'count(*) as count' )->hash->{count}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@stats,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
2022-12-28 16:41:17 +00:00
|
|
|
'checkin_count',
|
|
|
|
$db->select( 'journeys', 'count(*) as count' )->hash->{count}
|
2022-12-28 15:04:45 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@stats,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
|
|
|
'polyline_count',
|
|
|
|
$db->select( 'polylines', 'count(*) as count' )->hash->{count}
|
|
|
|
)
|
|
|
|
);
|
2023-08-13 08:55:32 +00:00
|
|
|
|
2024-07-26 17:20:50 +00:00
|
|
|
my @backends = $self->app->stations->get_backends;
|
|
|
|
|
|
|
|
for my $backend (@backends) {
|
|
|
|
push(
|
|
|
|
@backend_stats,
|
|
|
|
[
|
|
|
|
$backend->{iris} ? 'IRIS' : $backend->{name},
|
|
|
|
$db->select(
|
|
|
|
'stations',
|
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
source => $backend->{id},
|
|
|
|
archived => 0
|
|
|
|
}
|
|
|
|
)->hash->{count},
|
|
|
|
$db->select(
|
|
|
|
'related_stations',
|
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
backend_id => $backend->{id},
|
|
|
|
}
|
|
|
|
)->hash->{count}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2023-08-13 08:55:32 +00:00
|
|
|
|
2022-12-28 15:04:45 +00:00
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@traewelling,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
2023-01-02 05:20:07 +00:00
|
|
|
'pull_user_count',
|
2022-12-28 15:04:45 +00:00
|
|
|
$db->select(
|
|
|
|
'traewelling',
|
|
|
|
'count(*) as count',
|
|
|
|
{ pull_sync => 1 }
|
|
|
|
)->hash->{count}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@traewelling,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
2023-01-02 05:20:07 +00:00
|
|
|
'push_user_count',
|
2022-12-28 15:04:45 +00:00
|
|
|
$db->select(
|
|
|
|
'traewelling',
|
|
|
|
'count(*) as count',
|
|
|
|
{ push_sync => 1 }
|
|
|
|
)->hash->{count}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
push(
|
2023-01-02 05:20:07 +00:00
|
|
|
@stats,
|
2022-12-28 15:04:45 +00:00
|
|
|
query_to_influx(
|
|
|
|
'polyline_ratio',
|
|
|
|
$db->query(
|
|
|
|
'select (select count(polyline_id) from journeys)::float / (select count(*) from polylines) as ratio'
|
|
|
|
)->hash->{ratio}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-08-13 08:55:32 +00:00
|
|
|
if ( $self->app->mode eq 'development' ) {
|
|
|
|
$self->app->log->debug( 'POST '
|
|
|
|
. $self->app->config->{influxdb}->{url}
|
|
|
|
. ' stats '
|
|
|
|
. join( ',', @stats ) );
|
2024-07-26 17:20:50 +00:00
|
|
|
for my $backend_entry (@backend_stats) {
|
|
|
|
$self->app->log->debug(
|
|
|
|
'POST '
|
|
|
|
. $self->app->config->{influxdb}->{url}
|
|
|
|
. ' stations,backend='
|
|
|
|
. $backend_entry->[0]
|
|
|
|
. sprintf(
|
|
|
|
' count=%d,meta=%d',
|
|
|
|
$backend_entry->[1], $backend_entry->[2]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-08-13 08:55:32 +00:00
|
|
|
$self->app->log->debug( 'POST '
|
|
|
|
. $self->app->config->{influxdb}->{url}
|
|
|
|
. ' traewelling '
|
|
|
|
. join( ',', @traewelling ) );
|
|
|
|
}
|
|
|
|
elsif ( $self->app->config->{influxdb}->{url} ) {
|
2023-01-02 05:20:07 +00:00
|
|
|
$self->app->ua->post_p(
|
|
|
|
$self->app->config->{influxdb}->{url},
|
|
|
|
'stats ' . join( ',', @stats )
|
|
|
|
)->wait;
|
2024-07-26 17:20:50 +00:00
|
|
|
my $buf = q{};
|
|
|
|
for my $backend_entry (@backend_stats) {
|
|
|
|
$buf
|
|
|
|
.= "\nstations,backend="
|
|
|
|
. $backend_entry->[0]
|
|
|
|
. sprintf( ' count=%d,meta=%d',
|
|
|
|
$backend_entry->[1], $backend_entry->[2] );
|
|
|
|
}
|
|
|
|
$self->app->ua->post_p( $self->app->config->{influxdb}->{url}, $buf )
|
|
|
|
->wait;
|
2023-01-02 05:20:07 +00:00
|
|
|
$self->app->ua->post_p(
|
|
|
|
$self->app->config->{influxdb}->{url},
|
|
|
|
'traewelling ' . join( ',', @traewelling )
|
|
|
|
)->wait;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->app->log->warn(
|
|
|
|
"influxdb command called, but no influxdb url has been configured");
|
|
|
|
}
|
|
|
|
|
2022-12-28 15:04:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
2023-01-02 05:20:07 +00:00
|
|
|
Usage: index.pl influxdb
|
2022-12-28 15:04:45 +00:00
|
|
|
|
2023-01-02 05:20:07 +00:00
|
|
|
Write statistics to InfluxDB
|