Helios::Panoptes::Helper - helper methods used by Helios::Panoptes::ErrorLog and Helios::Panoptes::JobLog


Helios-Panoptes documentation Contained in the Helios-Panoptes distribution.

Index


Code Index:

NAME

Top

Helios::Panoptes::Helper - helper methods used by Helios::Panoptes::ErrorLog and Helios::Panoptes::JobLog

DESCRIPTION

Top

This module contains helper functions for the ErrorLog and JobLog run mode modules.

FUNCTIONS

Top

splitEpochDate($epoch_seconds)

db_fetch_all($sql, @params)

db_fetch_row($sql, @params)

option_function_map()

pagination($current_page, $number_per_page, $total_entries, $q, $max_pages)

option_priority()

Returns a list of available log priorities and their associated text labels.

option_time_horizon()

Returns a list of time horizon options, with associated text labels.

option_limit()

Returns a list of entry limits for each Error Log page.

Returns a list of optional search fields in the database, with associated labels.

hash_to_pair($hash, $mode)

SEE ALSO

Top

Helios::Panoptes, Helios::Service, helios.pl, <CGI::Application>, HTML::Template

AUTHOR

Top

Andrew Johnson, <lajandy at cpan dotorg> Ben Kucenski, <bkucenski at toolbox dotcom>

COPYRIGHT AND LICENSE

Top

WARRANTY

Top

This software comes with no warranty of any kind.


Helios-Panoptes documentation Contained in the Helios-Panoptes distribution.
package Helios::Panoptes::Helper;

use 5.008000;
use strict;
use warnings;
use Data::Dumper;
use DBI;
use Helios::Service;

our $VERSION = '1.40';
our $dbh;
our %config;

my $inifile;
if (defined($ENV{HELIOS_INI}) ) {
    $inifile = $ENV{HELIOS_INI};
} else {
    $inifile = './helios.ini';
}
my $worker = new Helios::Service;
%config = $worker->getConfigFromIni($inifile);

$dbh = DBI->connect($config{dsn}, $config{user}, $config{password});

sub splitEpochDate {
	my $epoch_secs = shift;

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoch_secs);

	my $return_date;

	$return_date->{YYYY} = sprintf("%04d", $year + 1900);
	$return_date->{MM} = sprintf("%02d", $mon+1);
	$return_date->{DD} = sprintf("%02d", $mday);
	$return_date->{MI} = sprintf("%02d", $min);
	$return_date->{SS} = sprintf("%02d", $sec);

	# hours
	if ($hour == 12) {
		$return_date->{AMPM} = 'PM';
		$return_date->{HH12} = '12';
		$return_date->{HH24} = '12';
		$return_date->{HH} = '12';
	} elsif ($hour == 0) {
		$return_date->{AMPM} = 'AM';
		$return_date->{HH12} = '12';
		$return_date->{HH24} = '00';
		$return_date->{HH} = '12';
	} elsif ($hour > 12) {
		$return_date->{AMPM} = 'PM';
		$return_date->{HH12} = sprintf("%02d", $hour - 12);
		$return_date->{HH24} = sprintf("%02d", $hour);
		$return_date->{HH} = sprintf("%02d", $hour);
	} else {
		# hour is AM
		$return_date->{AMPM} = 'AM';
		$return_date->{HH12} = sprintf("%02d", $hour);
		$return_date->{HH24} = sprintf("%02d", $hour);
		$return_date->{HH} = sprintf("%02d", $hour);
	}

	return $return_date;
}


sub db_fetch_all
{
	my $sql = shift;
	my @params = @_;
	
	my @result;

	my $sth = $dbh->prepare($sql);
	unless($sth) { throw Error::Simple($dbh->errstr); }

	$sth->execute(@params) or throw Error::Simple($dbh->errstr());
	
	while ( my $result = $sth->fetchrow_hashref() ) 
	{
		push @result, $result;
	}
	
	return @result;
}


sub db_fetch_row
{
	my $sql = shift;
	my @params = @_;
	
	my @result;

	my $sth = $dbh->prepare($sql);
	unless($sth) { throw Error::Simple($dbh->errstr); }

	$sth->execute(@params) or throw Error::Simple($dbh->errstr());
	
	return $sth->fetchrow_hashref();
}


sub option_function_map {
	my $funcmap;

	my $sql = "
				SELECT 
						funcid, 
						funcname 
				FROM 
						funcmap 
				ORDER BY 
						LOWER(funcname) ASC
				";

	return db_fetch_all($sql);
}


sub pagination
{
	my $cur_page = shift;
	my $per_page = shift;
	my $total_entries = shift;
	my $q = shift;
	my $max_pages = shift;
	
	$max_pages ||= 10;

	return "" if($total_entries <= $per_page);
	
	my $num_pages = int($total_entries / $per_page);
	
	my $start_page = $cur_page - int($max_pages / 2);
	my $end_page = $cur_page + int($max_pages / 2);

	$end_page -= $start_page if($start_page < 0);
	$start_page -= $end_page - $num_pages if $end_page > $num_pages;
	
	$start_page = 0 if $start_page < 0;
	$end_page = $num_pages if $end_page > $num_pages;
	
	my $short_back = $cur_page - $max_pages;
	$short_back = 0 if $short_back < 0;

	my $short_forward = $cur_page + $max_pages;
	$short_forward = $num_pages - 1 if $short_forward > $num_pages;
	
	my $j=0;

	my $html = "<div class='pagination'><ul>";
	
	if($start_page > 0)
	{
		$html .= "<li class='page'><a href='$q&page=0'>&lt;&lt;</a></li>";
		$html .= "<li class='page'><a href='$q&page=$short_back'>&lt;</a></li>";
	}
	
	for($j=$start_page;$j<$end_page;$j++)
	{
		if($j != $cur_page)
		{
			$html .= "<li class='page'><a href='$q&page=$j'>" . ($j+1) . "</a></li>";
		}
		else
		{
			$html .= "<li class='page_sel'>" . ($j+1) . "</li>";
		}
	}
	
	if($end_page < $num_pages-1)
	{
		$html .= "<li class='page'><a href='$q&page=$short_forward'>&gt;</a></li>";
		$html .= "<li class='page'><a href='$q&page=" . ($num_pages-1) . "'>&gt;&gt;</a></li>";
	}
	
	$html .= "</ul></div>";
	
	return $html;
	
	
}


sub option_priority
{
	return (
		-1 => 'All',
		0 => 'Emergency',
		1 => 'Alert',
		2 => 'Critical',
		3 => 'Error',
		4 => 'Warning',
		5 => 'Notice',
		6 => 'Information',
		7 => 'Debug',
	);
}

sub option_time_horizon
{
	return (
		    60 => '1 Minute',
		   300 => '5 Minutes',
		   900 => '15 Minutes',
		  1800 => '30 Minutes',
		  3600 => '1 Hour',
		  7200 => '2 Hours',
		 14400 => '4 Hours',
		 28800 => '8 Hours',
		 57600 => '16 Hours',
		 86400 => '1 Day',
		172800 => '2 Days',
		259200 => '3 Days',
		604800 => '1 Week',
	);
}


sub option_limit
{
	return (
		  25 => '25',
		  50 => '50',
		 100 => '100',
		 200 => '200',
		1000 => '1000'
	);
}


sub option_search
{
	return (
		message => 'Message',
		jobid => 'Job ID',
		host => 'Host',
		process_id => 'Process ID',
	);
}


sub hash_to_pair
{
	my $hash = shift;
	my $mode = shift;
	
	$mode ||= 'num';
	
	my $key;
	my $value;
	
	my %hash = %$hash;
	
	my @options = ();
	
	if($mode eq 'num')
	{
		foreach(sort { $a <=> $b } keys %hash)
		{	
			$key = $_;
			$value = $hash{$_};
			push @options,{key => $key, value => $value};
		}
	}
	else
	{
		foreach(sort { $a cmp $b } keys %hash)
		{	
			$key = $_;
			$value = $hash{$_};
			push @options,{key => $key, value => $value};
		}
	}
    
	return \@options;

}
1;
__END__