File Coverage

blib/lib/Zabbix2/API/Screen.pm
Criterion Covered Total %
statement 26 70 37.1
branch 0 14 0.0
condition 0 4 0.0
subroutine 9 18 50.0
pod 4 4 100.0
total 39 110 35.4


line stmt bran cond sub pod time code
1             package Zabbix2::API::Screen;
2              
3 1     1   459 use strict;
  1         1  
  1         26  
4 1     1   3 use warnings;
  1         1  
  1         20  
5 1     1   18 use 5.010;
  1         2  
6 1     1   3 use Carp;
  1         2  
  1         53  
7 1     1   418 use autodie;
  1         11900  
  1         5  
8 1     1   4298 use utf8;
  1         9  
  1         4  
9              
10 1     1   27 use Moo::Lax;
  1         1  
  1         7  
11             extends qw/Exporter Zabbix2::API::CRUDE/;
12              
13 1     1   493 use List::Util qw/max/;
  1         1  
  1         96  
14              
15             # extracted from frontends/php/include/defines.inc.php
16             use constant {
17 1         929 SCREEN_RESOURCE_GRAPH => 0,
18             SCREEN_RESOURCE_SIMPLE_GRAPH => 1,
19             SCREEN_RESOURCE_MAP => 2,
20             SCREEN_RESOURCE_PLAIN_TEXT => 3,
21             SCREEN_RESOURCE_HOSTS_INFO => 4,
22             SCREEN_RESOURCE_TRIGGERS_INFO => 5,
23             SCREEN_RESOURCE_SERVER_INFO => 6,
24             SCREEN_RESOURCE_CLOCK => 7,
25             SCREEN_RESOURCE_SCREEN => 8,
26             SCREEN_RESOURCE_TRIGGERS_OVERVIEW => 9,
27             SCREEN_RESOURCE_DATA_OVERVIEW => 10,
28             SCREEN_RESOURCE_URL => 11,
29             SCREEN_RESOURCE_ACTIONS => 12,
30             SCREEN_RESOURCE_EVENTS => 13,
31             SCREEN_RESOURCE_HOSTGROUP_TRIGGERS => 14,
32             SCREEN_RESOURCE_SYSTEM_STATUS => 15,
33             SCREEN_RESOURCE_HOST_TRIGGERS => 16,
34 1     1   4 };
  1         1  
35              
36             our @EXPORT_OK = qw/SCREEN_RESOURCE_GRAPH SCREEN_RESOURCE_SIMPLE_GRAPH SCREEN_RESOURCE_MAP SCREEN_RESOURCE_PLAIN_TEXT SCREEN_RESOURCE_HOSTS_INFO SCREEN_RESOURCE_TRIGGERS_INFO SCREEN_RESOURCE_SERVER_INFO SCREEN_RESOURCE_CLOCK SCREEN_RESOURCE_SCREEN SCREEN_RESOURCE_TRIGGERS_OVERVIEW SCREEN_RESOURCE_DATA_OVERVIEW SCREEN_RESOURCE_URL SCREEN_RESOURCE_ACTIONS SCREEN_RESOURCE_EVENTS SCREEN_RESOURCE_HOSTGROUP_TRIGGERS SCREEN_RESOURCE_SYSTEM_STATUS SCREEN_RESOURCE_HOST_TRIGGERS/;
37              
38             our %EXPORT_TAGS = (
39             resources => [ qw/SCREEN_RESOURCE_GRAPH SCREEN_RESOURCE_SIMPLE_GRAPH SCREEN_RESOURCE_MAP SCREEN_RESOURCE_PLAIN_TEXT SCREEN_RESOURCE_HOSTS_INFO SCREEN_RESOURCE_TRIGGERS_INFO SCREEN_RESOURCE_SERVER_INFO SCREEN_RESOURCE_CLOCK SCREEN_RESOURCE_SCREEN SCREEN_RESOURCE_TRIGGERS_OVERVIEW SCREEN_RESOURCE_DATA_OVERVIEW SCREEN_RESOURCE_URL SCREEN_RESOURCE_ACTIONS SCREEN_RESOURCE_EVENTS SCREEN_RESOURCE_HOSTGROUP_TRIGGERS SCREEN_RESOURCE_SYSTEM_STATUS SCREEN_RESOURCE_HOST_TRIGGERS/ ]
40             );
41              
42             my %classes_to_constants = ('Graph' => SCREEN_RESOURCE_GRAPH,
43             'Item' => SCREEN_RESOURCE_SIMPLE_GRAPH,
44             'Map' => SCREEN_RESOURCE_MAP,
45             'Screen' => SCREEN_RESOURCE_SCREEN,
46             'HostGroup' => SCREEN_RESOURCE_HOSTGROUP_TRIGGERS,
47             'Host' => SCREEN_RESOURCE_HOST_TRIGGERS);
48              
49             my %constants_to_classes = reverse %classes_to_constants;
50              
51             sub _readonly_properties {
52             return {
53 0     0     screenid => 1,
54             };
55             }
56              
57             before 'update' => sub {
58             # the documentation makes no mention of templateid, but it does
59             # exist for screens. you can set it at creation time but then you
60             # can never update it
61             my ($self) = @_;
62             delete $self->data->{templateid};
63             };
64              
65             sub id {
66             ## mutator for id
67 0     0 1   my ($self, $value) = @_;
68 0 0         if (defined $value) {
69 0           $self->data->{screenid} = $value;
70 0           return $self->data->{screenid};
71             } else {
72 0           return $self->data->{screenid};
73             }
74             }
75              
76             sub _prefix {
77 0     0     my (undef, $suffix) = @_;
78 0 0         if ($suffix) {
79 0           return 'screen'.$suffix;
80             } else {
81 0           return 'screen';
82             }
83             }
84              
85             sub _extension {
86 0     0     return (output => 'extend',
87             selectScreenItems => 'extend');
88             }
89              
90             sub _screenitem_to_object {
91 0     0     my ($self, $resource) = @_;
92 0 0         if (my $class = $constants_to_classes{$resource->{resourcetype}}) {
93             # only macros need an instance to call _prefix() so this should
94             # be safe
95 0           $class =~ s/^(?:Zabbix2::API::)?/Zabbix2::API::/;
96 0           return $self->root->fetch_single($class, params => { $class->_prefix('ids') => [ $resource->{resourceid} ]});
97             } else {
98             # can't map this!
99 0           return $resource;
100             }
101             }
102              
103             sub _object_to_screenitem {
104 0     0     my ($self, $object, %options) = @_;
105 0           my $resourcetype = $classes_to_constants{$object->short_class};
106 0 0         if (defined $resourcetype) {
107             return {
108 0           resourcetype => $resourcetype,
109             resourceid => $object->id,
110             %options,
111             };
112             } else {
113             # not mapped. assume it's not an object at all, merge with
114             # the options, hope for the best
115 0           return { %{$object}, %options };
  0            
116             }
117             }
118              
119             sub items {
120 0     0 1   my $self = shift;
121 0           return [ map { $self->_screenitem_to_object($_) } @{$self->data->{screenitems}} ];
  0            
  0            
122             }
123              
124             sub get_item_at {
125 0     0 1   my ($self, %options) = @_;
126 0           return map { $self->_screenitem_to_object($_) }
127 0 0         grep { $_->{x} == $options{x} and $_->{y} == $options{y} }
128 0           @{$self->data->{screenitems}};
  0            
129             }
130              
131             sub set_item_at {
132 0     0 1   my ($self, $object, %options) = @_;
133             # change hsize and vsize accordingly
134             # find if an existing screenitem needs to be replaced
135 0 0         my $candidate = grep { $_->{x} == $options{x} and $_->{y} == $options{y} }
136 0           @{$self->data->{screenitems}};
  0            
137 0 0         if ($candidate) {
138 0           %{$candidate} = %{$self->_object_to_screenitem($object, %options)};
  0            
  0            
139             } else {
140 0           push @{$self->data->{screenitems}}, $self->_object_to_screenitem($object, %options);
  0            
141             }
142 0   0       $self->data->{hsize} = max($self->data->{hsize} // 0, $options{x} + 1);
143 0   0       $self->data->{vsize} = max($self->data->{vsize} // 0, $options{y} + 1);
144 0           return $self;
145             }
146              
147             1;
148             __END__