| 1 | package Helios::Panoptes::Helper; | ||
| 2 | |||
| 3 | use 5.008000; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Data::Dumper; | ||
| 7 | use DBI; | ||
| 8 | use Helios::Service; | ||
| 9 | |||
| 10 | our $VERSION = '1.40'; | ||
| 11 | our $dbh; | ||
| 12 | our %config; | ||
| 13 | |||
| 14 | my $inifile; | ||
| 15 | if (defined($ENV{HELIOS_INI}) ) { | ||
| 16 | $inifile = $ENV{HELIOS_INI}; | ||
| 17 | } else { | ||
| 18 | $inifile = './helios.ini'; | ||
| 19 | } | ||
| 20 | my $worker = new Helios::Service; | ||
| 21 | %config = $worker->getConfigFromIni($inifile); | ||
| 22 | |||
| 23 | $dbh = DBI->connect($config{dsn}, $config{user}, $config{password}); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | Helios::Panoptes::Helper - helper methods used by Helios::Panoptes::ErrorLog and Helios::Panoptes::JobLog | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | This module contains helper functions for the ErrorLog and JobLog run mode modules. | ||
| 32 | |||
| 33 | =head1 FUNCTIONS | ||
| 34 | |||
| 35 | =head2 splitEpochDate($epoch_seconds) | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub splitEpochDate { | ||
| 40 | my $epoch_secs = shift; | ||
| 41 | |||
| 42 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoch_secs); | ||
| 43 | |||
| 44 | my $return_date; | ||
| 45 | |||
| 46 | $return_date->{YYYY} = sprintf("%04d", $year + 1900); | ||
| 47 | $return_date->{MM} = sprintf("%02d", $mon+1); | ||
| 48 | $return_date->{DD} = sprintf("%02d", $mday); | ||
| 49 | $return_date->{MI} = sprintf("%02d", $min); | ||
| 50 | $return_date->{SS} = sprintf("%02d", $sec); | ||
| 51 | |||
| 52 | # hours | ||
| 53 | if ($hour == 12) { | ||
| 54 | $return_date->{AMPM} = 'PM'; | ||
| 55 | $return_date->{HH12} = '12'; | ||
| 56 | $return_date->{HH24} = '12'; | ||
| 57 | $return_date->{HH} = '12'; | ||
| 58 | } elsif ($hour == 0) { | ||
| 59 | $return_date->{AMPM} = 'AM'; | ||
| 60 | $return_date->{HH12} = '12'; | ||
| 61 | $return_date->{HH24} = '00'; | ||
| 62 | $return_date->{HH} = '12'; | ||
| 63 | } elsif ($hour > 12) { | ||
| 64 | $return_date->{AMPM} = 'PM'; | ||
| 65 | $return_date->{HH12} = sprintf("%02d", $hour - 12); | ||
| 66 | $return_date->{HH24} = sprintf("%02d", $hour); | ||
| 67 | $return_date->{HH} = sprintf("%02d", $hour); | ||
| 68 | } else { | ||
| 69 | # hour is AM | ||
| 70 | $return_date->{AMPM} = 'AM'; | ||
| 71 | $return_date->{HH12} = sprintf("%02d", $hour); | ||
| 72 | $return_date->{HH24} = sprintf("%02d", $hour); | ||
| 73 | $return_date->{HH} = sprintf("%02d", $hour); | ||
| 74 | } | ||
| 75 | |||
| 76 | return $return_date; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | =head2 db_fetch_all($sql, @params) | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub db_fetch_all | ||
| 85 | { | ||
| 86 | my $sql = shift; | ||
| 87 | my @params = @_; | ||
| 88 | |||
| 89 | my @result; | ||
| 90 | |||
| 91 | my $sth = $dbh->prepare($sql); | ||
| 92 | unless($sth) { throw Error::Simple($dbh->errstr); } | ||
| 93 | |||
| 94 | $sth->execute(@params) or throw Error::Simple($dbh->errstr()); | ||
| 95 | |||
| 96 | while ( my $result = $sth->fetchrow_hashref() ) | ||
| 97 | { | ||
| 98 | push @result, $result; | ||
| 99 | } | ||
| 100 | |||
| 101 | return @result; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | =head2 db_fetch_row($sql, @params) | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub db_fetch_row | ||
| 110 | { | ||
| 111 | my $sql = shift; | ||
| 112 | my @params = @_; | ||
| 113 | |||
| 114 | my @result; | ||
| 115 | |||
| 116 | my $sth = $dbh->prepare($sql); | ||
| 117 | unless($sth) { throw Error::Simple($dbh->errstr); } | ||
| 118 | |||
| 119 | $sth->execute(@params) or throw Error::Simple($dbh->errstr()); | ||
| 120 | |||
| 121 | return $sth->fetchrow_hashref(); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | =head2 option_function_map() | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub option_function_map { | ||
| 130 | my $funcmap; | ||
| 131 | |||
| 132 | my $sql = " | ||
| 133 | SELECT | ||
| 134 | funcid, | ||
| 135 | funcname | ||
| 136 | FROM | ||
| 137 | funcmap | ||
| 138 | ORDER BY | ||
| 139 | LOWER(funcname) ASC | ||
| 140 | "; | ||
| 141 | |||
| 142 | return db_fetch_all($sql); | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | =head2 pagination($current_page, $number_per_page, $total_entries, $q, $max_pages) | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub pagination | ||
| 151 | { | ||
| 152 | my $cur_page = shift; | ||
| 153 | my $per_page = shift; | ||
| 154 | my $total_entries = shift; | ||
| 155 | my $q = shift; | ||
| 156 | my $max_pages = shift; | ||
| 157 | |||
| 158 | $max_pages ||= 10; | ||
| 159 | |||
| 160 | return "" if($total_entries <= $per_page); | ||
| 161 | |||
| 162 | my $num_pages = int($total_entries / $per_page); | ||
| 163 | |||
| 164 | my $start_page = $cur_page - int($max_pages / 2); | ||
| 165 | my $end_page = $cur_page + int($max_pages / 2); | ||
| 166 | |||
| 167 | $end_page -= $start_page if($start_page < 0); | ||
| 168 | $start_page -= $end_page - $num_pages if $end_page > $num_pages; | ||
| 169 | |||
| 170 | $start_page = 0 if $start_page < 0; | ||
| 171 | $end_page = $num_pages if $end_page > $num_pages; | ||
| 172 | |||
| 173 | my $short_back = $cur_page - $max_pages; | ||
| 174 | $short_back = 0 if $short_back < 0; | ||
| 175 | |||
| 176 | my $short_forward = $cur_page + $max_pages; | ||
| 177 | $short_forward = $num_pages - 1 if $short_forward > $num_pages; | ||
| 178 | |||
| 179 | my $j=0; | ||
| 180 | |||
| 181 | my $html = "<div class='pagination'><ul>"; | ||
| 182 | |||
| 183 | if($start_page > 0) | ||
| 184 | { | ||
| 185 | $html .= "<li class='page'><a href='$q&page=0'><<</a></li>"; | ||
| 186 | $html .= "<li class='page'><a href='$q&page=$short_back'><</a></li>"; | ||
| 187 | } | ||
| 188 | |||
| 189 | for($j=$start_page;$j<$end_page;$j++) | ||
| 190 | { | ||
| 191 | if($j != $cur_page) | ||
| 192 | { | ||
| 193 | $html .= "<li class='page'><a href='$q&page=$j'>" . ($j+1) . "</a></li>"; | ||
| 194 | } | ||
| 195 | else | ||
| 196 | { | ||
| 197 | $html .= "<li class='page_sel'>" . ($j+1) . "</li>"; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | if($end_page < $num_pages-1) | ||
| 202 | { | ||
| 203 | $html .= "<li class='page'><a href='$q&page=$short_forward'>></a></li>"; | ||
| 204 | $html .= "<li class='page'><a href='$q&page=" . ($num_pages-1) . "'>>></a></li>"; | ||
| 205 | } | ||
| 206 | |||
| 207 | $html .= "</ul></div>"; | ||
| 208 | |||
| 209 | return $html; | ||
| 210 | |||
| 211 | |||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | =head2 option_priority() | ||
| 216 | |||
| 217 | Returns a list of available log priorities and their associated text labels. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub option_priority | ||
| 222 | { | ||
| 223 | return ( | ||
| 224 | -1 => 'All', | ||
| 225 | 0 => 'Emergency', | ||
| 226 | 1 => 'Alert', | ||
| 227 | 2 => 'Critical', | ||
| 228 | 3 => 'Error', | ||
| 229 | 4 => 'Warning', | ||
| 230 | 5 => 'Notice', | ||
| 231 | 6 => 'Information', | ||
| 232 | 7 => 'Debug', | ||
| 233 | ); | ||
| 234 | } | ||
| 235 | |||
| 236 | =head2 option_time_horizon() | ||
| 237 | |||
| 238 | Returns a list of time horizon options, with associated text labels. | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | sub option_time_horizon | ||
| 243 | { | ||
| 244 | return ( | ||
| 245 | 60 => '1 Minute', | ||
| 246 | 300 => '5 Minutes', | ||
| 247 | 900 => '15 Minutes', | ||
| 248 | 1800 => '30 Minutes', | ||
| 249 | 3600 => '1 Hour', | ||
| 250 | 7200 => '2 Hours', | ||
| 251 | 14400 => '4 Hours', | ||
| 252 | 28800 => '8 Hours', | ||
| 253 | 57600 => '16 Hours', | ||
| 254 | 86400 => '1 Day', | ||
| 255 | 172800 => '2 Days', | ||
| 256 | 259200 => '3 Days', | ||
| 257 | 604800 => '1 Week', | ||
| 258 | ); | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | =head2 option_limit() | ||
| 263 | |||
| 264 | Returns a list of entry limits for each Error Log page. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub option_limit | ||
| 269 | { | ||
| 270 | return ( | ||
| 271 | 25 => '25', | ||
| 272 | 50 => '50', | ||
| 273 | 100 => '100', | ||
| 274 | 200 => '200', | ||
| 275 | 1000 => '1000' | ||
| 276 | ); | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | =head2 option_search() | ||
| 281 | |||
| 282 | Returns a list of optional search fields in the database, with associated labels. | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub option_search | ||
| 287 | { | ||
| 288 | return ( | ||
| 289 | message => 'Message', | ||
| 290 | jobid => 'Job ID', | ||
| 291 | host => 'Host', | ||
| 292 | process_id => 'Process ID', | ||
| 293 | ); | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | =head2 hash_to_pair($hash, $mode) | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | sub hash_to_pair | ||
| 302 | { | ||
| 303 | my $hash = shift; | ||
| 304 | my $mode = shift; | ||
| 305 | |||
| 306 | $mode ||= 'num'; | ||
| 307 | |||
| 308 | my $key; | ||
| 309 | my $value; | ||
| 310 | |||
| 311 | my %hash = %$hash; | ||
| 312 | |||
| 313 | my @options = (); | ||
| 314 | |||
| 315 | if($mode eq 'num') | ||
| 316 | { | ||
| 317 | foreach(sort { $a <=> $b } keys %hash) | ||
| 318 | { | ||
| 319 | $key = $_; | ||
| 320 | $value = $hash{$_}; | ||
| 321 | push @options,{key => $key, value => $value}; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | else | ||
| 325 | { | ||
| 326 | foreach(sort { $a cmp $b } keys %hash) | ||
| 327 | { | ||
| 328 | $key = $_; | ||
| 329 | $value = $hash{$_}; | ||
| 330 | push @options,{key => $key, value => $value}; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | return \@options; | ||
| 335 | |||
| 336 | } | ||
| 337 | 1; | ||
| 338 | __END__ |