| 1 | ## Webalizer::Hist, created by //YorHel | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Webalizer::Hist; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | $VERSION = "0.03"; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | my @webalizerheader = qw( month year totalhits totalfiles totalsites | |||||||||||||||||||
| 13 | totalkbytes firstday lastday totalpages totalvisits ); | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 4 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||
| 16 | 4 | my $self = shift; | |||||||||||||||||||
| 17 | 4 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 18 | 4 | ▶ | my %args; $#_ % 2 ? %args = @_ : warn "Odd argument list at " . __PACKAGE__ . "::new"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 4 | my $my = bless { desc => 1, %args, _curmonth => 0 }, $class; | |||||||||||||||||||
| 21 | 4 | ▶ | return $my->_parse() ? $my : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 12 | ▶ | sub month { | ||||||||||||||||||
|
Subroutine
month: called CC=9 SCAR=25.6
Pod
documented
| |||||||||||||||||||||
| 25 | 12 | my $self = shift; | |||||||||||||||||||
| 26 | 12 | ▶ | my $month = shift || undef; | ||||||||||||||||||
|
Condition50%1/2
shift() || undef
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 27 | 12 | ▶ | if($month) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 0 | foreach my $i (0..$#{$self->{_data}}) { | |||||||||||||||||||
| 29 | 0 | ▶ | if($self->{_data}[$i]{month} == $month) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | $month = $i; | |||||||||||||||||||
| 31 | 0 | last; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||
| 35 | 12 | $month = $self->{_curmonth}++; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 12 | ▶ | return 0 if $month > $#{$self->{_data}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 12 | my %hash = %{$self->{_data}[$month]}; | |||||||||||||||||||
| 40 | 12 | my $days = $hash{lastday} - $hash{firstday} + 1; | |||||||||||||||||||
| 41 | 12 | ▶ | return 0 if !$days; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 12 | foreach my $key (keys %hash) { | |||||||||||||||||||
| 43 | 120 | ▶ | $hash{"avg$1"} = sprintf("%.2f", $hash{$key}/$days) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | if $key =~ /^total(.+)$/; | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | 12 | return \%hash; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 4 | ▶ | sub totals { | ||||||||||||||||||
|
Subroutine
totals: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 50 | 4 | my $self = shift; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 4 | my %hash; | |||||||||||||||||||
| 53 | 4 | foreach my $key (@webalizerheader) { | |||||||||||||||||||
| 54 | 40 | ▶ | $hash{$1} = 0 if $key =~ /^total(.+)$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 4 | foreach my $month (@{$self->{_data}}) { | |||||||||||||||||||
| 58 | 12 | foreach my $key (keys %$month) { | |||||||||||||||||||
| 59 | 120 | ▶ | $hash{$1} += $month->{$key} if $key =~ /^total(.+)$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 4 | return \%hash; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 4 | ▶ | sub _parse { | ||||||||||||||||||
|
Subroutine
_parse: called CC=9 SCAR=22.3
| |||||||||||||||||||||
| 67 | 4 | my $self = shift; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 4 | my @data; | |||||||||||||||||||
| 70 | my @raw; | ||||||||||||||||||||
| 71 | 4 | ▶ | if(ref($self->{source}) eq "SCALAR") { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 72 | 2 | @raw = split(/\r?\n/, ${$self->{source}}); | |||||||||||||||||||
| 73 | ▶ | } elsif(!ref($self->{source}) && $self->{source}) { | |||||||||||||||||||
|
Condition33%1/3
not ref $self->{"source"} and $self->{"source"}
MC/DC0%0/2
not ref $self->{"source"} and $self->{"source"}
not ref $self->{"source"}
$self->{"source"}
Truth table33%1/3
not(A) and B
A= not ref $self->{"source"}B= $self->{"source"}
| |||||||||||||||||||||
| 74 | 2 | ▶ | open(my $FH, "<$self->{source}") || return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 2 | ▶ | push(@raw, $_) while(<$FH>); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 2 | close($FH); | |||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||
| 78 | 0 | return 0; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 4 | foreach my $line (@raw) { | |||||||||||||||||||
| 82 | 12 | chomp($line); | |||||||||||||||||||
| 83 | 12 | my $c = 0; my %line; my $item; | |||||||||||||||||||
| 84 | 12 | foreach $item (split(/[\t\s]+/, $line)) { | |||||||||||||||||||
| 85 | 120 | $line{$webalizerheader[$c++]} = int($item); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | 12 | push(@data, \%line); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | 4 | @data = sort { sprintf("%04d%02d", $b->{year}, $b->{month}) <=> sprintf("%04d%02d", $a->{year}, $a->{month}) } @data; | |||||||||||||||||||
| 90 | 4 | ▶ | @data = reverse @data if !$self->{desc}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 4 | $self->{_data} = \@data; | |||||||||||||||||||
| 92 | 4 | return 1; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 2 | 1; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||