From 336c6c8a415603c772f97ccd63912d05209f3795 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 2 Jan 2015 13:27:02 +0100 Subject: initial --- lib/PINGDOMFETCH/Result.pm | 120 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 lib/PINGDOMFETCH/Result.pm (limited to 'lib/PINGDOMFETCH/Result.pm') diff --git a/lib/PINGDOMFETCH/Result.pm b/lib/PINGDOMFETCH/Result.pm new file mode 100644 index 0000000..582c354 --- /dev/null +++ b/lib/PINGDOMFETCH/Result.pm @@ -0,0 +1,120 @@ +package PINGDOMFETCH::Result; + +use strict; +use warnings; + +use PINGDOMFETCH::Config; +use PINGDOMFETCH::DateHelper; +use PINGDOMFETCH::Display; +use PINGDOMFETCH::Utils; + +our @ISA = ('PINGDOMFETCH::Display'); + +sub new { + my ( $class, %vals ) = @_; + + my $self = bless \%vals, $class; + + $self->compute(); + + return $self; +} + +sub acc { + my ( $self, $service, $acc ) = @_; + + $acc->( $service, $self ); + + return undef; +} + +sub compute { + my ($self) = @_; + + my $config = $self->{config}; + + my ( $up, $down, $total ) = $self->compute_up_down(); + $self->{avail_perc} = $self->compute_avail_perc( $up, $down, $total ); + + if ( $config->bool('interval_is_in_future') ) { + my $remaining = do { + + # It's a Service result and not a TLS + if ( exists $self->{service} ) { + + # Total seconds in the current interval + my $seconds = + $config->{dh_to}->time() - $config->{dh_from}->time(); + $self->{remaining} = $seconds - $total; + + } + else { + + # It's a TLS result + $self->{remaining}; + } + }; + + $self->{possible_avail_perc_best} = + $self->compute_avail_perc( $up + $remaining, $down ); + + $self->{possible_avail_perc_worst} = + $self->compute_avail_perc( $up, $down + $remaining ); + } + + return undef; +} + +sub compute_up_down { + my ( $self, $totalup, $totalunknown, $totaldown ) = @_; + + my $config = $self->{config}; + my $unknown = $config->get('interpret.unknown.status.as.up'); + + $totalup = $self->{totalup} unless defined $totalup; + $totaldown = $self->{totaldown} unless defined $totaldown; + $totalunknown = $self->{totalunknown} unless defined $totalunknown; + + my $total = $totalup + $totaldown + $totalunknown; + + return $unknown =~ /true/i + ? ( $totalup + $totalunknown, $totaldown, $total ) + : ( $totalup, $totaldown + $totalunknown, $total ); +} + +sub compute_avail_perc { + my ( $self, $up, $down ) = @_; + + my $config = $self->{config}; + my $zero = $config->get('interpret.zero.results.as.up'); + + my $total = $up + $down; + + if ( $total > 0 ) { + return 100 * $up / $total; + } + else { + return $zero =~ /true/i ? 100 : 0; + } +} + +sub print { + my ($self) = @_; + + $self->print_full(); + + return undef; +} + +sub print_full { + my ($self) = @_; + + my $config = $self->{config}; + + $self->info("$_: $self->{$_}") + for sort grep { not ref $self->{$_} } keys %$self; + + return undef; +} + +1; -- cgit v1.2.3