Commit de519016 authored by Simon Welsh's avatar Simon Welsh

Swear stats

git-svn-id: svn+ssh://lsgn/var/logbot-www@67 5f99df0c-6b0a-442a-b40a-040688e84a9d
parent e0901025
......@@ -83,3 +83,36 @@ function build_dates() {
}
return $dates;
}
function get_stats() {
$r = new Redis;
if($r->connect('localhost')) {
$r->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$stats = $r->get('log:stats');
} else {
return [];
}
if(!$stats) {
build_stats($r);
$stats = $r->get('log:stats');
}
return $stats;
}
function build_stats(Redis $r) {
// SELECT COUNT(*) AS "Bitches", "Nick" FROM "Log" WHERE "Text" ILIKE '%cunt%' GROUP BY "Nick" ORDER BY "Bitches" DESC;
$stats = [];
$query = 'SELECT COUNT(*) AS "Number", "Nick" FROM "Log" WHERE "Text" ILIKE \'%%%s%%\' GROUP BY "Nick" HAVING COUNT(*) >= 10 ORDER BY "Number" DESC;';
foreach(['Bitch', 'Cunt', 'Fuck', 'Shit', 'Joomla', 'WordPress', 'Dick', 'Slut', 'Crap'] as $name) {
$res = DB::inst()->query(sprintf($query, $name));
$stats[$name] = [];
while($row = pg_fetch_array($res)) {
$stats[$name][$row[0]] = $row[1];
}
}
$stats = array_filter($stats);
uasort($stats, function($b, $a) {
return count($a) - count($b);
});
$r->set('log:stats', $stats, 3600);
}
......@@ -108,9 +108,20 @@ div.month h4 {
margin: 15px 0 10px;
}
div.swear {
float: left;
width: 300px;
margin-left: 10px;
}
div.swear h3 {
margin: 15px 0 10px;
}
.clear {
clear: both;
min-height: 1px;
}
h3 {
......
<?php
error_reporting(E_ALL);
require '../funcs.php';
$head = <logs:head robots="noindex" />;
$body = <x:frag>
<logs:info>
<h2>Swear stats</h2>
</logs:info>
</x:frag>;
$stats = get_stats();
foreach($stats as $name => $values) {
$d = <div class="swear" />;
$d->appendChild(<h3 id={strtolower($name)}>{$name}</h3>);
$tb = <tbody />;
foreach($values as $person => $count) {
$tb->appendChild(<tr><td>{$person}</td><td>{$count}</td></tr>);
}
$d->appendChild(<table>
<thead>
<tr>
<th>Nick</th><th>Number times "{$name}" used</th>
</tr>
</thead>
{$tb}
</table>);
$body->appendChild($d);
}
$body->appendChild(<div class="clear"></div>);
echo <logs:document head={$head} body={$body} />;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment