~ctrlproxy/ctrlproxy/trunk

« back to all changes in this revision

Viewing changes to example/stats.php

  • Committer: jelmer
  • Date: 2003-10-18 22:02:02 UTC
  • Revision ID: jelmer@samba.org-20031018220202-6801a76318fb4d13
Update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?
 
2
 
 
3
function mycmp($v1,$v2)
 
4
{
 
5
        global $sortfield;
 
6
        if ($v1[$sortfield] == $v2[$sortfield]) return 0;
 
7
    return ($v1[$sortfield] > $v2[$sortfield]) ? -1 : 1;
 
8
}
 
9
 
 
10
function sortmydata(&$data, $field)
 
11
{
 
12
        global $sortfield;
 
13
        $sortfield = $field;
 
14
        uasort(&$data, "mycmp");
 
15
}
 
16
 
 
17
 
 
18
$network = "INU";
 
19
$channel = "#ctrlproxy";
 
20
 
 
21
$fd = popen("../printstats /home/jelmer/.ctrlproxy-stats.tdb", "r");
 
22
 
 
23
while(!feof($fd)) {
 
24
        $v = fgets($fd);
 
25
        preg_match("'([^/]*)\/([^/]*)\/([^/]*)\\/([^/]*): ([0-9]+)'", $v, $regs);
 
26
        if($regs[0] == "")continue;
 
27
        $data[$regs[1]][$regs[2]][$regs[3]][$regs[4]] = $regs[5];
 
28
}
 
29
 
 
30
fclose($fd);
 
31
 
 
32
/* Okay, all data has been read. Now build a nice page based on it. */
 
33
 
 
34
$mydata = $data[$network][$channel];
 
35
 
 
36
/* Add some statistics to the array */
 
37
reset($mydata);
 
38
while(list($k,$v) = each($mydata)) {
 
39
        if($v["lines"] == 0 || $v["words"] == 0)continue;
 
40
        $mydata[$k]["avg_words_per_line"] = $v["words"] / $v["lines"];
 
41
        $mydata[$k]["avg_chars_per_line"] = $v["chars"] / $v["lines"];
 
42
}
 
43
 
 
44
?>
 
45
<html>
 
46
<head>
 
47
<title><?=$channel;?> @ <?=$network;?> stats by ctrlproxy</title>
 
48
</head>
 
49
<body bgcolor="#ffffff">
 
50
 
 
51
<h1><?=$channel;?> @ <?=$network;?> stats by ctrlproxy</h1>
 
52
 
 
53
<? if(isset($extended)) { ?>
 
54
<h2><?=$title;?></h2>
 
55
 
 
56
<table border=0>
 
57
 
 
58
<? if($extended == "lines") { ?>
 
59
 
 
60
<tr><td></td><td><b>Nick</b></td><td><b>Lines</b></td><td><b>Words</b></td></tr>
 
61
<? sortmydata($mydata, "lines"); 
 
62
reset($mydata);$i = 0;
 
63
while(list($k,$v) = each($mydata)) {
 
64
        if(!$k)continue;
 
65
        $i++;
 
66
        print "<tr><td>$i</td><td>$k</td><td>" . $v["lines"] . "</td><td>" . $v["words"] . "</td></tr>\n";
 
67
}
 
68
?><p>Total lines: <?=$mydata[""]["lines"]; 
 
69
} else {
 
70
?>
 
71
 
 
72
<tr><td></td><td><b>Nick</b></td><td><b><?=$extended;?></b></td></tr>
 
73
<? sortmydata($mydata, $extended);
 
74
reset($mydata);$i = 0;
 
75
while(list($k, $v) = each($mydata)) {
 
76
        if(!$k)continue;
 
77
        $i++;
 
78
        print "<tr><td>$i</td><td>$k</td><td>" . $v[$extended] . (isset($showprcnt)?(" (" . round($v[$extended] / $v["lines"] * 100) . "%)"):"") . "</td></tr>\n";
 
79
}
 
80
 
 
81
}
 
82
?>
 
83
 
 
84
</table>
 
85
 
 
86
<?
 
87
} else {
 
88
$fields = array();
 
89
$fields["lines"] = array("title" => "Lines", "desc1" => "%s wrote the most lines: %d", "desc2" => "%s wrote almost as much lines: %d");
 
90
$fields["foul"] = array("title" => "Foul Language", "desc1" => "%s makes sailors blush", "desc2" => "%s has a potty mouth as well");
 
91
$fields["happy"] = array("title" => "Happy", "desc1" => "%s is the happiest person on the channel", "desc2" => "%s is quite happy as well");
 
92
$fields["unhappy"] = array("title" => "Unhappy", "desc1" => "%s is the saddest person on the channel", "desc2" => "%s is quite sad as well");
 
93
 
 
94
 
 
95
function expand_desc($desc, $k, $v)
 
96
{
 
97
        return strtr($desc, array("%s" => $k, "%d" => $v));
 
98
}
 
99
 
 
100
reset($fields);
 
101
while(list($k,$v) = each($fields)) {
 
102
        sortmydata($mydata, $k);
 
103
        reset($mydata);
 
104
        echo "<h3>" . $v["title"] . "</h3>\n";
 
105
        list($k1,$v1) = each($mydata);
 
106
        if(isset($v["desc1"])) echo "<p>" . expand_desc($v["desc1"], $k1, $v1[$k]) . "\n";
 
107
        list($k1,$v1) = each($mydata);
 
108
        if(isset($v["desc2"])) echo "<p>" . expand_desc($v["desc2"], $k1, $v1[$k]) . "\n";
 
109
        echo "<p><a href='$PHP_SELF?extended=$k&title=" . urlencode($v["title"]) . (isset($v["showpercent"])?"&showprcnt=1":"") . "'>...</a>";
 
110
        echo "<hr>\n";
 
111
 
 
112
}
 
113
 
 
114
}
 
115
?>
 
116
 
 
117
</body>
 
118
</html>