Skip to content
Snippets Groups Projects
Commit 9c254090 authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Added start & end timestamps to stats retrieval

We also now calculate which key to use to give us the number of stats we're after
parent 4a34dd82
No related branches found
No related tags found
No related merge requests found
......@@ -796,10 +796,10 @@ sub getLastStats
# Return stats by SID
sub getStatsBySID
{
my ($sid,$conversions) = @_;
my ($sid,$conversions,$startTimestamp,$endTimestamp) = @_;
my $statistics = _getStatsBySID($sid);
my $statistics = _getStatsBySID($sid,$startTimestamp,$endTimestamp);
if (!defined($statistics)) {
return;
}
......@@ -1227,11 +1227,32 @@ sub _getAlignedTime
# Internal function to get stats by SID
sub _getStatsBySID
{
my $sid = shift;
my ($sid,$startTimestamp,$endTimestamp) = @_;
my $now = time();
# Setup our timestamps if we need to
if (!defined($startTimestamp)) {
$startTimestamp = $now - 3600;
}
if (!defined($endTimestamp)) {
$endTimestamp = $now;
}
# Work out the timestamp
my $timespan = $endTimestamp - $startTimestamp;
# Find the best key to use...
my $statsKey = 0;
foreach my $key (sort {$b <=> $a} keys %{$statsConfig}) {
# Grab first key that will hve 50+ entries
if ($timespan / $statsConfig->{$key}->{'precision'} > 50) {
$statsKey = $key;
last;
}
}
# Prepare query
my $sth = $globals->{'DBHandle'}->prepare('
SELECT
......@@ -1245,10 +1266,9 @@ sub _getStatsBySID
AND `Timestamp` < ?
ORDER BY
`Timestamp` DESC
LIMIT 100
');
# Grab last 60 mins of data
$sth->execute($sid,0,$now - 3600, $now);
$sth->execute($sid,$statsKey,$startTimestamp,$endTimestamp);
my $statistics;
while (my $item = $sth->fetchrow_hashref()) {
......@@ -1268,11 +1288,32 @@ sub _getStatsBySID
# Internal function to get basic stats by SID
sub _getStatsBasicBySID
{
my $sid = shift;
my ($sid,$startTimestamp,$endTimestamp) = @_;
my $now = time();
# Setup our timestamps if we need to
if (!defined($startTimestamp)) {
$startTimestamp = $now - 3600;
}
if (!defined($endTimestamp)) {
$endTimestamp = $now;
}
# Work out the timestamp
my $timespan = $endTimestamp - $startTimestamp;
# Find the best key to use...
my $statsKey = 0;
foreach my $key (sort {$b <=> $a} keys %{$statsConfig}) {
# Grab first key that will hve 50+ entries
if ($timespan / $statsConfig->{$key}->{'precision'} > 50) {
$statsKey = $key;
last;
}
}
# Prepare query
my $sth = $globals->{'DBHandle'}->prepare('
SELECT
......@@ -1286,10 +1327,9 @@ sub _getStatsBasicBySID
AND `Timestamp` < ?
ORDER BY
`Timestamp` DESC
LIMIT 100
');
# Grab last 60 mins of data
$sth->execute($sid,0,$now - 3600, $now);
$sth->execute($sid,$statsKey,$startTimestamp,$endTimestamp);
my $statistics;
while (my $item = $sth->fetchrow_hashref()) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment