Panoptes/Base.pm untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 211 CC 10 SCAR 21.4
CC
10
Cov
0%
modParam
30.0
parseAllConfigParams
12.0
getFuncmapByFuncid
6.0
CRAP 8.5
1 package Helios::Panoptes::Base;
2  
3 use 5.008;
4 use strict;
5 use warnings;
6 use base qw(CGI::Application);
7 use Data::Dumper;
8  
9 use CGI::Application::Plugin::DBH qw(dbh_config dbh);
10 use Data::ObjectDriver::Driver::DBI;
11  
12 use Helios::Service;
13 use Helios::Error;
14  
15 our $VERSION = '1.51_2830';
16  
17 our $CONF_PARAMS;
18 our %FUNCMAPBYID = ();
19 our %FUNCMAPBYNAME = ();
20  
21 # date structures
22 our @MON_LIST = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
23 our @MONTH_LIST = qw(January February March April May June July August September October November December);
24 our @DAY_LIST = qw(Sun Mon Tue Wed Thu Fri Sat);
25  
26 # log level structures
27 our @LOG_LEVEL_LIST = ('EMERG','ALERT','CRIT','ERROR','WARN','NOTICE','INFO','DEBUG');
28 our %LOG_LEVEL_MAP = (
29 'LOG_EMERG' => 0,
30 'LOG_ALERT' => 1,
31 'LOG_CRIT' => 2,
32 'LOG_ERR' => 3,
33 'LOG_WARNING' => 4,
34 'LOG_NOTICE' => 5,
35 'LOG_INFO' => 6,
36 'LOG_DEBUG' => 7
37 );
38  
39 =head1 NAME
40  
41 Helios::Panoptes::Base - base class for Helios::Panoptes applications
42  
43 =head1 DESCRIPTION
44  
45 Helios::Panoptes::Base serves as the base class for the different web
46 applications that make up Helios::Panoptes.
47  
48 =head1 CGI::APPLICATION METHODS
49  
50 =head2 setup()
51  
52 =cut
53  
54 sub setup {
55 die "NOT IMPLEMENTED";
56 }
57  
58  
59 =head2 teardown()
60  
61 The only thing that currently happens in teardown() is the database is disconnected.
62  
63 =cut
64  
65 sub teardown {
66 $_[0]->dbh->disconnect();
67 }
68  
69 =head1 COMMON METHODS
70  
71 These methods
72  
73 =head2 getConfig()
74  
75 =cut
76  
77 sub getConfig { return $CONF_PARAMS; }
78  
79  
80 =head2 getFuncmapByFuncid()
81  
82 =cut
83  
84 sub getFuncmapByFuncid {
85 my $self = shift;
86 if (keys %FUNCMAPBYID) { return %FUNCMAPBYID; }
87 my $dbh = $self->dbh();
88  
89 my $sth = $dbh->prepare_cached("SELECT funcid, funcname FROM funcmap");
90 $sth->execute();
91 my $rs = $sth->fetchall_arrayref();
92 $sth->finish();
93
94 foreach (@$rs) {
95 $FUNCMAPBYID{$_->[0]} = $_->[1];
96 }
97
98 return %FUNCMAPBYID;
99 }
100  
101 =head2 getFuncmapByFuncname()
102  
103 =cut
104  
105 sub getFuncmapByFuncname {
106 my $self = shift;
107 if (keys %FUNCMAPBYNAME) { return %FUNCMAPBYNAME; }
108 my $dbh = $self->dbh();
109  
110 my $sth = $dbh->prepare_cached("SELECT funcname, funcid FROM funcmap");
111 $sth->execute();
112 my $rs = $sth->fetchall_arrayref();
113 $sth->finish();
114
115 foreach (@$rs) {
116 $FUNCMAPBYNAME{$_->[0]} = $_->[1];
117 }
118
119 return %FUNCMAPBYNAME;
120 }
121  
122  
123 =head2 getLogLevelList()
124  
125 =head2 getLogLevelMap()
126  
127 =cut
128  
129 sub getLogLevelList { return @LOG_LEVEL_LIST; }
130 sub getLogLevelMap { return %LOG_LEVEL_MAP; }
131  
132  
133 =head2 initDriver()
134  
135 =cut
136  
137 sub initDriver {
138 my $self = shift;
139 my $config = $self->getConfig();
140 my $driver = Data::ObjectDriver::Driver::DBI->new(
141 dsn => $config->{dsn},
142 username => $config->{user},
143 password => $config->{password}
144 );
145 return $driver;
146 }
147  
148  
149 =head2 parseEpochDate($epoch_seconds)
150  
151 Given a time in epoch seconds, parseEpochDate() returns a hashref containing
152 the component date parts of the date on the Greogorian calendar:
153  
154 yyyy four-digit year
155 mm two-digit month
156 mon 3-letter month
157 month Month full name
158 day 3-letter day of week
159 ddd 3-digit day of year
160 dd two-digit day of month
161 hh twenty-four hour
162 mi two-digit minutes
163 ss two-digit seconds
164  
165 =cut
166  
167 sub parseEpochDate {
168 my $self = shift;
169 my $e = shift;
170 my $dhash;
171 my @p = localtime($e);
172 $dhash->{ss} = sprintf("%02d", $p[0]);
173 $dhash->{mi} = sprintf("%02d", $p[1]);
174 $dhash->{hh} = sprintf("%02d", $p[2]);
175 $dhash->{dd} = sprintf("%02d", $p[3]);
176 $dhash->{mm} = sprintf("%02d", $p[4] + 1);
177 $dhash->{mon} = $MON_LIST[ $p[4] ];
178 $dhash->{month} = $MONTH_LIST[ $p[4] ];
179 $dhash->{yyyy} = sprintf("%04d", $p[5] + 1900);
180 $dhash->{day} = $DAY_LIST[ $p[6] ];
181 $dhash->{ddd} = sprintf("%03d", $p[7] + 1);
182  
183 return $dhash;
184 }
185  
186  
187 # BEGIN CODE Copyright (C) 2008-9 by CEB Toolbox, Inc.
188  
189 # These methods have been modified from the original code extracted from
190 # Helios::Panoptes 1.44 to better fit operation in H::P 1.5x.
191  
192 =head2 parseConfig()
193  
194 =cut
195  
196 sub parseConfig {
197 my $self = shift;
198 my $inifile;
199 if (defined($ENV{HELIOS_INI}) ) {
200 $inifile = $ENV{HELIOS_INI};
201 } else {
202 $inifile = './helios.ini';
203 }
204 $self->{service} = Helios::Service->new();
205 $self->{service}->prep();
206 my $config = $self->{service}->getConfig();
207 $CONF_PARAMS = $config;
208 return $CONF_PARAMS;
209 }
210  
211 =head2 parseAllConfigParams([$group_by_field])
212  
213 Returns a hashref data structure containing all of the config params for all of
214 the services in the Helios collective database. The $group_by_field can be
215 either 'service' or 'host' ('service' is the default).
216  
217 =cut
218  
219 sub parseAllConfigParams {
220 my $self = shift;
221 my $group_by_field = @_ ? shift : 'service';
222 my $dbh = $self->dbh();
223 my $order_by_field;
224 my $order_by_field2;
225 my $config;
226  
227 if ($group_by_field eq 'service') {
228 $order_by_field = 'worker_class';
229 $order_by_field2 = 'host';
230 } else {
231 $order_by_field = 'host';
232 $order_by_field2 = 'worker_class';
233 }
234  
235 my $sql = "SELECT $order_by_field, $order_by_field2, param, value FROM helios_params_tb ORDER BY $order_by_field, $order_by_field2";
236 my $sth = $dbh->prepare_cached($sql);
237 $sth->execute();
238 my $rs = $sth->fetchall_arrayref();
239 $sth->finish();
240
241 foreach (@$rs) {
242 $config->{ $_->[0] }->{ $_->[1] }->{ $_->[2] } = $_->[3];
243 }
244  
245 return $config;
246 }
247  
248  
249 =head2 modParam($action, $worker_class, $host, $param, [$value])
250  
251 Modify Helios config parameters. Used by ctrl_panel() and collective() displays.
252  
253 Valid values for $action:
254  
255 =over 4
256  
257 =item add
258  
259 Add the given parameter for the given class and host
260  
261 =item delete
262  
263 Delete the given parameter for the given class and host
264  
265 =item modify
266  
267 Modify the given parameter for the given class and host with a new value. Effectively the same
268 as a delete followed by an add.
269  
270 =back
271  
272 Worker class is the name of the class.
273  
274 Host is the name of the host. Use '*' to make the parameter global to all instances of the worker
275 class.
276  
277 Returns a true value if successful and throws an Error::Simple exception otherwise.
278  
279 =cut
280  
281 sub modParam {
282 my $self = shift;
283 my $dbh = $self->dbh();
284 my $action = shift;
285 my $worker_class = shift;
286 my $host = shift;
287 my $param = shift;
288 my $value = shift;
289
290 my $sql;
291  
292 unless ($worker_class && $host && $param && $action) {
293 throw Error::Simple("Worker class ($worker_class), host ($host), param ($param), and action ($action) required");
294 }
295  
296 SWITCH: {
297 if ($action eq 'add') {
298 $sql = 'INSERT INTO helios_params_tb (host, worker_class, param, value) VALUES (?,?,?,?)';
299 $dbh->do($sql, undef, $host, $worker_class, $param, $value) or throw Error::Simple('modParam add FAILURE: '.$dbh->errstr);
300 last SWITCH;
301 }
302 if ($action eq 'del') {
303 $sql = 'DELETE FROM helios_params_tb WHERE host = ? AND worker_class = ? AND param = ?';
304 $dbh->do($sql, undef, $host, $worker_class, $param) or throw Error::Simple('modParam delete FAILURE: '.$dbh->errstr);
305 last SWITCH;
306 }
307 if ($action eq 'mod') {
308 $self->modParam('del', $worker_class, $host, $param);
309 $self->modParam('add', $worker_class, $host, $param, $value);
310 last SWITCH;
311 }
312 throw Error::Simple("modParam invalid action: $action");
313 }
314  
315 return 1;
316 }
317  
318  
319 # END CODE Copyright CEB Toolbox, Inc.
320  
321  
322  
323 1;
324 __END__