From Redis to Postgres - Part 1 (Getting started)
[gruntmaster-page.git] / lib / Gruntmaster / Page / Generic.pm
index e31740d63df50734e356ac991c4a3ca848e399e1..2cb82d91d86720a5e79e3ff3573a72e83f34e9d0 100644 (file)
@@ -5,19 +5,17 @@ use strict;
 use warnings;
 our $VERSION = '5999.000_001';
 
-use Gruntmaster::Data;
 use Gruntmaster::Page::Base;
 use JSON qw/encode_json decode_json/;
+use Plack::Request;
+use Sub::Name qw/subname/;
 
-sub hgetall {
-       my $hash = shift;
-       my $cp = $Gruntmaster::Data::contest ? "contest.$Gruntmaster::Data::contest." : '';
-       map { { id => $_, HGETALL "$cp$hash.$_" } } SMEMBERS "$cp$hash"
-}
+use constant PAGE_SIZE => 10;
 
 sub putsym {
        my ($key, $value) = @_;
        no strict 'refs';
+       subname $key => $value if ref $value eq 'CODE';
        *{"$key"} = $value;
 }
 
@@ -33,10 +31,16 @@ sub makepkg {
 sub list {
        my ($thing, $lang, $env, $ct) = @_;
        my %thing = %$thing;
-       undef $ct unless $thing{contest};
+       my $req = Plack::Request->new($env);
        debug $env => "Contest is $ct";
-       local $Gruntmaster::Data::contest = $ct if $ct;
-       my @thing = hgetall $thing{hash};
+       $thing{makers} //= sub { shift->resultset($thing{rsname}) };
+       my $rs = $thing{makers}->(db $env)->search(undef, {order_by => 'me.id'});
+       if (my $page = $req->param('page')) {
+               my $pages = $rs->count / PAGE_SIZE;
+               $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))});
+       }
+       $rs = $rs->search(undef, {prefetch => $thing{prefetch}}) if exists $thing{prefetch};
+       my @thing = map +{rs => $_, $_->get_columns}, $rs->all;
        @thing = map  { $thing{mangle}->(); $_ } @thing if exists $thing{mangle};
        @thing = grep { $thing{choose}->() } @thing if exists $thing{choose};
        @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby};
@@ -53,10 +57,11 @@ sub list {
 sub entry {
        my ($thing, $lang, $env, $id, $ct) = @_;
        my %thing = %$thing;
-       ($id, $ct) = ($ct, $id) if $thing{contest};
-       local $Gruntmaster::Data::contest = $ct if $ct;
-       debug $env => "Hash is $thing{hash} and id is $id";
-       my %params = HGETALL "$thing{hash}.$id";
+       ($id, $ct) = ($ct, $id) if $ct;
+       debug $env => "Rsname is $thing{rsname} and id is $id";
+       $thing{makers} //= sub { shift->resultset($thing{rsname}) };
+       my %params = map {+ rs => $_, $_->get_columns } $thing{makers}->(db $env)->find($id);
+       $params{contest} = $ct if  $ct;
        $thing{mangle}->(local $_ = \%params) if exists $thing{mangle};
        wantarray ? %params : \%params
 }
@@ -75,21 +80,23 @@ sub create_thing {
 }
 
 sub params;
-sub contest;
+sub makers (&);
 sub choose (&);
 sub sortby (&);
 sub group  (&);
 sub mangle (&);
+sub prefetch;
 
 sub thing (&){
        my %thing;
        no strict 'refs';
-       local *{"params"} = sub { @thing{qw/id hash title/} = @_ };
+       local *{"params"} = sub { @thing{qw/id rsname title/} = @_ };
        local *{"choose"} = sub { $thing{choose} = shift };
        local *{"sortby"} = sub { $thing{sortby} = shift };
        local *{"mangle"} = sub { $thing{mangle} = shift };
        local *{"group"} = sub { $thing{group} = shift };
-       local *{"contest"} = sub { $thing{contest} = 1 };
+       local *{"makers"} = sub { $thing{makers} = shift };
+       local *{"prefetch"} = sub { $thing{prefetch} = \@_ };
        use strict 'refs';
 
        shift->();
@@ -99,30 +106,54 @@ sub thing (&){
 ##################################################
 
 thing {
-       params qw/us user Users/;
+       params qw/us User Users/;
        choose { $_->{name} =~ /\w/ };
        sortby { lc $a->{name} cmp lc $b->{name} };
 };
 
 thing {
-       params qw/pb problem Problems/;
-       contest;
-       sortby { $a->{name} cmp $b->{name} };
+       params qw/pb Problem Problems/;
+       prefetch 'owner';
+       makers { my ($db, $ct) = @_; $ct ? $db->contest($ct)->problems : $db->problems->search({private => 0}) };
+       sortby { $a->{name} cmp $b->{name}};
        group { $_->{level} };
-       mangle { $_->{owner_name} = do { local $Gruntmaster::Data::contest; user_name $_->{owner} } }
+       mangle {
+               my $env = shift;
+               $_->{owner_name} = $_->{rs}->owner->name;
+               $_->{cansubmit} = $_->{contest} ? time < $_->{rs}->contest->stop : 1;
+               eval {
+                       db($env)->open->create({
+                               contest => $_->{contest},
+                               problem => $_->{id},
+                               owner   => $env->{REMOTE_USER},
+                       })
+               } if $_->{contest} && time >= $_->{rs}->contest->start;
+       };
 };
 
 thing {
-       params qw/ct contest Contests/;
+       params qw/ct Contest Contests/;
+       prefetch 'owner';
        sortby { $b->{start} <=> $a->{start} };
-       group { time < $_->{start} ? 'pending' : time > $_->{end} ? 'finished' : 'running' };
-       mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = do { local $Gruntmaster::Data::contest; user_name $_->{owner} } };
+       group { time < $_->{start} ? 'pending' : time > $_->{stop} ? 'finished' : 'running' };
+       mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} =  $_->{rs}->owner->name };
 };
 
 thing {
-       params qw/log job/, 'Job log';
-       contest;
-       mangle { $_->{results} &&= decode_json $_->{results}; $_->{user_name} = do { local $Gruntmaster::Data::contest; user_name $_->{user} } }
+       params qw/log Job/, 'Job log';
+       prefetch 'owner', 'problem';
+       makers { shift->jobs->search({contest => shift}) };
+       sortby { $b->{id} <=> $a->{id}};
+       mangle {
+               $_->{results} &&= decode_json $_->{results};
+               $_->{owner_name} = $_->{rs}->owner->name;
+               $_->{problem_name} = $_->{rs}->problem->name;
+               $_->{size} = length $_->{source};
+               delete $_->{source};
+       }
 };
 
+putsym 'Gruntmaster::Page::Pb::Entry::vary',    sub { 'Authorization' };
+putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 };
+
 1
This page took 0.013826 seconds and 4 git commands to generate.