Adding the chance to monitor google data.

This commit is contained in:
Sergiotarxz 2023-05-07 18:10:55 +02:00
parent ff572e844b
commit 52067b5647
4 changed files with 162 additions and 88 deletions

View File

@ -37,8 +37,10 @@ sub stats {
}
my $data = $tracking->get_global_data($self);
my $data_per_url = $tracking->get_data_for_urls($self);
my $google_data = $tracking->get_google_data($self);
$self->_filter_data_per_url($data_per_url);
$self->render(tracking_data => $data, tracking_by_url => $data_per_url);
$self->_filter_data_per_url($google_data);
$self->render(tracking_data => $data, tracking_by_url => $data_per_url, google_data => $google_data);
}
sub _filter_data_per_url($self, $data_per_url) {

View File

@ -29,6 +29,8 @@ sub MIGRATIONS {
'ALTER TABLE paths ALTER COLUMN last_seen SET DEFAULT NOW();',
'ALTER TABLE requests ADD PRIMARY KEY (uuid)',
'CREATE INDEX request_extra_index on requests (date, path);',
'ALTER TABLE requests ADD column referer text;',
'CREATE INDEX request_referer_index on requests (referer);',
);
}
1;

View File

@ -19,7 +19,6 @@ SELECT COUNT(DISTINCT (remote_address, user_agent))
FROM requests
EOF
sub new {
my $class = shift;
$app = shift;
@ -27,7 +26,7 @@ sub new {
return bless {}, $class;
}
sub _add_path($self, $url) {
sub _add_path ( $self, $url ) {
my $dbh = BurguillosInfo::DB->connect($app);
$dbh->do( <<'EOF', undef, $url );
INSERT INTO paths (path) VALUES($1)
@ -36,9 +35,9 @@ ON CONFLICT (path) DO
EOF
}
sub _update_null_last_seen_paths_if_any($self) {
sub _update_null_last_seen_paths_if_any ($self) {
my $dbh = BurguillosInfo::DB->connect($app);
$dbh->do( <<'EOF', undef);
$dbh->do( <<'EOF', undef );
UPDATE paths
SET last_seen = requests_for_path.last_date
@ -51,12 +50,14 @@ WHERE paths.last_seen IS NULL AND requests_for_path.path = paths.path;
EOF
}
sub _register_request_query($self, $remote_address, $user_agent, $params_json, $path) {
sub _register_request_query ( $self, $remote_address, $user_agent,
$params_json, $path, $referer )
{
my $dbh = BurguillosInfo::DB->connect($app);
$dbh->do(
<<'EOF', undef, $remote_address, $user_agent, $params_json, $path );
INSERT INTO requests(remote_address, user_agent, params, path)
VALUES (?, ?, ?, ?);
<<'EOF', undef, $remote_address, $user_agent, $params_json, $path, $referer );
INSERT INTO requests(remote_address, user_agent, params, path, referer)
VALUES (?, ?, ?, ?, ?);
EOF
}
@ -69,17 +70,20 @@ sub register_request {
$self->_update_null_last_seen_paths_if_any();
my $remote_address = $c->tx->remote_address;
my $user_agent = $c->req->headers->user_agent;
my $referer = $c->req->headers->referer // '';
my $params_json = encode_json( $c->req->params->to_hash );
$self->_register_request_query($remote_address, $user_agent, $params_json, $path);
say "Registered $remote_address with user agent $user_agent visited $path with $params_json";
$self->_register_request_query( $remote_address, $user_agent, $params_json,
$path, $referer );
say
"Registered $remote_address with user agent $user_agent visited $path with $params_json";
}
sub get_global_data {
sub get_global_data {
my $self = shift;
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_GLOBAL
@ -95,14 +99,54 @@ SELECT
) as unique_ips_last_month;
EOF
return $data;
}
}
sub get_data_for_urls {
my $GOOGLE_SELECT = "$SELECT_GLOBAL
where requests.path = paths.path
and requests.referer IS NOT NULL
and requests.referer ~* '^https?://(?:www\\.)?google\\.\\w'
and date > NOW()";
sub get_google_data {
my $self = shift;
my $c = shift;
my $app = $c->app;
my $dbh = BurguillosInfo::DB->connect($app);
my $data = $dbh->selectall_arrayref(<<"EOF", {Slice => {}});
my $data = $dbh->selectall_arrayref(<<"EOF", { Slice => {} } );
SELECT paths.path,
(
$GOOGLE_SELECT - interval '1 hour'
) as unique_ips_last_1_hour,
(
$GOOGLE_SELECT - interval '3 hour'
) as unique_ips_last_3_hours,
(
$GOOGLE_SELECT - interval '6 hour'
) as unique_ips_last_6_hours,
(
$GOOGLE_SELECT - interval '12 hour'
) as unique_ips_last_12_hours,
(
$GOOGLE_SELECT - interval '1 day'
) as unique_ips_last_24_hours,
(
$GOOGLE_SELECT - interval '1 week'
) as unique_ips_last_week,
(
$GOOGLE_SELECT - interval '1 month'
) as unique_ips_last_month
FROM paths
WHERE paths.last_seen > NOW() - INTERVAL '1 month';
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

View File

@ -32,4 +32,30 @@
</tr>
%}
</table>
<h3>Google data.</h3>
<table>
<tr>
<th>Path</th>
<th data-sort-method="number">Visitors 1h</th>
<th data-sort-method="number">Visitors 3h</th>
<th data-sort-method="number">Visitors 6h</th>
<th data-sort-method="number">Visitors 12h</th>
<th data-sort-method="number">Visitors 24h</th>
<th data-sort-method="number">Visitors week</th>
<th data-sort-method="number">Visitors month</th>
</tr>
% for my $row (@$google_data) {
<tr>
<td><%="${base_url}$row->{path}"%></td>
<td><%=$row->{unique_ips_last_1_hour}%></td>
<td><%=$row->{unique_ips_last_3_hours}%></td>
<td><%=$row->{unique_ips_last_6_hours}%></td>
<td><%=$row->{unique_ips_last_12_hours}%></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>