API/Graph.pm

filter: Statementstmt 21.4% 18 / 84 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 6 / 15 Podpod 100% 9 / 9 totaltotal 23.9% 33 / 138 CC 15 SCAR 26.9
CC
15
Cov
16%
push
42.0
url
20.0
pull
12.0
CRAP 14.7
« API/Macro.pm ↑ Summary API/Screen.pm »
1 package Zabbix::API::Graph;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Params::Validate qw/validate :types/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use parent qw/Zabbix::API::CRUDE/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
12 0 my ($class, %args) = @_;
13  
14 0 my $self = $class->SUPER::new(%args);
15  
16 0 ▶ $self->data->{gitems} = [] unless defined $self->data->{gitems};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->data->{"gitems"} 0 0 0
17  
18 0 return $self;
19 }
20  
21  
22 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
23  
24 ## mutator for id
25  
26 0 my ($self, $value) = @_;
27  
28 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
29  
30 0 $self->data->{graphid} = $value;
31 0 return $self->data->{graphid};
32  
33 } else {
34  
35 0 return $self->data->{graphid};
36  
37 }
38  
39 }
40  
41 0 ▶sub prefix {
Subroutine prefix: not called CC=2 SCAR=17.9
Pod documented
42  
43 0 my (undef, $suffix) = @_;
44  
45 0 ▶ if ($suffix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix) { } 0 0 0
46  
47 0 return 'graph'.$suffix;
48  
49 } else {
50  
51 0 return 'graph';
52  
53 }
54  
55 }
56  
57 0 ▶sub extension {
Subroutine extension: not called CC=1 SCAR=6.9
Pod documented
58  
59 0 return ( output => 'extend',
60 select_graph_items => 'extend' );
61  
62 }
63  
64 0 ▶sub collides {
Subroutine collides: not called CC=1 SCAR=6.9
Pod documented
65  
66 0 my $self = shift;
67  
68 0 return @{$self->{root}->query(method => $self->prefix('.get'),
69 params => { filter => { name => $self->data->{name} },
70 0 $self->extension })};
71  
72 }
73  
74 0 ▶sub items {
Subroutine items: not called CC=2 SCAR=17.9
Pod documented
75  
76 ## mutator for graph_items
77  
78 0 my ($self, $value) = @_;
79  
80 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
81  
82 0 $self->data->{gitems} = $value;
83 0 return $self->data->{gitems};
84  
85 } else {
86  
87 0 return $self->data->{gitems};
88  
89 }
90  
91 }
92  
93 0 ▶sub url {
Subroutine url: not called CC=4 SCAR=30.0
Pod documented
94  
95 ## return url for a graph
96  
97 0 my $self = shift;
98  
99 0 my $base_url = $self->{root}->{server};
100 0 $base_url =~ s{(?:/api_jsonrpc\.php)?$}{};
101  
102 0 my %args = validate(@_, { width => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
103 period => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
104 start_time => { type => SCALAR, optional => 1, regex => qr/^\d{14}$/ } });
105  
106 0 my $url = $base_url.'/chart2.php?graphid='.$self->id;
107 0 ▶ $url .= '&width='.$args{width} if $args{width};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"width"} 0 0 0
108 0 ▶ $url .= '&period='.$args{period} if $args{period};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"period"} 0 0 0
109 0 ▶ $url .= '&stime='.$args{start_time} if $args{start_time};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"start_time"} 0 0 0
110  
111 0 return $url;
112  
113 }
114  
115 0 ▶sub push {
Subroutine push: not called CC=6 SCAR=37.4
Pod documented
116  
117 # override CRUDE's push()
118  
119 0 my ($self, $data) = @_;
120  
121 0 ▶ $data //= $self->data;
Condition0%0/3
$data //= $self->data
l!l&&r!l&&!r
000
MC/DC0%0/2
$data //= $self->data
$data $self->data
Truth table0%0/3
A //= B
A= $dataB= $self->data
ABresult
1X1
011
000
122  
123 0 foreach my $item (@{$data->{gitems}}) {
124  
125 0 ▶ if (exists $item->{item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"item"}) 0 0 0
126  
127 0 ▶ if (eval { $item->{item}->isa('Zabbix::API::Item') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $item->{'item'}->isa('Zabbix::API::Item') } }) { } 0 0 0
128  
129 0 $item->{item}->push;
130  
131 0 $item->{itemid} = $item->{item}->id;
132  
133 } else {
134  
135 0 croak 'Type mismatch: item attribute should be an instance of Zabbix::API::Item';
136  
137 }
138  
139 }
140  
141 }
142  
143 # copying the anonymous hashes so we can delete stuff without touching the
144 # originals
145 0 my $gitems_copy = [ map { { %{$_} } } @{$data->{gitems}} ];
146  
147 0 foreach my $item (@{$gitems_copy}) {
148  
149 0 delete $item->{item};
150  
151 }
152  
153 # copying the data hashref so we can replace its gitems with the fake
154 0 my $data_copy = { %{$data} };
155  
156 # the old switcheroo
157 0 $data_copy->{gitems} = $gitems_copy;
158  
159 0 return $self->SUPER::push($data_copy);
160  
161 }
162  
163 0 ▶sub pull {
Subroutine pull: not called CC=3 SCAR=24.8
Pod documented
164  
165 # override CRUDE's pull()
166  
167 0 my ($self, $data) = @_;
168  
169 0 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 0 0 0
170  
171 0 $self->{data} = $data;
172  
173 } else {
174  
175 0 my %stash = map { $_->id => $_ } grep { eval { $_->isa('Zabbix::API::Item') } } @{$self->items};
176  
177 0 $self->SUPER::pull;
178  
179 ## no critic (ProhibitCommaSeparatedStatements)
180 # restore stashed items that have been removed by pulling
181 $self->items(
182 [map {
183 0 ▶ { %{$_},
Condition0%0/3
$stash{$_->{"itemid"}} // "Zabbix::API::Item"->new("root", $self->{"root"}, "data", {"itemid", $_->{"itemid"}})->pull
l!l&&r!l&&!r
000
MC/DC0%0/2
$stash{$_->{"itemid"}} // "Zabbix::API::Item"->new("root", $self->{"root"}, "data", {"itemid", $_->{"itemid"}})->pull
$stash{$_->{"itemid"}} "Zabbix::API::Item"->new("root", $self->{"root"}, "data", {"itemid", $_->{"itemid"}})->pull
Truth table0%0/3
A // B
A= $stash{$_->{"itemid"}}B= "Zabbix::API::Item"->new("root", $self->{"root"}, "data", {"itemid", $_->{"itemid"}})->pull
ABresult
1X1
011
000
184 item =>
185 $stash{$_->{itemid}} // Zabbix::API::Item->new(root => $self->{root},
186 0 data => { itemid => $_->{itemid} })->pull
187 }
188 }
189 0 @{$self->items}]
190 );
191 ## use critic
192  
193 }
194  
195 0 return $self;
196  
197 }
198  
199 1 1;
200 __END__
« API/Macro.pm ↑ Summary API/Screen.pm »