Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Sprinklings of prefetch. Cut down massively on DB queries.
  • Loading branch information
davorg committed Sep 19, 2015
1 parent bdc8900 commit f415dfc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
23 changes: 17 additions & 6 deletions Kinza/lib/Kinza.pm
Expand Up @@ -61,11 +61,15 @@ hook before_template => sub {
$params->{domain} = $ENV{KZ_DOMAIN};
$params->{reg_live} = $reg_live;
$params->{sel_live} = $sel_live;
if (session('email')) {
if (session('email') and ! $params->{student}) {
$params->{student} = $rs{Student}->find({
email => session('email')
}, {
prefetch => { form => 'year' },
});
} else {
}

if (!session('email')) {
delete $params->{student};
}
};
Expand All @@ -88,6 +92,8 @@ get '/' => sub {
if (session('email')) {
$student = $rs{Student}->find({
email => session('email'),
}, {
prefetch => [{ attendances => 'presentation' }, { form => 'year' } ],
});
}

Expand All @@ -96,7 +102,7 @@ get '/' => sub {
}

my @terms = $rs{Term}->search({}, {
prefetch => { presentations => 'attendances' },
prefetch => { presentations => ['course', 'attendances'] },
order_by => 'seq',
})->all;

Expand All @@ -111,7 +117,6 @@ get '/' => sub {
error => $error,
choices => $choices,
student => $student,
courses => [ $rs{Course}->all ],
terms => \@terms,
term_course => \%term_course,
};
Expand All @@ -128,8 +133,14 @@ post '/save' => sub {
my $terms = 0;
my %courses;
my @unavailable;
foreach (keys %params) {
my $pres = $rs{Presentation}->find({ id => $params{$_} });

my @pres = $rs{Presentation}->search({
'me.id' => [ values %params],
}, {
prefetch => [ 'course', 'term', 'attendances' ],
});

foreach my $pres (@pres) {
$terms += $pres->number_of_terms;
$courses{$pres->course->id} = 1;
push @unavailable, $pres->course->title . ' (' . $pres->term->name . ')'
Expand Down
2 changes: 1 addition & 1 deletion Kinza/lib/Kinza/Schema/Result/Student.pm
Expand Up @@ -171,7 +171,7 @@ sub sorted_attendances {
my $self = shift;

return $self->attendances->search({}, {
join => { presentation => 'term' },
prefetch => { presentation => ['term', 'course'] },
order_by => 'term.seq',
});
}
Expand Down
2 changes: 1 addition & 1 deletion Kinza/views/index.tt
Expand Up @@ -42,7 +42,7 @@
IF ! skip_terms; THEN;
SET p = term_course.${t.id}.${c.id};
IF p; THEN -%]
<td class="option"[% IF p.number_of_terms > 1; THEN %] colspan=[% p.number_of_terms; END %] title="[% t.name %]"><input type="radio" class="C[% c.id %][% FOREACH i IN [1 .. p.number_of_terms]; SET id = t.id+loop.index; ' T' _ id; END %]" name="T[% p.term.seq %]" value="[% p.id %]"[% IF choices.${p.id} %] checked[% ELSIF p.full %] disabled[% END %]></td>
<td class="option"[% IF p.number_of_terms > 1; THEN %] colspan=[% p.number_of_terms; END %] title="[% t.name %]"><input type="radio" class="C[% c.id %][% FOREACH i IN [1 .. p.number_of_terms]; SET id = t.id+loop.index; ' T' _ id; END %]" name="T[% t.seq %]" value="[% p.id %]"[% IF choices.${p.id} %] checked[% ELSIF p.full %] disabled[% END %]></td>
[% SET skip_terms = p.number_of_terms - 1;
ELSE -%]
<td class="option" title="[% t.name %]">&nbsp;</td>
Expand Down

0 comments on commit f415dfc

Please sign in to comment.