package Gruntmaster::Data;
-use v5.14;
+use 5.014;
use warnings;
use parent qw/Exporter/;
our $VERSION = '5999.000_013';
-our @EXPORT = qw/purge user_list user_entry problem_list problem_entry contest_list contest_entry contest_full contest_has_problem job_list job_entry job_full create_job standings update_status/; ## no critic (ProhibitAutomaticExportation)
+our @EXPORT = qw/purge db user_list user_entry problem_list problem_entry problem_full contest_list contest_entry contest_full contest_has_problem job_list job_entry job_full create_job standings update_status rerun_job take_job finish_job/; ## no critic (ProhibitAutomaticExportation)
use JSON::MaybeXS qw/decode_json/;
use HTTP::Tiny;
opens_sth => 'SELECT problem,owner,time FROM opens WHERE contest = ?',
problem_entry_sth => 'SELECT ' . (join ',', @{PROBLEM_PUBLIC_COLUMNS()}, 'statement', 'solution') . ' FROM problems WHERE id = ?',
+ problem_full_sth => 'SELECT * FROM problems WHERE id = ?',
limits_sth => 'SELECT format,timeout FROM limits WHERE problem = ?',
problem_values_sth => 'SELECT id,value FROM problems',
job_entry_sth => 'SELECT * FROM job_entry WHERE id = ?',
job_full_sth => 'SELECT * FROM jobs WHERE id = ?',
+
+ rerun_job_sth => 'UPDATE jobs SET daemon=NULL,result=-2,result_text=NULL,results=NULL,errors=NULL WHERE id = ?',
+ take_job_sth => 'UPDATE jobs SET daemon=? WHERE id = (SELECT id FROM jobs WHERE daemon IS NULL LIMIT 1 FOR UPDATE) RETURNING id',
);
our $db;
+sub db () { $db }
sub init {
$db = DBIx::Simple->new(@_);
sub query {
my ($stat, @extra) = @_;
- $db->query($statements{$stat} // $stat, @extra)
+ $db->query($statements{$stat}, @extra)
}
my (%name_cache, %name_cache_time);
}
-sub add_names ($) {
+sub add_names ($) { ## no critic (ProhibitSubroutinePrototypes)
my ($el) = @_;
+ return unless defined $el;
if (ref $el eq 'ARRAY') {
- &add_names ($_) for @$el
+ &add_names ($_) for @$el ## no critic (ProhibitAmpersandSigils)
} else {
for my $object (qw/contest owner problem/) {
my $table = $object eq 'owner' ? 'users' : "${object}s";
}
sub problem_entry {
- my ($id, $contest, $user) = @_;
- $contest &&= contest_entry $contest;
+ my ($id, $contest) = @_;
+ $contest = contest_entry ($contest) if $contest;
my $ret = add_names query(problem_entry_sth => $id)->hash;
my $limits = query(limits_sth => $id)->hashes;
$ret->{limits} = $limits if @$limits;
$ret
}
+sub problem_full {
+ my ($id) = @_;
+ scalar query(problem_full_sth => $id)->hash;
+}
+
sub contest_list {
my $ret = add_names query('contest_list_sth')->hashes;
sub job_entry {
my ($id) = @_;
my $ret = add_names query(job_entry_sth => $id)->hash;
- $ret->{results} &&= decode_json $ret->{results};
+ $ret->{results} = decode_json $ret->{results} if $ret->{results};
$ret
}
sub create_job {
my (%args) = @_;
- $db->update('users', {lastjob => time});
+ $db->update('users', {lastjob => time}, {id => $args{owner}});
purge '/log/';
scalar $db->insert('jobs', \%args, {returning => 'id'})->list
}
sub calc_score {
my ($mxscore, $time, $tries, $totaltime) = @_;
my $score = $mxscore;
- $time = 0 if $time < 0;
- $time = 300 if $time > $totaltime;
+ $time = 300 if $time > $totaltime; # uncoverable branch true does not happen anymore (only possible if opens are broken)
$score = ($totaltime - $time) / $totaltime * $score;
$score -= $tries / 10 * $mxscore;
$score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10;
my @problems = query(contest_problems_sth => $ct->{id})->flat;
my $pblist = problem_list;
my %values = query('problem_values_sth')->map;
-# $values{$_} = $values{$_}->{value} for keys %values;
my (%scores, %tries, %opens);
my $opens = query(opens_sth => $ct->{id});
while (my $job = $jobs->hash) {
my $open = $opens{$job->{problem}, $job->{owner}} // $ct->{start};
my $time = $job->{date} - $open;
- next if $time < 0;
+ next if $time < 0; # uncoverable branch true job sent before contest is deprecated
my $value = $values{$job->{problem}};
my $factor = $job->{result} ? 0 : 1;
$factor = $1 / 100 if $job->{result_text} =~ /^(\d+ )/s;
}
sub update_status {
- my $jobs = $db->select('jobs', 'id,owner,problem,result', {}, 'id');
+ my $jobs = $db->select('jobs', 'id,owner,problem,result', {-not_bool => 'private'}, 'id');
my %hash;
while ($jobs->into(my ($id, $owner, $problem, $result))) {
$db->commit
}
+sub rerun_job {
+ my ($id) = @_;
+ query rerun_job_sth => $id;
+ purge '/log/';
+ purge "/log/$id";
+}
+
+sub take_job {
+ my ($daemon) = @_;
+ my $id = query(take_job_sth => $daemon)->list;
+ return $id ? job_full $id : undef;
+}
+
+sub finish_job {
+ my ($job, $private, %args) = @_;
+ db->update(jobs => \%args, {id => $job->{id}});
+ return if $private;
+ my $status = {
+ problem => $job->{problem},
+ owner => $job->{owner},
+ job => $job->{id},
+ solved => ($args{result} ? 0 : 1),
+ };
+ eval {
+ db->insert(problem_status => $status)
+ } or db->update(problem_status => $status, {owner => $job->{owner}, problem => $job->{problem}});
+}
+
my @PURGE_HOSTS = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
my $ht = HTTP::Tiny->new;