File Coverage

blib/lib/SIAM/Report.pm
Criterion Covered Total %
statement 35 52 67.3
branch 6 12 50.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 48 72 66.6


line stmt bran cond sub pod time code
1             package SIAM::Report;
2              
3 2     2   11 use warnings;
  2         5  
  2         79  
4 2     2   13 use strict;
  2         3  
  2         72  
5              
6 2     2   9 use base 'SIAM::Object';
  2         4  
  2         189  
7 2     2   2600 use JSON;
  2         34424  
  2         13  
8              
9             =head1 NAME
10              
11             SIAM::Report - Report object class
12              
13             =head1 SYNOPSIS
14              
15             my $sorted_items = $report->get_items();
16             my $item_class = $report->attr('siam.report.object_class');
17             foreach my $item (@{$sorted_items}) {
18             my $obj = $item->{'siam.report.item'};
19             my $traffic_in = $item->{'traffic.in'};
20             ...
21             }
22              
23             =head1 METHODS
24              
25             =head2 get_items
26              
27             Returns arrayref with hashes. The content of these hashes is defined by
28             the report type. Each hash has a mandatory key C
29             that points to an instantiated object.
30              
31             =cut
32              
33             sub get_items
34             {
35 1     1 1 498 my $self = shift;
36              
37 1         4 my $ret = [];
38 1         6 my $objclass = $self->attr('siam.report.object_class');
39 1         10 my $content_json = $self->computable('siam.report.content');
40 1 50       7 if( not defined($content_json) )
41             {
42 0         0 $self->error('Computable siam.report.contents returned undef');
43 0         0 return $ret;
44             }
45            
46 1         3 my $content = eval { decode_json($content_json) };
  1         16  
47 1 50       5 if( $@ )
48             {
49 0         0 $self->error('Failed to process JSON in ' .
50             'siam.report.contents computable: ' . $@);
51 0         0 return $ret;
52             }
53            
54 1 50       5 if( ref($content) ne 'ARRAY' )
55             {
56 0         0 $self->error('siam.report.contents returned non-array data');
57 0         0 return $ret;
58             }
59              
60 1         8 foreach my $item (@{$content})
  1         6  
61             {
62 2         4 my $objid = $item->{'siam.report.item_id'};
63 2 50       8 if( not defined($objid) )
64             {
65 0         0 $self->error('siam.report.contents has array element ' .
66             'without siam.report.item_id');
67 0         0 next;
68             }
69            
70 2         21 my $object = $self->instantiate_object($objclass, $objid);
71 2 50       7 if( not defined($object) )
72             {
73 0         0 $self->error('Cannot instantiate a report item "' . $objid . '"');
74 0         0 next;
75             }
76              
77 2         6 my $ret_item = {'siam.report.item' => $object};
78 2         4 while( my($key, $val) = each %{$item} )
  4         18  
79             {
80 2 50       8 if( $key ne 'siam.report.item_id' )
81             {
82 0         0 $ret_item->{$key} = $val;
83             }
84             }
85              
86 2         3 push(@{$ret}, $ret_item);
  2         9  
87             }
88            
89 1         5 return $ret;
90             }
91              
92              
93            
94            
95             # mandatory attributes
96              
97             my $mandatory_attributes =
98             [ 'siam.report.name',
99             'siam.report.description',
100             'siam.report.object_class',
101             'siam.report.type',
102             'siam.report.last_updated' ];
103              
104             sub _mandatory_attributes
105             {
106 2     2   8 return $mandatory_attributes;
107             }
108              
109              
110             sub _manifest_attributes
111             {
112 0     0     my $ret = [];
113 0           push(@{$ret}, @{$mandatory_attributes},
  0            
  0            
114 0           @{ SIAM::Reports->_manifest_attributes() });
115              
116 0           return $ret;
117             }
118              
119              
120             1;
121              
122             # Local Variables:
123             # mode: cperl
124             # indent-tabs-mode: nil
125             # cperl-indent-level: 4
126             # cperl-continued-statement-offset: 4
127             # cperl-continued-brace-offset: -4
128             # cperl-brace-offset: 0
129             # cperl-label-offset: -2
130             # End: