Browse Source

Merge pull request #233 from nonmakina/ipCounter

Hourly, daily, weekly IP counter
pull/40/head
towards-a-new-leftypol 3 years ago
committed by GitHub
parent
commit
b68daf1607
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      templates/themes/categories/news.html
  2. 41
      templates/themes/categories/theme.php

20
templates/themes/categories/news.html

@ -36,7 +36,9 @@
<tr>
<th>{% trans "Board" %}</th>
<th>{% trans "PPH" %}</th>
<th>{% trans "Recent IPs" %}</th>
<th>{% trans "IPs last hour" %}</th>
<th>{% trans "IPs last day" %}</th>
<th>{% trans "IPs last week" %}</th>
</tr>
</thead>
<tbody>
@ -48,7 +50,13 @@
<span>{{ stats.pph }}</span>
</td>
<td class="minimal">
<span>{{ stats.recent_ips }}</span>
<span>{{ stats.hourly_ips }}</span>
</td>
<td class="minimal">
<span>{{ stats.daily_ips }}</span>
</td>
<td class="minimal">
<span>{{ stats.weekly_ips }}</span>
</td>
</tr>
{% for boardStat in stats.boards %}
@ -60,7 +68,13 @@
<span>{{ boardStat.pph }}</span>
</td>
<td class="minimal">
<span>{{ boardStat.recent_ips }}</span>
<span>{{ boardStat.hourly_ips }}</span>
</td>
<td class="minimal">
<span>{{ boardStat.daily_ips }}</span>
</td>
<td class="minimal">
<span>{{ boardStat.weekly_ips }}</span>
</td>
</tr>
{% endfor %}

41
templates/themes/categories/theme.php

@ -100,8 +100,14 @@
return null;
}
$HOUR = 3600;
$DAY = $HOUR * 24;
$WEEK = $DAY * 7;
$stats = [];
$unique = [];
$hourly = [];
$daily = [];
$weekly = [];
foreach (array_merge(... $config['boards']) as $uri) {
$_board = getBoardInfo($uri);
@ -112,6 +118,10 @@
$boardStat['title'] = $_board['title'];
$boardStat['hourly_ips'] = Categories::countUniqueIps($hourly, $HOUR, $_board);
$boardStat['daily_ips'] = Categories::countUniqueIps($daily, $DAY, $_board);
$boardStat['weekly_ips'] = Categories::countUniqueIps($weekly, $WEEK, $_board);
$pph_query = query(
sprintf("SELECT COUNT(*) AS count FROM ``posts_%s`` WHERE time > %d",
$_board['uri'],
@ -120,29 +130,30 @@
$boardStat['pph'] = $pph_query->fetch()['count'];
$unique_query = query(
sprintf("SELECT DISTINCT ip FROM ``posts_%s`` WHERE time > %d",
$_board['uri'],
time()-3600)
) or error(db_error());
$unique_ips = $unique_query->fetchAll();
$boardStat['recent_ips'] = count($unique_ips);
foreach ($unique_ips as $_k => $row) {
$unique[$row['ip']] = true;
}
$stats['boards'][] = $boardStat;
}
$stats['recent_ips'] = count($unique);
$stats['hourly_ips'] = count($hourly);
$stats['daily_ips'] = count($daily);
$stats['weekly_ips'] = count($weekly);
$stats['pph'] = array_sum(array_column($stats['boards'], 'pph'));
return $stats;
}
private static function countUniqueIps($markAsCounted, $timespan, $_board) {
$unique_query = query(
sprintf("SELECT DISTINCT ip FROM ``posts_%s`` WHERE time > %d",
$_board['uri'],
time()-$timespan)
) or error(db_error());
$uniqueIps = $unique_query->fetchAll();
foreach ($uniqueIps as $_k => $row) {
$markAsCounted[$row['ip']] = true;
}
return count($uniqueIps);
}
};
?>

Loading…
Cancel
Save