Skip to content

Commit

Permalink
Add execution_time to stat table (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-suse authored Jan 14, 2022
1 parent 6a537ef commit 7c9c68a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
10 changes: 9 additions & 1 deletion lib/MirrorCache/Datamodule.pm
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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';

Expand Down Expand Up @@ -47,13 +48,20 @@ 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));
$self->_root_region(region_for_country($self->root_country) || '');
}

sub reset($self, $c, $top_folder = undef) {
$self->at(time);
if ($top_folder) {
$self->route('');
$self->route_len(0);
Expand Down
2 changes: 1 addition & 1 deletion lib/MirrorCache/Task/FolderHashesImport.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/MirrorCache/WebAPI/Plugin/AuditLog.pm
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 6 additions & 10 deletions lib/MirrorCache/WebAPI/Plugin/Stat.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion lib/MirrorCache/resources/migrations/pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

0 comments on commit 7c9c68a

Please sign in to comment.