]>
iEval git - gruntmaster-data.git/blob - lib/Gruntmaster/Data.pm
1 package Gruntmaster
:: Data
;
4 use parent qw
/Exporter/ ;
6 use JSON qw
/encode_json decode_json/ ;
8 use Sub
:: Name qw
/subname/ ;
10 our $VERSION = '5999.000_001' ;
13 my $redis = Redis
-> new ;
14 my $pubsub = Redis
-> new ;
17 our ( $name , $sub ) = @_ ;
19 * $name = subname
$name => $sub
23 for my $cmd ( qw
/multi exec smembers get hget hdel hset sadd srem incr hmset hsetnx publish del/ ) {
24 dynsub
uc $cmd , sub { $redis -> $cmd ( @_ ) };
27 for my $cmd ( qw
/subscribe wait_for_messages/ ) {
28 dynsub
uc $cmd , sub { $pubsub -> $cmd ( @_ ) };
32 sub cp
{ defined $contest ?
"contest. $contest ." : '' }
34 sub problems
() { SMEMBERS cp
. 'problem' }
35 sub contests
() { SMEMBERS cp
. 'contest' }
36 sub users
() { SMEMBERS cp
. 'user' }
37 sub jobcard
() { GET cp
. 'job' }
39 sub job_results
( _
) { decode_json HGET cp
. "job. $_ [0]" , 'results' }
40 sub set_job_results
($+) { HSET cp
. "job. $_ [0]" , 'results' , encode_json
$_ [ 1 ] }
41 sub job_inmeta
( _
) { decode_json HGET cp
. "job. $_ [0]" , 'inmeta' }
42 sub set_job_inmeta
($+) { HSET cp
. "job. $_ [0]" , 'inmeta' , encode_json
$_ [ 1 ] }
43 sub problem_meta
( _
) { decode_json HGET cp
. "problem. $_ [0]" , 'meta' }
44 sub set_problem_meta
($+) { HSET cp
. "problem. $_ [0]" , 'meta' , encode_json
$_ [ 1 ] }
45 sub job_daemon
( _
) { HGET cp
. "job. $_ [0]" , 'daemon' }
46 sub set_job_daemon
($$) { HSETNX cp
. "job. $_ [0]" , 'daemon' , $_ [ 1 ] };
49 my ( $name , @keys ) = @_ ;
51 dynsub
"${name}_ $key " , sub ( _
) { HGET cp
. " $name . $_ [0]" , $key };
52 dynsub
"set_${name}_ $key " , sub ($$) { HSET cp
. " $name . $_ [0]" , $key , $_ [ 1 ] };
55 dynsub
"edit_ $name " , sub {
56 my ( $key , %values ) = @_ ;
57 HMSET cp
. " $name . $key " , %values ;
60 dynsub
"insert_ $name " , sub {
61 my ( $key , %values ) = @_ ;
62 SADD cp
. $name , $key or return ;
63 HMSET cp
. " $name . $key " , %values ;
65 dynsub
"remove_ $name " , sub ( _
) {
67 SREM cp
. $name , $key ;
68 DEL cp
. " $name . $key " ;
71 dynsub
"push_ $name " , sub {
72 my $nr = INCR cp
. $name ;
73 HMSET cp
. " $name . $nr " , @_ ;
78 defhash problem
=> qw
/name level statement owner author/ ;
79 defhash contest
=> qw
/start end name owner/ ;
80 defhash job
=> qw
/date errors extension filesize private problem result result_text user/ ;
81 defhash user
=> qw
/name email town university level/ ;
84 HDEL cp
. "job. $_ [0]" , qw
/result result_text results daemon/
88 my ( $problem , $user ) = @_ ;
89 HSETNX cp
. 'open' , " $problem . $user " , time ;
93 my ( $problem , $user ) = @_ ;
94 HGET cp
. 'open' , " $problem . $user " ;
99 grep { $_ =~ /^[a-zA-Z]/ and exists & $_ } keys %{ __PACKAGE__
. '::' };
109 Gruntmaster::Data - Gruntmaster 6000 Online Judge -- database interface and tools
113 for my $problem (problems) {
114 say "Problem name: " . problem_name $problem;
115 say "Problem level: " . problem_level $problem;
121 Gruntmaster::Data is the Redis interface used by the Gruntmaster 6000 Online Judge. It exports many functions for talking to the database. All functions are exported by default.
123 The current contest is selected by setting the C<< $Gruntmaster::Data::contest >> variable.
125 local $Gruntmaster::Data::contest = 'mycontest';
126 say 'There are' . jobcard . ' jobs in my contest';
132 Gruntmaster::Data exports some functions for talking directly to the Redis server. These functions should not normally be used, except for B<MULTI>, B<EXEC>, B<PUBLISH>, B<SUBSCRIBE> and B<WAIT_FOR_MESSAGES>.
134 These functions correspond to Redis commands. The current list is: B<< MULTI EXEC SMEMBERS GET HGET HDEL HSET SADD SREM INCR HMSET HSETNX DEL PUBLISH SUBSCRIBE WAIT_FOR_MESSAGES >>.
142 Returns a list of problems in the current contest.
144 =item B<problem_meta> I<$problem>
146 Returns a problem's meta.
148 =item B<set_problem_meta> I<$problem>, I<$meta>
150 Sets a problem's meta.
152 =item B<problem_name> I<$problem>
154 Returns a problem's name.
156 =item B<set_problem_name> I<$problem>, I<$name>
158 Sets a problem's name.
160 =item B<problem_level> I<$problem>
162 Returns a problem's level. The levels are beginner, easy, medium, hard.
164 =item B<set_problem_level> I<$problem>, I<$level>
166 Sets a problem's level. The levels are beginner, easy, medium, hard.
168 =item B<problem_statement> I<$problem>
170 Returns a problem's statement.
172 =item B<set_problem_statement> I<$problem>, I<$statement>
174 Sets a problem's statement.
176 =item B<problem_owner> I<$problem>
178 Returns a problem's owner.
180 =item B<set_problem_owner> I<$problem>, I<$owner>
182 Sets a problem's owner.
184 =item B<problem_author> I<$problem>
186 Returns a problem's author.
188 =item B<set_problem_author> I<$problem>, I<$author>
190 Sets a problem's author.
192 =item B<get_open> I<$problem>, I<$user>
194 Returns the time when I<$user> opened I<$problem>.
196 =item B<mark_open> I<$problem>, I<$user>
198 Sets the time when I<$user> opened I<$problem> to the current time. Does nothing if I<$user> has already opened I<$problem>.
200 =item B<insert_problem> I<$id>, I<$key> => I<$value>, ...
202 Inserts a problem with id I<$id> and the given initial configuration. Does nothing if a problem with id I<$id> already exists. Returns true if the problem was added, false otherwise.
204 =item B<edit_problem> I<$id>, I<$key> => I<$value>, ...
206 Updates the configuration of a problem. The values of the given keys are updated. All other keys/values are left intact.
208 =item B<remove_problem> I<$id>
216 B<<< WARNING: these functions only work correctly when C<< $Gruntmaster::Data::contest >> is undef >>>
222 Returns a list of contests.
224 =item B<contest_start> I<$contest>
226 Returns a contest's start time.
228 =item B<set_contest_start> I<$contest>, I<$start>
230 Sets a contest's start time.
232 =item B<contest_end> I<$contest>
234 Returns a contest's end time.
236 =item B<set_contest_end> I<$contest>, I<$end>
238 Sets a contest's end time.
240 =item B<contest_name> I<$contest>
242 Returns a contest's name.
244 =item B<set_contest_name> I<$contest>, I<$name>
246 Sets a contest's name.
248 =item B<contest_owner> I<$contest>
250 Returns a contest's owner.
252 =item B<set_contest_owner> I<$contest>, I<$owner>
254 Sets a contest's owner.
256 =item B<insert_contest> I<$id>, I<$key> => I<$value>, ...
258 Inserts a contest with id I<$id> and the given initial configuration. Does nothing if a contest with id I<$id> already exists. Returns true if the contest was added, false otherwise.
260 =item B<edit_contest> I<$id>, I<$key> => I<$value>, ...
262 Updates the configuration of a contest. The values of the given keys are updated. All other keys/values are left intact.
264 =item B<remove_contest> I<$id>
276 Returns the number of jobs in the database.
278 =item B<job_results> I<$job>
280 Returns an array of job results. Each element corresponds to a test and is a hashref with keys B<id> (test number), B<result> (result code, see L<Gruntmaster::Daemon::Constants>), B<result_text> (result description) and B<time> (time taken).
282 =item B<set_job_results> I<$job>, I<$results>
284 Sets a job's results.
286 =item B<job_inmeta> I<$job>
288 Returns a job's meta.
290 =item B<set_job_inmeta> I<$job>, I<$meta>
294 =item B<job_daemon> I<$job>
296 Returns the hostname:pid of the daemon which ran this job.
298 =item B<set_job_daemon> I<$job>, I<$hostname_and_pid>
300 If the job has no associated daemon, it sets the daemon and returns true. Otherwise it returns false without setting the daemon.
302 =item B<job_date> I<$job>
304 Returns a job's submit date.
306 =item B<set_job_date> I<$job>, I<$date>
308 Sets a job's submit date.
310 =item B<job_errors> I<$job>
312 Returns a job's compile errors.
314 =item B<set_job_errors> I<$job>, I<$errors>
316 Sets a job's compile errors.
318 =item B<job_extension> I<$job>
320 Returns a job's file name extension (e.g. "cpp", "pl", "java").
322 =item B<set_job_extension> I<$job>, I<$extension>
324 Sets a job's file name extension.
326 =item B<job_filesize> I<$job>
328 Returns a job's source file size, in bytes.
330 =item B<set_job_filesize> I<$job>, I<$filesize>
332 Sets a job's source file size, in bytes.
334 =item B<job_private> I<$job>
336 Returns the value of a job's private flag.
338 =item B<set_job_private> I<$job>, I<$private>
340 Sets the value of a job's private flag.
342 =item B<job_problem> I<$job>
344 Returns a job's problem.
346 =item B<set_job_problem> I<$job>, I<$problem>
348 Sets a job's problem.
350 =item B<job_result> I<$job>
352 Returns a job's result code. Possible result codes are described in L<Gruntmaster::Daemon::Constants>
354 =item B<set_job_result> I<$job>, I<$result>
356 Sets a job's result code.
358 =item B<job_result_text> I<$job>
360 Returns a job's result text.
362 =item B<set_job_result_text> I<$job>, I<$result_text>
364 Sets a job's result text.
366 =item B<job_user> I<$job>
368 Returns the user who submitted a job.
370 =item B<set_job_user> I<$job>, I<$user>
372 Sets the suer who submitted a job.
374 =item B<clean_job> I<$job>
376 Removes a job's daemon, result code, result text and result array.
378 =item B<push_job> I<$key> => I<$value>, ...
380 Inserts a job with a given initial configuration. Returns the id of the newly-added job.
382 =item B<edit_job> I<$id>, I<$key> => I<$value>, ...
384 Updates the configuration of a job. The values of the given keys are updated. All other keys/values are left intact.
386 =item B<remove_job> I<$id>
394 B<<< WARNING: these functions only work correctly when C<< $Gruntmaster::Data::contest >> is undef >>>
400 Returns a list of users.
402 =item B<user_name> I<$user>
404 Returns a user's full name.
406 =item B<set_user_name> I<$user>, I<$name>
408 Sets a user's full name.
410 =item B<user_email> I<$user>
412 Returns a user's email address.
414 =item B<set_user_email> I<$user>, I<$email>
416 Sets a user's email address.
418 =item B<user_town> I<$user>
420 Returns a user's town.
422 =item B<set_user_town> I<$user>, I<$town>
426 =item B<user_university> I<$user>
428 Returns a user's university/highschool/place of work/etc.
430 =item B<set_user_university> I<$user>, I<$university>
432 Sets a user's university, highschool/place of work/etc.
434 =item B<user_level> I<$user>
436 Returns a user's current level of study. One of 'Highschool', 'Undergraduate', 'Master', 'Doctorate' or 'Other'.
438 =item B<set_user_level> I<$user>, I<$level>
440 Sets a user's current level of study.
442 =item B<insert_user> I<$id>, I<$key> => I<$value>, ...
444 Inserts a user with id I<$id> and the given initial configuration. Does nothing if a user with id I<$id> already exists. Returns true if the user was added, false otherwise.
446 =item B<edit_user> I<$id>, I<$key> => I<$value>, ...
448 Updates the configuration of a user. The values of the given keys are updated. All other keys/values are left intact.
450 =item B<remove_user> I<$id>
458 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
460 =head1 COPYRIGHT AND LICENSE
462 Copyright (C) 2014 by Marius Gavrilescu
464 This library is free software: you can redistribute it and/or modify
465 it under the terms of the GNU Affero General Public License as published by
466 the Free Software Foundation, either version 3 of the License, or
467 (at your option) any later version.
This page took 0.081946 seconds and 5 git commands to generate.