From 917660442ed681df7377583ca20ee71bcc31362a Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Thu, 24 Oct 2013 13:18:36 -0400 Subject: [PATCH] Add eager fetching of relations and enable that for jobset->jobsetinputs Signed-off-by: Shea Levy --- src/lib/Hydra/Component/ToJSON.pm | 5 +++++ src/lib/Hydra/Schema/JobsetInputs.pm | 10 ++++++++++ src/lib/Hydra/Schema/Jobsets.pm | 2 +- tests/api-test.pl | 8 ++++---- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/lib/Hydra/Component/ToJSON.pm b/src/lib/Hydra/Component/ToJSON.pm index 4b038401..469af78b 100644 --- a/src/lib/Hydra/Component/ToJSON.pm +++ b/src/lib/Hydra/Component/ToJSON.pm @@ -23,6 +23,11 @@ sub TO_JSON { $json{$relname} = [ map { $_->$key } $self->$relname ]; } + foreach my $relname (keys %{$hint->{eager_relations}}) { + my $key = $hint->{eager_relations}->{$relname}; + $json{$relname} = { map { $_->$key => $_ } $self->$relname }; + } + return \%json; } else { my $json = { $self->get_columns }; diff --git a/src/lib/Hydra/Schema/JobsetInputs.pm b/src/lib/Hydra/Schema/JobsetInputs.pm index c6dafde1..b67a3519 100644 --- a/src/lib/Hydra/Schema/JobsetInputs.pm +++ b/src/lib/Hydra/Schema/JobsetInputs.pm @@ -153,4 +153,14 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07033 @ 2013-10-08 13:06:15 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+mZZqLjQNwblb/EWW1alLQ +my %hint = ( + relations => { + "jobsetinputalts" => "value" + } +); + +sub json_hint { + return \%hint; +} + 1; diff --git a/src/lib/Hydra/Schema/Jobsets.pm b/src/lib/Hydra/Schema/Jobsets.pm index 1c8e63f4..4af1ee88 100644 --- a/src/lib/Hydra/Schema/Jobsets.pm +++ b/src/lib/Hydra/Schema/Jobsets.pm @@ -309,7 +309,7 @@ __PACKAGE__->has_many( # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:DTAGxP5RFvcNxP/ciJGo4Q my %hint = ( - relations => { + eager_relations => { jobsetinputs => "name" } ); diff --git a/tests/api-test.pl b/tests/api-test.pl index e4128f29..3ee4881f 100644 --- a/tests/api-test.pl +++ b/tests/api-test.pl @@ -1,6 +1,6 @@ use LWP::UserAgent; use JSON; -use Test::Simple tests => 9; +use Test::Simple tests => 10; #use Test::Simple tests => 15; my $ua = LWP::UserAgent->new; @@ -43,12 +43,12 @@ ok($result->code() == 201, "PUTting a new jobset creates it"); my $jobset = decode_json(request_json({ uri => '/jobset/sample/default' })->content()); -ok($jobset->{jobsetinputs}->[0] eq "my-src", "The new jobset has a 'my-src' input"); +ok(exists $jobset->{jobsetinputs}->{"my-src"}, "The new jobset has a 'my-src' input"); + +ok($jobset->{jobsetinputs}->{"my-src"}->{jobsetinputalts}->[0] eq "/run/jobset", "The 'my-src' input is in /run/jobset"); =begin comment -ok($jobset->{jobsetinputs}->[0]->{jobsetinputalts}->[0]->{value} eq "/run/jobset", "The 'my-src' input is in /run/jobset"); - system("LOGNAME=root NIX_STORE_DIR=/run/nix/store NIX_LOG_DIR=/run/nix/var/log/nix NIX_STATE_DIR=/run/nix/var/nix HYDRA_DATA=/var/lib/hydra HYDRA_DBI='dbi:Pg:dbname=hydra;user=root;' hydra-evaluator sample default"); $result = request_json({ uri => '/jobset/sample/default/evals' }); ok($result->code() == 200, "Can get evals of a jobset");