blib/lib/Zabbix2/API/Graph.pm

filter: Statementstmt 40.6% 37 / 91 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 43.4% 10 / 23 Podpod 100% 5 / 5 totaltotal 40.6% 52 / 128 CC 5 SCAR 10.3
CC
5
Cov
37%
add_items
6.0
id
6.0
_prefix
6.0
CRAP 2.8
« blib/lib/Zabbix2/API/Application.pm ↑ Summary blib/lib/Zabbix2/API/Action.pm »
1 package Zabbix2::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 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Params::Validate qw/validate :types/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Moo::Lax;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 extends qw/Zabbix2::API::CRUDE/;
15  
16 1 ▶use Zabbix2::API::GraphItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 has 'graphitems' => (is => 'rw');
19 1 has 'color_wheel' => (is => 'ro',
20 clearer => 1,
21 lazy => 1,
22 builder => '_build_color_wheel');
23  
24 sub make_color_wheel {
25 0 ▶ my ($class, $colors) = @_;
Subroutine make_color_wheel: not called CC=1 SCAR=6.9
Pod documented
26 return sub {
27 0 ▶ state $iterator = 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 my $color = $colors->[$iterator];
29 0 $iterator++;
30 0 $iterator = $iterator % scalar(@{$colors});
31 0 return $color;
32 0 };
33 }
34  
35 sub _build_color_wheel {
36 0 ▶ my $self = shift;
Subroutine _build_color_wheel: not called CC=1 SCAR=6.9
37 # TODO pick nicer colors
38 0 return $self->make_color_wheel([qw/1C1CCC 1CCC1C CC1C1C FDFD49 9A1C9A 1CCCCC FD8C1C/]);
39 }
40  
41 sub add_items {
42 0 ▶ my ($self, @items) = @_;
Subroutine add_items: not called CC=2 SCAR=17.9
Pod documented
43 0 my @graphitems = map { Zabbix2::API::GraphItem->new(
44 root => $self->root,
45 data => { itemid => $_->id,
46 color => $self->color_wheel->() }) } @items;
47 0 ▶ if ($self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) { } 0 0 0
48 # has been sync'd with server at least once.
49 0 push @{$self->graphitems}, @graphitems;
50 } else {
51 # assume graphitems is empty, avoids fetching the graphitems
52 # list which we cannot do without a graphid
53 0 $self->graphitems(\@graphitems);
54 }
55 0 return @graphitems;
56 }
57  
58 sub id {
59 ## mutator for id
60 0 ▶ my ($self, $value) = @_;
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
61 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
62 0 $self->data->{graphid} = $value;
63 0 return $self->data->{graphid};
64 } else {
65 0 return $self->data->{graphid};
66 }
67 }
68  
69 sub _readonly_properties {
70 ## hash of item properties that cannot be updated; they will be
71 ## removed before pushing the item to the server
72 return {
73 0 ▶ graphid => 1,
Subroutine _readonly_properties: not called CC=1 SCAR=6.9
74 flags => 1,
75 templateid => 1,
76 };
77 }
78 sub _prefix {
79 0 ▶ my ($class, $suffix) = @_;
Subroutine _prefix: not called CC=2 SCAR=17.9
80 0 ▶ if ($suffix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix) { } 0 0 0
81 0 return 'graph'.$suffix;
82 } else {
83 0 return 'graph';
84 }
85 }
86  
87 sub _extension {
88 0 ▶ return (output => 'extend',
Subroutine _extension: not called CC=1 SCAR=6.9
89 selectHosts => ['hostid'],
90 selectGraphItems => 'extend');
91 }
92  
93 sub name {
94 0 ▶ return shift->data->{name} || '???';
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->data->{'name'} || '???'
l!l
00
MC/DC0%0/1
+(shift())->data->{'name'} || '???'
+(shift())->data->{'name'}
Truth table0%0/2
A
A= +(shift())->data->{'name'}
Aresult
11
00
95 }
96  
97 sub url {
98 0 ▶ my $self = shift;
Subroutine url: not called CC=1 SCAR=6.9
Pod documented
99 0 my $base_url = $self->{root}->{server};
100 0 my %args = validate(@_, { width => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
101 height => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
102 period => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
103 start_time => { type => SCALAR, optional => 1, regex => qr/^\d{14}$/ } });
104  
105 0 my $url = URI->new($base_url);
106 0 my @path_segments = $url->path_segments;
107 # replace api_jsonrpc.php with the chart generation page
108 0 $path_segments[-1] = 'chart2.php';
109 0 $url->path_segments(@path_segments);
110  
111 0 $url->query_form(graphid => $self->id, %args);
112  
113 0 return $url;
114 }
115  
116 sub _map_graphitems_to_property {
117 0 ▶ my ($self) = @_;
Subroutine _map_graphitems_to_property: not called CC=1 SCAR=6.9
118 0 $self->data->{gitems} = [ map { $_->data } @{$self->graphitems} ];
119 0 return;
120 }
121  
122 sub _map_property_to_graphitems {
123 0 ▶ my ($self) = @_;
Subroutine _map_property_to_graphitems: not called CC=1 SCAR=6.9
124 0 my @graphitems = map { Zabbix2::API::GraphItem->new(root => $self->root,
125 0 data => $_) } @{$self->data->{gitems}};
126 0 $self->graphitems(\@graphitems);
127 0 return;
128 }
129  
130 1 before 'create' => \&_map_graphitems_to_property;
131 1 before 'update' => \&_map_graphitems_to_property;
132 1 after 'pull' => \&_map_property_to_graphitems;
133 around 'new' => sub {
134 0 ▶ my ($orig, @rest) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 my $graph = $orig->(@rest);
136 0 $graph->_map_property_to_graphitems;
137 0 return $graph;
138 1 };
139  
140 1 1;
141 __END__
« blib/lib/Zabbix2/API/Application.pm ↑ Summary blib/lib/Zabbix2/API/Action.pm »