Yenta/Stats.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 114 CC 7 SCAR 17.3
CC
7
Cov
0%
add_idle
12.0
handler
12.0
http_data
6.0
CRAP 5.6
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Apr-06 13:27 (EDT)
6 # Function: internal stats monitoring
7 #
8 # $Id: Stats.pm,v 1.7 2010/09/07 15:31:55 jaw Exp $
9  
10 package AC::Yenta::Stats;
11 use AC::Yenta::Debug 'stats';
12 use AC::Import;
13 use strict;
14  
15 our @EXPORT = qw(add_idle loadave inc_stat);
16 require AC::Yenta::Store;
17 AC::Yenta::Store->import();
18  
19 my $loadave = 0;
20 my %STATS;
21  
22 my %HANDLER = (
23 loadave => \&http_load,
24 stats => \&http_stats,
25 status => \&http_status,
26 peers => \&AC::Yenta::Status::report,
27 # dumppeers => \&AC::Yenta::Status::report_long,
28 );
29  
30 sub add_idle {
31 my $idle = shift;
32 my $total = shift;
33  
34 # decaying average
35 return unless $total;
36 my $load = 1 - $idle / $total;
37 $total = 60 if $total > 60;
38 my $exp = exp( - $total / 60 );
39 $loadave = $loadave * $exp + $load * (1 - $exp);
40 }
41  
42 sub loadave {
43 return $loadave;
44 }
45  
46 sub inc_stat {
47 my $stat = shift;
48  
49 $STATS{$stat} ++;
50 }
51  
52 sub handler {
53 my $class = shift;
54 my $io = shift;
55 my $proto = shift;
56 my $url = shift;
57  
58 debug("http request $url");
59 $url =~ s|^/||;
60 $url =~ s/%(..)/chr(hex($1))/eg;
61  
62 my $f = $HANDLER{$url};
63 $f = \&http_data if $url =~ m|^data/|;
64 $f = \&http_file if $url =~ m|^file/|;
65 $f ||= \&http_notfound;
66 my( $content, $code, $text ) = $f->($url);
67 $code ||= 200;
68 $text ||= 'OK';
69  
70 my $res = "HTTP/1.0 $code $text\r\n"
71 . "Server: AC/Yenta\r\n"
72 . "Connection: close\r\n"
73 . "Content-Type: text/plain; charset=UTF-8\r\n"
74 . "Content-Length: " . length($content) . "\r\n"
75 . "\r\n"
76 . $content ;
77  
78 $io->write($res);
79 $io->set_callback('write_buffer_empty', \&_done );
80 }
81  
82 sub _done {
83 my $io = shift;
84 $io->shut();
85 }
86  
87 ################################################################
88  
89 sub http_notfound {
90 my $url = shift;
91  
92 return ("404 NOT FOUND\nThe requested url /$url was not found on this server.\nSo sorry.\n\n", 404, "Not Found");
93 }
94  
95 sub http_load {
96  
97 return sprintf("loadave: %0.4f\n\n", loadave());
98 }
99  
100 sub http_status {
101 return "status: OK\n\n";
102 }
103  
104 sub http_stats {
105  
106 my $res;
107 for my $k (sort keys %STATS){
108 $res .= sprintf("%-24s%s\n", "$k:", $STATS{$k});
109 }
110  
111 my @peers = AC::Yenta::Status->allpeers();
112 $res .= sprintf("%-24s%s\n", "peers:", scalar @peers);
113 $res .= "\n";
114 return $res;
115 }
116  
117 sub http_data {
118 my $url = shift;
119  
120 my(undef, $map, $key, $ver) = split m|/|, $url;
121 my($data, $version, $file, $meta) = store_get($map, $key, $ver);
122  
123 return http_notfound($url) unless $version;
124 return $data;
125 }
126  
127 sub http_file {
128 my $url = shift;
129  
130 my(undef, $map, $key, $ver) = split m|/|, $url;
131 my($data, $version, $file, $meta) = store_get($map, $key, $ver);
132  
133 return http_notfound($url) unless $version && $file;
134 return $$file;
135 }
136  
137 1;