blib/lib/Zabbix7/API/Graph.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 163 CC 4 SCAR 10.9
CC
4
Cov
0%
add_items
6.0
id
6.0
url
6.0
CRAP 3.0
1 package Zabbix7::API::Graph;
2
3 use strict;
4 use warnings;
5 use 5.010;
6 use Carp;
7 use autodie;
8 use utf8;
9
10 use URI;
11 use Params::Validate qw/validate :types/;
12
13 use Moo;
14 extends qw/Exporter Zabbix7::API::CRUDE/;
15
16 # extracted from frontends/php/include/defines.inc.php
17 use constant {
18 GRAPH_TYPE_NORMAL => 0,
19 GRAPH_TYPE_STACKED => 1,
20 GRAPH_TYPE_PIE => 2,
21 GRAPH_TYPE_EXPLODED => 3,
22 };
23
24 our @EXPORT_OK = qw/GRAPH_TYPE_NORMAL GRAPH_TYPE_STACKED GRAPH_TYPE_PIE GRAPH_TYPE_EXPLODED/;
25
26 our %EXPORT_TAGS = (
27 graphtypes => [ qw/GRAPH_TYPE_NORMAL GRAPH_TYPE_STACKED GRAPH_TYPE_PIE GRAPH_TYPE_EXPLODED/ ]
28 );
29
30 use Zabbix7::API::GraphItem;
31
32 has 'graphitems' => (is => 'rw');
33 has 'color_wheel' => (is => 'ro', clearer => 1, lazy => 1, builder => '_build_color_wheel');
34
35 sub make_color_wheel {
36 my ($class, $colors) = @_;
37 return sub {
38 state $iterator = 0;
39 my $color = $colors->[$iterator];
40 $iterator++;
41 $iterator = $iterator % scalar(@{$colors});
42 return $color;
43 };
44 }
45
46 sub _build_color_wheel {
47 my $self = shift;
48 return $self->make_color_wheel([qw/1C1CCC 1CCC1C CC1C1C FDFD49 9A1C9A 1CCCCC FD8C1C/]);
49 }
50
51 sub add_items {
52 my ($self, @items) = @_;
53 my @graphitems = map {
54 Zabbix7::API::GraphItem->new(
55 root => $self->root,
56 data => {
57 itemid => $_->id,
58 color => $self->color_wheel->(),
59 }
60 )
61 } @items;
62 if ($self->id) {
63 push @{$self->graphitems}, @graphitems;
64 } else {
65 $self->graphitems(\@graphitems);
66 }
67 Log::Any->get_logger->debug("Added " . scalar @graphitems . " graph items to graph ID: " . ($self->id // 'new'));
68 return @graphitems;
69 }
70
71 sub id {
72 my ($self, $value) = @_;
73 if (defined $value) {
74 $self->data->{graphid} = $value;
75 return $self->data->{graphid};
76 }
77 return $self->data->{graphid};
78 }
79
80 sub _readonly_properties {
81 return {
82 graphid => 1,
83 flags => 1,
84 templateid => 1,
85 };
86 }
87
88 sub _prefix {
89 my ($class, $suffix) = @_;
90 return 'graph' . ($suffix // '');
91 }
92
93 sub _extension {
94 return (
95 output => 'extend',
96 selectHosts => ['hostid', 'host'], # Updated for Zabbix 7.0
97 selectGraphItems => 'extend',
98 );
99 }
100
101 sub name {
102 my $self = shift;
103 return $self->data->{name} || '???';
104 }
105
106 sub url {
107 my $self = shift;
108 my %args = validate(@_, {
109 width => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
110 height => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
111 period => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
112 stime => { type => SCALAR, optional => 1, regex => qr/^\d{14}$/ }, # Changed from start_time
113 });
114
115 my $base_url = $self->{root}->{server};
116 my $url = URI->new($base_url);
117 my @path_segments = $url->path_segments;
118
119 # Zabbix 7.0 uses zabbix.php with action parameter
120 $path_segments[-1] = 'zabbix.php';
121 $url->path_segments(@path_segments);
122
123 my %query_params = (
124 action => 'charts.view',
125 view_as => ($self->data->{type} == GRAPH_TYPE_NORMAL || $self->data->{type} == GRAPH_TYPE_STACKED)
126 ? 'showgraph' : 'showvalues',
127 filter_graphid => $self->id,
128 %args,
129 );
130 $url->query_form(%query_params);
131
132 Log::Any->get_logger->debug("Generated URL for graph ID: " . $self->id . ": $url");
133 return $url;
134 }
135
136 sub _map_graphitems_to_property {
137 my ($self) = @_;
138 $self->data->{gitems} = [ map { $_->data } @{$self->graphitems // []} ];
139 Log::Any->get_logger->debug("Mapped " . scalar(@{$self->data->{gitems}}) . " graph items to property for graph ID: " . ($self->id // 'new'));
140 return;
141 }
142
143 sub _map_property_to_graphitems {
144 my ($self) = @_;
145 my @graphitems = map {
146 Zabbix7::API::GraphItem->new(root => $self->root, data => $_)
147 } @{$self->data->{gitems} // []};
148 $self->graphitems(\@graphitems);
149 Log::Any->get_logger->debug("Mapped " . scalar(@graphitems) . " graph items from property for graph ID: " . ($self->id // 'new'));
150 return;
151 }
152
153 before 'create' => \&_map_graphitems_to_property;
154 before 'update' => \&_map_graphitems_to_property;
155 after 'pull' => \&_map_property_to_graphitems;
156 around 'new' => sub {
157 my ($orig, @rest) = @_;
158 my $graph = $orig->(@rest);
159 $graph->_map_property_to_graphitems;
160 return $graph;
161 };
162
163 1;
164 __END__