blib/lib/Webalizer/Hist.pm

filter: Statementstmt 90.9% 60 / 66 Branchbran 67.8% 19 / 28 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 78.6% 92 / 117 CC 25 SCAR 21.2
CC
25
Cov
80%
month
13.0
_parse
9.3
totals
6.0
CRAP 8.3
↑ Summary
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
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
18 4 ▶ my %args; $#_ % 2 ? %args = @_ : warn "Odd argument list at " . __PACKAGE__ . "::new";
Branch: 1 branch
BranchTrueFalseTotal
$#_ % 2 ? : 4 0 4
19  
20 4 my $my = bless { desc => 1, %args, _curmonth => 0 }, $class;
21 4 ▶ return $my->_parse() ? $my : undef;
Branch: 1 branch
BranchTrueFalseTotal
$my->_parse ? : 4 0 4
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
l!l
012
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
27 12 ▶ if($month) {
Branch: 1 branch
BranchTrueFalseTotal
if ($month) { } 0 12 12
28 0 foreach my $i (0..$#{$self->{_data}}) {
29 0 ▶ if($self->{_data}[$i]{month} == $month) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_data"}[$i]{"month"} == $month) 0 0 0
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
BranchTrueFalseTotal
if $month > $#{$$self{"_data"};} 0 12 12
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
BranchTrueFalseTotal
unless $days 0 12 12
42 12 foreach my $key (keys %hash) {
43 120 ▶ $hash{"avg$1"} = sprintf("%.2f", $hash{$key}/$days)
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^total(.+)$/ 72 48 120
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
BranchTrueFalseTotal
if $key =~ /^total(.+)$/ 24 16 40
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
BranchTrueFalseTotal
if $key =~ /^total(.+)$/ 72 48 120
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
BranchTrueFalseTotal
if (ref $self->{'source'} eq 'SCALAR') { } 2 2 4
elsif (not ref $self->{'source'} and $self->{'source'}) { } 2 0 2
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"}
!ll&&!rl&&r
002
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"}
ABresult
0X0
100
111
74 2 ▶ open(my $FH, "<$self->{source}") || return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, qq[<$self->{"source"}] or return 0 2 0 2
75 2 ▶ push(@raw, $_) while(<$FH>);
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $FH) and do { push @raw, $_ } 6 2 8
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
BranchTrueFalseTotal
unless $self->{"desc"} 2 2 4
91 4 $self->{_data} = \@data;
92 4 return 1;
93 }
94  
95  
96 2 1;
97  
98 __END__
↑ Summary