Merge pull request #233 from nonmakina/ipCounter
Hourly, daily, weekly IP counter
This commit is contained in:
commit
b68daf1607
|
@ -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 %}
|
||||
|
|
|
@ -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…
Reference in New Issue