Adding tracking by url.

This commit is contained in:
sergiotarxz 2022-11-17 02:17:44 +01:00
parent 5576215fd4
commit 31c7e569ec
3 changed files with 60 additions and 9 deletions

View File

@ -36,7 +36,8 @@ sub stats {
return;
}
my $data = $tracking->get_global_data($self);
$self->render(tracking_data => $data);
my $data_per_url = $tracking->get_data_for_urls($self);
$self->render(tracking_data => $data, tracking_by_url => $data_per_url);
}
sub submit_login {

View File

@ -6,11 +6,18 @@ use strict;
use warnings;
use JSON;
use Const::Fast;
use BurguillosInfo::DB;
my $app;
const my $SELECT_GLOBAL => <<'EOF';
SELECT COUNT(DISTINCT (remote_address, user_agent))
FROM requests
EOF
sub new {
my $class = shift;
$app = shift;
@ -42,24 +49,21 @@ EOF
my $c = shift;
my $app = $c->app;
my $dbh = BurguillosInfo::DB->connect($app);
my $data = $dbh->selectrow_hashref(<<'EOF', undef);
my $data = $dbh->selectrow_hashref(<<"EOF", undef);
SELECT
(
SELECT COUNT(DISTINCT (remote_address, user_agent)) FROM requests
$SELECT_GLOBAL
) as unique_ips,
(
SELECT COUNT(DISTINCT (remote_address, user_agent))
FROM requests
$SELECT_GLOBAL
where date > NOW() - interval '1 day'
) as unique_ips_last_24_hours,
(
SELECT COUNT(DISTINCT (remote_address, user_agent))
FROM requests
$SELECT_GLOBAL
where date > NOW() - interval '1 week'
) as unique_ips_last_week,
(
SELECT COUNT(DISTINCT (remote_address, user_agent))
FROM requests
$SELECT_GLOBAL
where date > NOW() - interval '1 month'
) as unique_ips_last_month;
@ -68,4 +72,34 @@ EOF
return $data;
}
sub get_data_for_urls {
my $self = shift;
my $c = shift;
my $app = $c->app;
my $dbh = BurguillosInfo::DB->connect($app);
my $data = $dbh->selectall_arrayref(<<"EOF", {Slice => {}});
SELECT paths.path,
(
$SELECT_GLOBAL
where requests.path = paths.path
) as unique_ips,
(
$SELECT_GLOBAL
where requests.path = paths.path and date > NOW() - interval '1 day'
) as unique_ips_last_24_hours,
(
$SELECT_GLOBAL
where requests.path = paths.path and date > NOW() - interval '1 week'
) as unique_ips_last_week,
(
$SELECT_GLOBAL
where requests.path = paths.path and date > NOW() - interval '1 month'
) as unique_ips_last_month
FROM paths;
EOF
return $data;
}
1;

View File

@ -6,4 +6,20 @@
<p>Unique visitors last 24 hours <%=$tracking_data->{unique_ips_last_24_hours}%></p>
<p>Unique visitors last week <%=$tracking_data->{unique_ips_last_week}%></p>
<p>Unique visitors last month <%=$tracking_data->{unique_ips_last_month}%></p>
<table>
<tr>
<th>Path</th>
<th>Visitors 24h</th>
<th>Visitors week</th>
<th>Visitors month</th>
</tr>
% for my $row (@$tracking_by_url) {
<tr>
<td><%=$row->{path}%></td>
<td><%=$row->{unique_ips_last_24_hours}%></td>
<td><%=$row->{unique_ips_last_week}%></td>
<td><%=$row->{unique_ips_last_month}%></td>
</tr>
%}
</table>
</div>