From 31c7e569ecba52cb99784a02664ca3cc47b2410a Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Thu, 17 Nov 2022 02:17:44 +0100 Subject: [PATCH] Adding tracking by url. --- lib/BurguillosInfo/Controller/Metrics.pm | 3 +- lib/BurguillosInfo/Tracking.pm | 50 ++++++++++++++++++++---- templates/metrics/stats.html.ep | 16 ++++++++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/lib/BurguillosInfo/Controller/Metrics.pm b/lib/BurguillosInfo/Controller/Metrics.pm index db53fe5..ac010a7 100644 --- a/lib/BurguillosInfo/Controller/Metrics.pm +++ b/lib/BurguillosInfo/Controller/Metrics.pm @@ -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 { diff --git a/lib/BurguillosInfo/Tracking.pm b/lib/BurguillosInfo/Tracking.pm index 68eb2e4..1a8c9d9 100644 --- a/lib/BurguillosInfo/Tracking.pm +++ b/lib/BurguillosInfo/Tracking.pm @@ -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; diff --git a/templates/metrics/stats.html.ep b/templates/metrics/stats.html.ep index 97dfa2a..a4291c0 100644 --- a/templates/metrics/stats.html.ep +++ b/templates/metrics/stats.html.ep @@ -6,4 +6,20 @@

Unique visitors last 24 hours <%=$tracking_data->{unique_ips_last_24_hours}%>

Unique visitors last week <%=$tracking_data->{unique_ips_last_week}%>

Unique visitors last month <%=$tracking_data->{unique_ips_last_month}%>

+ + + + + + + +% for my $row (@$tracking_by_url) { + + + + + + +%} +
PathVisitors 24hVisitors weekVisitors month
<%=$row->{path}%><%=$row->{unique_ips_last_24_hours}%><%=$row->{unique_ips_last_week}%><%=$row->{unique_ips_last_month}%>