| 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/Exporter Zabbix2::API::CRUDE/; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | # extracted from frontends/php/include/defines.inc.php | ||||||||||||||||||||||||||||||||
| 17 | use constant { | ||||||||||||||||||||||||||||||||
| 18 | 1 | GRAPH_TYPE_NORMAL => 0, | |||||||||||||||||||||||||||||||
| 19 | GRAPH_TYPE_STACKED => 1, | ||||||||||||||||||||||||||||||||
| 20 | GRAPH_TYPE_PIE => 2, | ||||||||||||||||||||||||||||||||
| 21 | GRAPH_TYPE_EXPLODED => 3, | ||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 1 | our @EXPORT_OK = qw/GRAPH_TYPE_NORMAL GRAPH_TYPE_STACKED GRAPH_TYPE_PIE GRAPH_TYPE_EXPLODED/; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 1 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||
| 27 | graphtypes => [ qw/GRAPH_TYPE_NORMAL GRAPH_TYPE_STACKED GRAPH_TYPE_PIE GRAPH_TYPE_EXPLODED/ ] | ||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Zabbix2::API::GraphItem; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 1 | has 'graphitems' => (is => 'rw'); | |||||||||||||||||||||||||||||||
| 33 | 1 | has 'color_wheel' => (is => 'ro', | |||||||||||||||||||||||||||||||
| 34 | clearer => 1, | ||||||||||||||||||||||||||||||||
| 35 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 36 | builder => '_build_color_wheel'); | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | sub make_color_wheel { | ||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my ($class, $colors) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
make_color_wheel: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 40 | return sub { | ||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | state $iterator = 0; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 42 | 0 | my $color = $colors->[$iterator]; | |||||||||||||||||||||||||||||||
| 43 | 0 | $iterator++; | |||||||||||||||||||||||||||||||
| 44 | 0 | $iterator = $iterator % scalar(@{$colors}); | |||||||||||||||||||||||||||||||
| 45 | 0 | return $color; | |||||||||||||||||||||||||||||||
| 46 | 0 | }; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | sub _build_color_wheel { | ||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_build_color_wheel: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 51 | # TODO pick nicer colors | ||||||||||||||||||||||||||||||||
| 52 | 0 | return $self->make_color_wheel([qw/1C1CCC 1CCC1C CC1C1C FDFD49 9A1C9A 1CCCCC FD8C1C/]); | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | sub add_items { | ||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ($self, @items) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
add_items: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 57 | 0 | my @graphitems = map { Zabbix2::API::GraphItem->new( | |||||||||||||||||||||||||||||||
| 58 | root => $self->root, | ||||||||||||||||||||||||||||||||
| 59 | data => { itemid => $_->id, | ||||||||||||||||||||||||||||||||
| 60 | color => $self->color_wheel->() }) } @items; | ||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($self->id) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | # has been sync'd with server at least once. | ||||||||||||||||||||||||||||||||
| 63 | 0 | push @{$self->graphitems}, @graphitems; | |||||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||||||
| 65 | # assume graphitems is empty, avoids fetching the graphitems | ||||||||||||||||||||||||||||||||
| 66 | # list which we cannot do without a graphid | ||||||||||||||||||||||||||||||||
| 67 | 0 | $self->graphitems(\@graphitems); | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 0 | return @graphitems; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | sub id { | ||||||||||||||||||||||||||||||||
| 73 | ## mutator for id | ||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 0 | $self->data->{graphid} = $value; | |||||||||||||||||||||||||||||||
| 77 | 0 | return $self->data->{graphid}; | |||||||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||||||
| 79 | 0 | return $self->data->{graphid}; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | sub _readonly_properties { | ||||||||||||||||||||||||||||||||
| 84 | ## hash of item properties that cannot be updated; they will be | ||||||||||||||||||||||||||||||||
| 85 | ## removed before pushing the item to the server | ||||||||||||||||||||||||||||||||
| 86 | return { | ||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | graphid => 1, | ||||||||||||||||||||||||||||||
|
Subroutine
_readonly_properties: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 88 | flags => 1, | ||||||||||||||||||||||||||||||||
| 89 | templateid => 1, | ||||||||||||||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | sub _prefix { | ||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my ($class, $suffix) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_prefix: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($suffix) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 0 | return 'graph'.$suffix; | |||||||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||||||
| 97 | 0 | return 'graph'; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | sub _extension { | ||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return (output => 'extend', | ||||||||||||||||||||||||||||||
|
Subroutine
_extension: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 103 | selectHosts => ['hostid'], | ||||||||||||||||||||||||||||||||
| 104 | selectGraphItems => 'extend'); | ||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | sub name { | ||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return shift->data->{name} || '???'; | ||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
+(shift())->data->{'name'} || '???'
MC/DC0%0/1
+(shift())->data->{'name'} || '???'
+(shift())->data->{'name'}
Truth table0%0/2
A
A= +(shift())->data->{'name'}
| |||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | sub url { | ||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
url: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 113 | 0 | my $base_url = $self->{root}->{server}; | |||||||||||||||||||||||||||||||
| 114 | 0 | my %args = validate(@_, { width => { type => SCALAR, optional => 1, regex => qr/^\d+$/ }, | |||||||||||||||||||||||||||||||
| 115 | height => { type => SCALAR, optional => 1, regex => qr/^\d+$/ }, | ||||||||||||||||||||||||||||||||
| 116 | period => { type => SCALAR, optional => 1, regex => qr/^\d+$/ }, | ||||||||||||||||||||||||||||||||
| 117 | start_time => { type => SCALAR, optional => 1, regex => qr/^\d{14}$/ } }); | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 0 | my $url = URI->new($base_url); | |||||||||||||||||||||||||||||||
| 120 | 0 | my @path_segments = $url->path_segments; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | # replace api_jsonrpc.php with the chart generation page | ||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($self->data->{graphtype} == GRAPH_TYPE_NORMAL | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$self->data->{"graphtype"} == 0 or $self->data->{"graphtype"} == 1
MC/DC0%0/2
$self->data->{"graphtype"} == 0 or $self->data->{"graphtype"} == 1
$self->data->{"graphtype"} == 0
$self->data->{"graphtype"} == 1
Truth table0%0/3
A or B
A= $self->data->{"graphtype"} == 0B= $self->data->{"graphtype"} == 1
| |||||||||||||||||||||||||||||||||
| 124 | or $self->data->{graphtype} == GRAPH_TYPE_STACKED) { | ||||||||||||||||||||||||||||||||
| 125 | 0 | $path_segments[-1] = 'chart2.php'; | |||||||||||||||||||||||||||||||
| 126 | ▶ | } elsif ($self->data->{graphtype} == GRAPH_TYPE_PIE | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->data->{"graphtype"} == 2 or $self->data->{"graphtype"} == 3
MC/DC0%0/2
$self->data->{"graphtype"} == 2 or $self->data->{"graphtype"} == 3
$self->data->{"graphtype"} == 2
$self->data->{"graphtype"} == 3
Truth table0%0/3
A or B
A= $self->data->{"graphtype"} == 2B= $self->data->{"graphtype"} == 3
| |||||||||||||||||||||||||||||||||
| 127 | or $self->data->{graphtype} == GRAPH_TYPE_EXPLODED) { | ||||||||||||||||||||||||||||||||
| 128 | 0 | $path_segments[-1] = 'chart6.php'; | |||||||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||||||
| 130 | croak(sprintf(q{Unknown graph type %d, cannot guess URL}, | ||||||||||||||||||||||||||||||||
| 131 | 0 | $self->data->{graphtype})); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 0 | $url->path_segments(@path_segments); | |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 0 | $url->query_form(graphid => $self->id, %args); | |||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 0 | return $url; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | sub _map_graphitems_to_property { | ||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_map_graphitems_to_property: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 143 | 0 | $self->data->{gitems} = [ map { $_->data } @{$self->graphitems} ]; | |||||||||||||||||||||||||||||||
| 144 | 0 | return; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | sub _map_property_to_graphitems { | ||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_map_property_to_graphitems: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 149 | 0 | my @graphitems = map { Zabbix2::API::GraphItem->new(root => $self->root, | |||||||||||||||||||||||||||||||
| 150 | 0 | data => $_) } @{$self->data->{gitems}}; | |||||||||||||||||||||||||||||||
| 151 | 0 | $self->graphitems(\@graphitems); | |||||||||||||||||||||||||||||||
| 152 | 0 | return; | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 1 | before 'create' => \&_map_graphitems_to_property; | |||||||||||||||||||||||||||||||
| 156 | 1 | before 'update' => \&_map_graphitems_to_property; | |||||||||||||||||||||||||||||||
| 157 | 1 | after 'pull' => \&_map_property_to_graphitems; | |||||||||||||||||||||||||||||||
| 158 | around 'new' => sub { | ||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my ($orig, @rest) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 160 | 0 | my $graph = $orig->(@rest); | |||||||||||||||||||||||||||||||
| 161 | 0 | $graph->_map_property_to_graphitems; | |||||||||||||||||||||||||||||||
| 162 | 0 | return $graph; | |||||||||||||||||||||||||||||||
| 163 | 1 | }; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 1 | 1; | |||||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||||||