diff --git a/lib/Spectre/Workflow.pm b/lib/Spectre/Workflow.pm index ab2679506..aa94413ac 100644 --- a/lib/Spectre/Workflow.pm +++ b/lib/Spectre/Workflow.pm @@ -288,36 +288,52 @@ sub getJsonStatus { my $queue = $queues{$queueName}; my $count = $queue->get_item_count; + # list of instances to be added to the %output structure in the event + # that a site name is provided + my @instances; + # and if there are items in that queue, add them to our data structure if ($count > 0) { - QUEUEITEM: - foreach my $queueItem ($queue->peek_items(sub {1})) { - my($priority, $id, $instance) = @{$queueItem}; - - # if we were provided a sitename, keep the original behavior of - # only returning the data for that site. - if($sitename) { - next QUEUEITEM unless $instance->{sitename} eq $sitename; + # if a site name is provided, only process data for that site name, + # and only construct data for that site name + if($sitename ne '') { + foreach my $itemAref ($queue->peek_items(sub { shift()->{sitename} eq $sitename })) { + push @instances, $itemAref; } + $output{$queueName} = \@instances; + } - # The site's name in the list of %output keys isn't a hashref; - # we haven't seen it yet - if(ref $output{$instance->{sitename}} ne 'HASH') { - $output{$instance->{sitename}} = {}; + # otherwise, process data for all sites. + else { + foreach my $queueItem ($queue->peek_items(sub {1})) { + my($priority, $id, $instance) = @{$queueItem}; + + # The site's name in the list of %output keys isn't a hashref; + # we haven't seen it yet + if(ref $output{$instance->{sitename}} ne 'HASH') { + $output{$instance->{sitename}} = {}; + } + + # The queue name in the $output{sitename} hashref isn't an + # arrayref; we haven't seen it yet + if(ref $output{$instance->{sitename}}{$queueName} ne 'ARRAY') { + $output{$instance->{sitename}}{$queueName} = []; + } + + # calculate originalPriority separately + $instance->{originalPriority} = ($instance->{priority} - 1) * 10; + + # finally, add the instance to the returned data structure + push @{$output{$instance->{sitename}}{$queueName}}, $instance; } - - # The queue name in the $output{sitename} hashref isn't an - # arrayref; we haven't seen it yet - if(ref $output{$instance->{sitename}}{$queueName} ne 'ARRAY') { - $output{$instance->{sitename}}{$queueName} = []; - } - - # calculate originalPriority separately - $instance->{originalPriority} = ($instance->{priority} - 1) * 10; - - # finally, add the instance to the returned data structure - push @{$output{$instance->{sitename}}{$queueName}}, $instance; + } + } + # there's no items in this queue, but the version of this call that + # accepts a sitename expects an empty array ref anyway. Give it one. + else { + if($sitename) { + $output{$queueName} = \@instances; } } } diff --git a/t/Spectre/Workflow.t b/t/Spectre/Workflow.t index bd5391241..4d34c618a 100755 --- a/t/Spectre/Workflow.t +++ b/t/Spectre/Workflow.t @@ -27,9 +27,8 @@ use WRE::Spectre; use POE::Component::IKC::ClientLite; use JSON; use Config::JSON; -use Data::Dumper; -use Test::More tests => 20; # increment this value for each test you create +use Test::More tests => 19; # increment this value for each test you create $|++; @@ -85,7 +84,6 @@ SKIP: { # then check it for the old style, single site result structure ok($oneSiteStructure = jsonToObj($oneSiteResult), 'workflow/getJsonStatus for one site returns a proper JSON data structure'); isa_ok($oneSiteStructure, 'HASH', 'workflow/getJsonStatus for one site returns a JSON structure parseable into a Perl hashref'); - ok(exists $oneSiteStructure->{$sitename}, "$sitename exists in one site result structure"); } # Not sure how to handle these; the problem is that there may or may not be @@ -98,7 +96,7 @@ TODO: { ok(exists $allSitesStructure->{$sitename}{$key}, "$key exists for $sitename in all sites structure"); isa_ok($allSitesStructure->{$sitename}{$key}, 'ARRAY', "$key is an arrayref in the $sitename hash in all sites structure"); - ok(exists $oneSiteStructure->{$sitename}{$key}, "$key exists for $sitename in one site structure"); - isa_ok($oneSiteStructure->{$sitename}{$key}, 'ARRAY', "$key is an arrayref in the $sitename hash in one site structure"); + ok(exists $oneSiteStructure->{$key}, "$key exists for $sitename in one site structure"); + isa_ok($oneSiteStructure->{$key}, 'ARRAY', "$key is an arrayref in the $sitename hash in one site structure"); } }