OMatic/stats.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 126 CC 15 SCAR 47.0
CC
15
Cov
0%
main
182.0
niceValue
12.0
BEGIN
2.0
CRAP 109.7
1 ##############################################################################
2 # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. #
3 # #
4 # This program is free software; you can redistribute it and/or #
5 # modify it under the terms of the GNU General Public License #
6 # as published by the Free Software Foundation; either version 2 #
7 # of the License, or (at your option) any later version. #
8 # #
9 # This program is distributed in the hope that it will be useful, #
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 # GNU General Public License for more details. #
13 # #
14 # You should have received a copy of the GNU General Public License #
15 # along with this program; if not, write to the Free Software #
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#
17 # #
18 # Jon Howell can be contacted at: #
19 # 6211 Sudikoff Lab, Dartmouth College #
20 # Hanover, NH 03755-3510 #
21 # jonh@cs.dartmouth.edu #
22 # #
23 # An electronic copy of the GPL is available at: #
24 # http://www.gnu.org/copyleft/gpl.html #
25 # #
26 ##############################################################################
27  
28 use strict;
29  
30 package FAQ::OMatic::stats;
31  
32 use CGI;
33 use FAQ::OMatic::Item;
34 use FAQ::OMatic;
35 use FAQ::OMatic::Log;
36 use FAQ::OMatic::I18N;
37  
38 sub main {
39 my $cgi = FAQ::OMatic::dispatch::cgi();
40 my $rt = '';
41
42 my $params = FAQ::OMatic::getParams($cgi);
43  
44 $params->{'duration'} = '' if (not defined($params->{'duration'}));
45  
46 my $simpleHTML = $params->{'simpleHTML'};
47  
48 # make sure today is summarized in a .smry file
49 FAQ::OMatic::Log::summarizeDay(FAQ::OMatic::Log::numericToday());
50
51 # check history now that today is summarized (so there's at least 1 .smry)
52 if ($params->{'duration'} eq 'history') {
53 # tickle the earliestLogHint file so all 8 graph CGIs
54 # don't try to do so simultaneously.
55 FAQ::OMatic::Log::earliestSmry();
56 }
57  
58 # get today's stats for the text part of the display
59 my $today = $params->{'today'} || '';
60 $today =~ m/([\d-]*)/; # sanitize user input before treating it as a path
61 $today = $1;
62 if (not -e "$FAQ::OMatic::Config::metaDir/$today.smry") {
63 $today = FAQ::OMatic::Log::numericToday();
64 }
65 my $todayItem = new FAQ::OMatic::Item("$today.smry", $FAQ::OMatic::Config::metaDir);
66  
67 # The list of properties and corresponding titles
68 my @props = ('Hits', 'CumHits');
69 my @titles = (gettext("Hits Per Day"),
70 gettext("Cumulative Hits"));
71  
72 if ($FAQ::OMatic::Config::statUniqueHosts) {
73 push @props, ('UniqueHosts', 'CumUniqueHosts');
74 push @titles, (gettext("New Hosts Per Day"),
75 gettext("Total Hosts"));
76 }
77  
78 push @props, ('HitsPerHost', 'CumOperation-search');
79 push @titles, (gettext("Hits Per Host"),
80 gettext("Cumulative Searches"));
81  
82 push @props, ('Operation-submitPart', 'CumOperation-submitPart');
83 push @titles, (gettext("Submissions Per Day"),
84 gettext("Cumulative Submissions"));
85  
86 $rt.= FAQ::OMatic::pageHeader($params);
87 $rt.= gettext("Please be patient ... the image files are generated dynamically, and can take from 20 to 50 seconds to create.\n");
88 $rt.="<table>" if (!$simpleHTML);
89
90 my $i;
91 for ($i=0; $i<@props; $i++) {
92 my $url = FAQ::OMatic::makeAref('statgraph',
93 {'property'=>$props[$i],
94 'title'=>$titles[$i],
95 'duration'=>$params->{'duration'},
96 'resolution'=>$params->{'resolution'},
97 'today'=>$params->{'today'}}, 'url');
98  
99 if ($i & 0x01) {
100 # before the second graph of a pair
101 $rt .= $simpleHTML ?
102 "" :
103 "<td align=center>\n";
104 } else {
105 # before a pair
106 $rt .= $simpleHTML ?
107 "" :
108 "<tr><td align=center>\n";
109 }
110  
111 $rt .= "<img src=\"$url\" "
112 ."width=$FAQ::OMatic::Appearance::graphWidth "
113 ."height=$FAQ::OMatic::Appearance::graphHeight>";
114 my $value = niceValue($todayItem->{$props[$i]});
115 $rt .= "<br>".$titles[$i].": ".$value."\n";
116  
117 if ($i & 0x01) {
118 # after a pair
119 $rt .= $simpleHTML ?
120 "<p>" :
121 "</td></tr>\n";
122 } else {
123 # after the first graph of a pair
124 $rt .= $simpleHTML ?
125 "<p>" :
126 "</td>\n";
127 }
128 }
129 $rt.="</table>" if (!$simpleHTML);
130  
131 $rt.=FAQ::OMatic::button(FAQ::OMatic::makeAref('faq',
132 {'duration'=>'','resolution'=>'','today'=>''}),
133 gettext("Return to the FAQ"));
134  
135 # let the user change the view
136 $rt.=" "
137 . gettext("Change View Duration")
138 . ": ";
139 $rt.=FAQ::OMatic::button(FAQ::OMatic::makeAref('stats',
140 {'duration'=>'30'}), gettext("One Month"));
141 $rt.=FAQ::OMatic::button(FAQ::OMatic::makeAref('stats',
142 {'duration'=>'60'}), gettext("Two Months"));
143 $rt.=FAQ::OMatic::button(FAQ::OMatic::makeAref('stats',
144 {'duration'=>'90'}), gettext("Three Months"));
145 $rt.=FAQ::OMatic::button(FAQ::OMatic::makeAref('stats',
146 {'duration'=>'history'}), gettext("History"));
147  
148 $rt .= FAQ::OMatic::pageFooter($params);
149 print $rt;
150 }
151  
152 sub niceValue {
153 my $value = shift || '';
154  
155 $value = 0 if ($value eq '');
156  
157 if (not $value =~ m/\./) {
158 # for big numbers
159 $value =~ s/(?!^|,)(\d\d\d)$/,$1/; # add commas for readability
160 $value =~ s/(?!^|,)(\d\d\d),/,$1,/; # of big numbers. This'll keep you
161 $value =~ s/(?!^|,)(\d\d\d),/,$1,/; # til your trillionth hit. :v)
162 } else {
163 # for little numbers
164 $value = sprintf "%.2f", $value;
165 }
166 return $value;
167 }
168  
169 1;