]> iEval git - gruntmaster-data.git/blobdiff - lib/Gruntmaster/Data.pm
Rename init to dbinit and export it
[gruntmaster-data.git] / lib / Gruntmaster / Data.pm
index 2706f94338f643e17bd84c71f1b8e3cfb9ae1965..bcc0085580c5fdb6d6ea2a0ce769604ec59d3a0e 100644 (file)
@@ -1,10 +1,10 @@
 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/dbinit purge db user_list user_entry problem_list problem_entry contest_list contest_entry contest_has_problem job_list job_entry create_job standings update_status rerun_job take_job finish_job/; ## no critic (ProhibitAutomaticExportation)
 
 use JSON::MaybeXS qw/decode_json/;
 use HTTP::Tiny;
@@ -27,22 +27,22 @@ my %statements = (
 
        contest_list_sth => 'SELECT * FROM contest_entry',
        contest_entry_sth => 'SELECT * FROM contest_entry WHERE id = ?',
-       contest_full_sth => 'SELECT * FROM contests WHERE id = ?',
-       contest_problems_sth => 'SELECT problem FROM contest_problems JOIN problems pb ON problem=pb.id WHERE contest = ? ORDER BY pb.value',
        contest_has_problem_sth => 'SELECT EXISTS(SELECT 1 FROM contest_problems WHERE contest = ? AND problem = ?)',
        opens_sth => 'SELECT problem,owner,time FROM opens WHERE contest = ?',
 
        problem_entry_sth => 'SELECT ' . (join ',', @{PROBLEM_PUBLIC_COLUMNS()}, 'statement', 'solution') . ' 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 {
+sub dbinit {
        $db = DBIx::Simple->new(@_);
        $db->keep_statements = 100;
 };
@@ -51,7 +51,7 @@ sub purge;
 
 sub query {
        my ($stat, @extra) = @_;
-       $db->query($statements{$stat} // $stat, @extra)
+       $db->query($statements{$stat}, @extra)
 }
 
 my (%name_cache, %name_cache_time);
@@ -70,10 +70,11 @@ sub object_name {
 }
 
 
-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";
@@ -105,19 +106,12 @@ sub problem_list {
        $where{owner} = $args{owner} if $args{owner};
 
        my $table = $args{contest} ? 'problems JOIN contest_problems cp ON cp.problem = id' : 'problems';
-       my $ret = add_names $db->select(\$table, \@columns, \%where, 'name')->hashes;
-
-       my %params;
-       for (@$ret) {
-               $params{$_->{level}} //= [];
-               push @{$params{$_->{level}}}, $_
-       }
-       \%params
+       add_names $db->select(\$table, \@columns, \%where, 'name')->hashes
 }
 
 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;
@@ -125,42 +119,21 @@ sub problem_entry {
        if ($contest) {
                $ret->{contest_start} = $contest->{start};
                $ret->{contest_stop}  = $contest->{stop};
+               delete $ret->{solution}
        }
 
        $ret
 }
 
-sub contest_list {
-       my $ret = add_names query('contest_list_sth')->hashes;
-
-       my %ret;
-       for (@$ret) {
-               my $state = $_->{finished} ? 'finished' : $_->{started} ? 'running' : 'pending';
-               $ret{$state} //= [];
-               push @{$ret{$state}}, $_;
-       }
+sub contest_list { add_names query('contest_list_sth')->hashes }
 
-       \%ret
-}
+sub contest_entry { add_names query(contest_entry_sth => $_[0])->hash }
 
-sub contest_entry {
-       my ($id) = @_;
-       add_names query(contest_entry_sth => $id)->hash;
-}
-
-sub contest_full {
-       my ($id) = @_;
-       scalar query(contest_full_sth => $id)->hash;
-}
-
-sub contest_has_problem {
-       my ($contest, $problem) = @_;
-       query('contest_has_problem_sth', $contest, $problem)->flat
-}
+sub contest_has_problem { query('contest_has_problem_sth', @_[0, 1])->flat }
 
 sub job_list {
        my (%args) = @_;
-       $args{page} //= 1;
+       $args{page} = int ($args{page} // 1);
        my %where = (
                maybe contest => $args{contest},
                maybe owner => $args{owner},
@@ -172,42 +145,33 @@ sub job_list {
        my $rows = $db->select('job_entry', 'COUNT(*)', \%where)->list;
        my $pages = int (($rows + JOBS_PER_PAGE - 1) / JOBS_PER_PAGE);
        my ($stmt, @bind) = $db->abstract->select('job_entry', '*', \%where, {-desc => 'id'});
-       my $jobs = $db->query("$stmt LIMIT " . JOBS_PER_PAGE . ' OFFSET ' . ($args{page} - 1) * JOBS_PER_PAGE, @bind)->hashes;
-       my %ret = (
-               log => add_names $jobs,
+       my $jobs = add_names $db->query("$stmt LIMIT " . JOBS_PER_PAGE . ' OFFSET ' . ($args{page} - 1) * JOBS_PER_PAGE, @bind)->hashes;
+       my $pageinfo = {
                current_page => $args{page},
                last_page    => $pages,
-       );
-       $ret{previous_page} = $args{page} - 1 if $args{page} - 1;
-       $ret{next_page} = $args{page} + 1 if $args{page} < $pages;
-
-       \%ret;
+               ($args{page} - 1) ? (previous_page => $args{page} - 1) : (),
+               ($args{page} < $pages) ? (next_page => $args{page} + 1) : (),
+       };
+       wantarray ? ($jobs, $pageinfo) : $jobs;
 }
 
 sub job_entry {
-       my ($id) = @_;
-       my $ret = add_names query(job_entry_sth => $id)->hash;
-       $ret->{results} &&= decode_json $ret->{results};
+       my $ret = add_names query(job_entry_sth => $_[0])->hash;
+       $ret->{results} = decode_json $ret->{results} if $ret->{results};
        $ret
 }
 
-sub job_full {
-       my ($id) = @_;
-       scalar query(job_full_sth => $id)->hash
-}
-
 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 {
+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;
@@ -216,13 +180,10 @@ sub calc_score {
 
 sub standings {
        my ($ct) = @_;
+       my @problems = sort { $a->{value} <=> $b->{value} } @{problem_list contest => $ct};
+       my %values = map { $_->{id} => $_->{value} } @problems;
        $ct = contest_entry $ct;
 
-       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 ($opens->into(my ($problem, $owner, $time))) {
@@ -234,11 +195,11 @@ sub standings {
        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;
-               $scores{$job->{owner}}{$job->{problem}} = int ($factor * calc_score ($value, $time, $tries{$job->{owner}}{$job->{problem}}++, $ct->{stop} - $ct->{start}));
+               $scores{$job->{owner}}{$job->{problem}} = int ($factor * _calc_score ($value, $time, $tries{$job->{owner}}{$job->{problem}}++, $ct->{stop} - $ct->{start}));
        }
 
        my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock)
@@ -247,20 +208,18 @@ sub standings {
                        user => $user,
                        user_name => object_name(users => $user),
                        score => sum (values %{$scores{$user}}),
-                       scores => [map { $scores{$user}{$_} // '-'} @problems],
+                       scores => [map { $scores{$user}{$_->{id}} // '-'} @problems],
                }
        } keys %scores;
 
        $st[0]->{rank} = 1 if @st;
        $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st;
-       +{
-               st => \@st,
-               problems => [map { [ $_, object_name(problems => $_)] } @problems],
-       }
+
+       \@st
 }
 
 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))) {
@@ -271,7 +230,7 @@ sub update_status {
 
        my @contest_statuses = map {
                my $ct = $_;
-               map { [$ct, $_->{user}, $_->{score}, $_->{rank}] } @{standings($ct)->{st}}
+               map { [$ct, $_->{user}, $_->{score}, $_->{rank}] } @{standings $ct}
        } $db->select('contests', 'id')->flat;
 
        $db->begin;
@@ -282,6 +241,34 @@ sub update_status {
        $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 ? db->select(jobs => '*', {id => $id})->hash : 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;
 
This page took 0.026385 seconds and 4 git commands to generate.