diff --git a/lib/MirrorCache/Datamodule.pm b/lib/MirrorCache/Datamodule.pm index d4e66175..2d197ed2 100644 --- a/lib/MirrorCache/Datamodule.pm +++ b/lib/MirrorCache/Datamodule.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2021 SUSE LLC +# Copyright (C) 2021,2022 SUSE LLC # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,6 +17,7 @@ package MirrorCache::Datamodule; use Mojo::Base -base, -signatures; use Mojo::URL; +use Time::HiRes qw(time); use Digest::SHA qw(sha1_hex); use MirrorCache::Utils 'region_for_country'; @@ -47,6 +48,12 @@ has root_subtree => ($ENV{MIRRORCACHE_SUBTREE} // ""); has vpn_prefix => ($ENV{MIRRORCACHE_VPN_PREFIX} ? lc($ENV{MIRRORCACHE_VPN_PREFIX}) : "10."); +has 'at'; + +sub elapsed($self) { + return abs(time - $self->at); +} + sub app($self, $app) { $self->_route($app->mc->route); $self->_route_len(length($self->_route)); @@ -54,6 +61,7 @@ sub app($self, $app) { } sub reset($self, $c, $top_folder = undef) { + $self->at(time); if ($top_folder) { $self->route(''); $self->route_len(0); diff --git a/lib/MirrorCache/Task/FolderHashesImport.pm b/lib/MirrorCache/Task/FolderHashesImport.pm index 3427a3ad..42eca06d 100644 --- a/lib/MirrorCache/Task/FolderHashesImport.pm +++ b/lib/MirrorCache/Task/FolderHashesImport.pm @@ -53,7 +53,7 @@ sub _run { my $mojo_url = Mojo::URL->new($hq_url); my $res = Mojo::UserAgent->new->get($mojo_url, {'User-Agent' => 'MirrorCache/hashes_import'})->result; return $job->fail('Request to HEADQUARTER ' . $hq_url . ' failed, response code ' . $res->code) - if $res->code != 200; + if $res->code > 299; my $res_json = $res->json; my $last_import; diff --git a/lib/MirrorCache/WebAPI/Plugin/AuditLog.pm b/lib/MirrorCache/WebAPI/Plugin/AuditLog.pm index 4470295a..3c1f5e00 100644 --- a/lib/MirrorCache/WebAPI/Plugin/AuditLog.pm +++ b/lib/MirrorCache/WebAPI/Plugin/AuditLog.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2020 SUSE LLC +# Copyright (C) 2020-2022 SUSE LLC # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -43,7 +43,7 @@ sub register { # log restart my $schema = $app->schema; $schema->resultset('AuditEvent') - ->create({user_id => -1, name => 'startup', event_data => 'AuditLog registered'}); + ->create({user_id => -1, name => 'startup', event_data => "AuditLog registered $$"}); } sub on_event { diff --git a/lib/MirrorCache/WebAPI/Plugin/Stat.pm b/lib/MirrorCache/WebAPI/Plugin/Stat.pm index dbf46230..f92f3eaf 100644 --- a/lib/MirrorCache/WebAPI/Plugin/Stat.pm +++ b/lib/MirrorCache/WebAPI/Plugin/Stat.pm @@ -62,11 +62,10 @@ sub redirect_to_region($self, $dm) { sub redirect_to_mirror($self, $mirror_id, $dm) { my ($path, $trailing_slash) = $dm->path; - return undef if $mirror_id == -1 && 'media' eq substr($path, -length('media')); $path = $dm->root_subtree . $path; my $rows = $self->rows; my @rows = defined $rows? @$rows : (); - push @rows, [ $dm->ip_sha1, scalar $dm->agent, scalar ($path . $trailing_slash), $dm->country, datetime_now(), $mirror_id, $dm->folder_id, $dm->file_id, $dm->is_secure, $dm->is_ipv4, $dm->metalink? 1 : 0, $dm->mirrorlist? 1 : 0, $dm->is_head, $dm->file_age, $dm->folder_scan_last ]; + push @rows, [ $dm->ip_sha1, scalar $dm->agent, scalar ($path . $trailing_slash), $dm->country, datetime_now(), $mirror_id, $dm->folder_id, $dm->file_id, $dm->is_secure, $dm->is_ipv4, $dm->metalink? 1 : 0, $dm->mirrorlist? 1 : 0, $dm->is_head, $$, int($dm->elapsed*1000), $dm->file_age, $dm->folder_scan_last ]; my $cnt = @rows; if ($cnt >= $FLUSH_COUNT) { $self->rows(undef); @@ -75,7 +74,6 @@ sub redirect_to_mirror($self, $mirror_id, $dm) { $self->rows(\@rows); return if $self->timer; - # my $loop = $self->ioloop; my $id = Mojo::IOLoop->singleton->timer($FLUSH_INTERVAL_SECONDS => sub ($loop) { $self->flush($self->rows); }); @@ -90,9 +88,9 @@ sub flush($self, $rows) { return unless $rows; $self->rows(undef); my @rows = @$rows; - my $sql = <<'END_SQL'; -insert into stat(ip_sha1, agent, path, country, dt, mirror_id, folder_id, file_id, secure, ipv4, metalink, mirrorlist, head) -values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + my $sql = <<"END_SQL"; +insert into stat(ip_sha1, agent, path, country, dt, mirror_id, folder_id, file_id, secure, ipv4, metalink, mirrorlist, head, pid, execution_time) +values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) END_SQL my %demand_sync; @@ -106,10 +104,8 @@ END_SQL for my $row (@rows) { my $folder_id = $row->[6]; my $mirror_id = $row->[5]; - my $file_age = $row->[13]; - my $scan_last = $row->[14]; - pop @$row; - pop @$row; + my $scan_last = pop @$row; + my $file_age = pop @$row; $prep->execute(@$row); if ($folder_id) { next if $mirror_id > 0; diff --git a/lib/MirrorCache/resources/migrations/pg.sql b/lib/MirrorCache/resources/migrations/pg.sql index 8708cab2..7b92eeb7 100644 --- a/lib/MirrorCache/resources/migrations/pg.sql +++ b/lib/MirrorCache/resources/migrations/pg.sql @@ -153,7 +153,10 @@ create table if not exists stat ( secure boolean NOT NULL, ipv4 boolean NOT NULL, metalink boolean default 'f', - head boolean default 'f' + head boolean default 'f', + mirrorlist boolean default 'f', + pid int, + execution_time int ); create index if not exists stat_dt_mirror on stat(dt, mirror_id, secure, ipv4); @@ -251,3 +254,6 @@ alter table hash add column if not exists zlengths varchar(32), add column if not exists zblock_size int, add column if not exists zhashes bytea; +alter table stat + add column if not exists pid int, + add column if not exists execution_time int;