consolidate weekly stats and make common name agnostic
This commit is contained in:
+42
-76
@@ -22,36 +22,57 @@ function safe_percentage($count, $prior_count) {
|
|||||||
return $percentagediff;
|
return $percentagediff;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($_GET['ascii'])) {
|
|
||||||
|
|
||||||
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
|
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
|
||||||
$db->busyTimeout(1000);
|
$db->busyTimeout(1000);
|
||||||
|
|
||||||
$statement1 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'" GROUP By Com_Name ORDER BY COUNT(*) DESC');
|
$statement1 = $db->prepare('SELECT Sci_Name, Com_Name, COUNT(*) FROM detections WHERE Date BETWEEN "' . date("Y-m-d", $startdate) . '" AND "' . date("Y-m-d", $enddate) . '" GROUP By Sci_Name ORDER BY COUNT(*) DESC');
|
||||||
ensure_db_ok($statement1);
|
ensure_db_ok($statement1);
|
||||||
$result1 = $statement1->execute();
|
$result1 = $statement1->execute();
|
||||||
|
while ($detection = $result1->fetchArray(SQLITE3_ASSOC)) {
|
||||||
|
$com_name = $detection["Com_Name"];
|
||||||
|
$sci_name = $detection["Sci_Name"];
|
||||||
|
$scount = $detection["COUNT(*)"];
|
||||||
|
|
||||||
$statement4 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
|
# previous week
|
||||||
|
$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Sci_Name == "' . $detection["Sci_Name"] . '" AND Date BETWEEN "' . date("Y-m-d", $startdate - (7 * 86400)) . '" AND "' . date("Y-m-d", $enddate - (7 * 86400)) . '"');
|
||||||
|
ensure_db_ok($statement2);
|
||||||
|
$result2 = $statement2->execute();
|
||||||
|
$priorweekcount = $result2->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
|
||||||
|
$percentagediff = safe_percentage($scount, $priorweekcount);
|
||||||
|
|
||||||
|
# is_first_seen?
|
||||||
|
$statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Sci_Name == "'.$sci_name.'" AND Date NOT BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
|
||||||
|
ensure_db_ok($statement3);
|
||||||
|
$result3 = $statement3->execute();
|
||||||
|
$totalcount = $result3->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
|
||||||
|
$is_first_seen = $totalcount === 0;
|
||||||
|
|
||||||
|
$detections[$com_name] = ["count" => $scount, "percentagediff" => $percentagediff, "is_first_seen" => $is_first_seen];
|
||||||
|
}
|
||||||
|
|
||||||
|
$statement4 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
|
||||||
ensure_db_ok($statement4);
|
ensure_db_ok($statement4);
|
||||||
$result4 = $statement4->execute();
|
$result4 = $statement4->execute();
|
||||||
$totalcount = $result4->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
|
$totalcount = $result4->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
|
||||||
|
|
||||||
$statement5 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate- (7*86400)).'" AND "'.date("Y-m-d",$enddate- (7*86400)).'"');
|
$statement5 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate- (7*86400)).'" AND "'.date("Y-m-d",$enddate- (7*86400)).'"');
|
||||||
ensure_db_ok($statement5);
|
ensure_db_ok($statement5);
|
||||||
$result5 = $statement5->execute();
|
$result5 = $statement5->execute();
|
||||||
$priortotalcount = $result5->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
|
$priortotalcount = $result5->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
|
||||||
|
|
||||||
$statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
|
$statement6 = $db->prepare('SELECT COUNT(DISTINCT(Sci_Name)) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
|
||||||
ensure_db_ok($statement6);
|
ensure_db_ok($statement6);
|
||||||
$result6 = $statement6->execute();
|
$result6 = $statement6->execute();
|
||||||
$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))'];
|
$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Sci_Name))'];
|
||||||
|
|
||||||
$statement7 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate- (7*86400)).'" AND "'.date("Y-m-d",$enddate- (7*86400)).'"');
|
$statement7 = $db->prepare('SELECT COUNT(DISTINCT(Sci_Name)) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate- (7*86400)).'" AND "'.date("Y-m-d",$enddate- (7*86400)).'"');
|
||||||
ensure_db_ok($statement7);
|
ensure_db_ok($statement7);
|
||||||
$result7= $statement7->execute();
|
$result7= $statement7->execute();
|
||||||
$priortotalspeciestally = $result7->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))'];
|
$priortotalspeciestally = $result7->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Sci_Name))'];
|
||||||
|
|
||||||
$percentagedifftotal = safe_percentage($totalcount, $priortotalcount);
|
$percentagedifftotal = safe_percentage($totalcount, $priortotalcount);
|
||||||
|
|
||||||
|
if(isset($_GET['ascii'])) {
|
||||||
if($percentagedifftotal > 0) {
|
if($percentagedifftotal > 0) {
|
||||||
$percentagedifftotal = "<span style='color:green;font-size:small'>+".$percentagedifftotal."%</span>";
|
$percentagedifftotal = "<span style='color:green;font-size:small'>+".$percentagedifftotal."%</span>";
|
||||||
} else {
|
} else {
|
||||||
@@ -65,13 +86,6 @@ if(isset($_GET['ascii'])) {
|
|||||||
$percentagedifftotaldistinctspecies = "<span style='color:red;font-size:small'>-".abs($percentagedifftotaldistinctspecies)."%</span>";
|
$percentagedifftotaldistinctspecies = "<span style='color:red;font-size:small'>-".abs($percentagedifftotaldistinctspecies)."%</span>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$detections = [];
|
|
||||||
$i = 0;
|
|
||||||
while($detection=$result1->fetchArray(SQLITE3_ASSOC))
|
|
||||||
{
|
|
||||||
$detections[$detection["Com_Name"]] = $detection["COUNT(*)"];
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "# BirdNET-Pi: Week ".date('W', $enddate)." Report\n";
|
echo "# BirdNET-Pi: Week ".date('W', $enddate)." Report\n";
|
||||||
|
|
||||||
echo "Total Detections: <b>".$totalcount."</b> (".$percentagedifftotal.")<br>";
|
echo "Total Detections: <b>".$totalcount."</b> (".$percentagedifftotal.")<br>";
|
||||||
@@ -80,19 +94,12 @@ if(isset($_GET['ascii'])) {
|
|||||||
echo "= <b>Top 10 Species</b> =<br>";
|
echo "= <b>Top 10 Species</b> =<br>";
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach($detections as $com_name=>$scount)
|
foreach($detections as $com_name=>$stats)
|
||||||
{
|
{
|
||||||
|
$count = $stats["count"];
|
||||||
|
$percentagediff = $stats["percentagediff"];
|
||||||
$i++;
|
$i++;
|
||||||
|
|
||||||
if($i <= 10) {
|
if($i <= 10) {
|
||||||
$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date BETWEEN "'.date("Y-m-d",$startdate - (7*86400)).'" AND "'.date("Y-m-d",$enddate - (7*86400)).'"');
|
|
||||||
ensure_db_ok($statement2);
|
|
||||||
$result2 = $statement2->execute();
|
|
||||||
$totalcount = $result2->fetchArray(SQLITE3_ASSOC);
|
|
||||||
$priorweekcount = $totalcount['COUNT(*)'];
|
|
||||||
|
|
||||||
// really percent changed
|
|
||||||
$percentagediff = safe_percentage($scount, $priorweekcount);
|
|
||||||
if($percentagediff > 0) {
|
if($percentagediff > 0) {
|
||||||
$percentagediff = "<span style='color:green;font-size:small'>+".$percentagediff."%</span>";
|
$percentagediff = "<span style='color:green;font-size:small'>+".$percentagediff."%</span>";
|
||||||
} else {
|
} else {
|
||||||
@@ -106,15 +113,9 @@ if(isset($_GET['ascii'])) {
|
|||||||
echo "<br>= <b>Species Detected for the First Time</b> =<br>";
|
echo "<br>= <b>Species Detected for the First Time</b> =<br>";
|
||||||
|
|
||||||
$newspeciescount=0;
|
$newspeciescount=0;
|
||||||
foreach($detections as $com_name=>$scount)
|
foreach($detections as $com_name=>$stats)
|
||||||
{
|
{
|
||||||
$statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date NOT BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
|
if($stats["is_first_seen"]) {
|
||||||
ensure_db_ok($statement3);
|
|
||||||
$result3 = $statement3->execute();
|
|
||||||
$totalcount = $result3->fetchArray(SQLITE3_ASSOC);
|
|
||||||
$nonthisweekcount = $totalcount['COUNT(*)'];
|
|
||||||
|
|
||||||
if($nonthisweekcount == 0) {
|
|
||||||
$newspeciescount++;
|
$newspeciescount++;
|
||||||
echo $com_name." - ".$scount."<br>";
|
echo $com_name." - ".$scount."<br>";
|
||||||
}
|
}
|
||||||
@@ -135,32 +136,8 @@ if(isset($_GET['ascii'])) {
|
|||||||
?>
|
?>
|
||||||
<div class="brbanner"> <?php
|
<div class="brbanner"> <?php
|
||||||
echo "<h1>Week ".date('W', $enddate)." Report</h1>".date('F jS, Y',$startdate)." — ".date('F jS, Y',$enddate)."<br>";
|
echo "<h1>Week ".date('W', $enddate)." Report</h1>".date('F jS, Y',$startdate)." — ".date('F jS, Y',$enddate)."<br>";
|
||||||
?></div><?php
|
|
||||||
|
|
||||||
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
|
|
||||||
$db->busyTimeout(1000);
|
|
||||||
if($debug == false){
|
|
||||||
$statement1 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'" GROUP By Com_Name ORDER BY COUNT(*) DESC');
|
|
||||||
} else {
|
|
||||||
$statement1 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'" GROUP By Com_Name ORDER BY COUNT(*) ASC');
|
|
||||||
}
|
|
||||||
ensure_db_ok($statement1);
|
|
||||||
$result1 = $statement1->execute();
|
|
||||||
|
|
||||||
$detections = [];
|
|
||||||
$i = 0;
|
|
||||||
while($detection=$result1->fetchArray(SQLITE3_ASSOC))
|
|
||||||
{
|
|
||||||
if($debug == true){
|
|
||||||
if($i > 10) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$i++;
|
|
||||||
$detections[$detection["Com_Name"]] = $detection["COUNT(*)"];
|
|
||||||
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
|
</div>
|
||||||
<br>
|
<br>
|
||||||
<?php // TODO: fix the box shadows, maybe make them a bit smaller on the tr ?>
|
<?php // TODO: fix the box shadows, maybe make them a bit smaller on the tr ?>
|
||||||
<table align="center" style="box-shadow:unset"><tr><td style="background-color:transparent">
|
<table align="center" style="box-shadow:unset"><tr><td style="background-color:transparent">
|
||||||
@@ -174,24 +151,19 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC))
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach($detections as $com_name=>$scount)
|
foreach($detections as $com_name=>$stats)
|
||||||
{
|
{
|
||||||
$i++;
|
$i++;
|
||||||
if($i <= 10) {
|
if($i <= 10) {
|
||||||
$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date BETWEEN "'.date("Y-m-d",$startdate - (7*86400)).'" AND "'.date("Y-m-d",$enddate - (7*86400)).'"');
|
$count = $stats["count"];
|
||||||
ensure_db_ok($statement2);
|
$percentagediff = $stats["percentagediff"];
|
||||||
$result2 = $statement2->execute();
|
|
||||||
$totalcount = $result2->fetchArray(SQLITE3_ASSOC);
|
|
||||||
$priorweekcount = $totalcount['COUNT(*)'];
|
|
||||||
|
|
||||||
$percentagediff = safe_percentage($scount, $priorweekcount);
|
|
||||||
if($percentagediff > 0) {
|
if($percentagediff > 0) {
|
||||||
$percentagediff = "<span style='color:green;font-size:small'>+".$percentagediff."%</span>";
|
$percentagediff = "<span style='color:green;font-size:small'>+".$percentagediff."%</span>";
|
||||||
} else {
|
} else {
|
||||||
$percentagediff = "<span style='color:red;font-size:small'>-".abs($percentagediff)."%</span>";
|
$percentagediff = "<span style='color:red;font-size:small'>-".abs($percentagediff)."%</span>";
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "<tr><td>".$com_name."<br><small style=\"font-size:small\">".$scount." (".$percentagediff.")</small><br></td></tr>";
|
echo "<tr><td>".$com_name."<br><small style=\"font-size:small\">".$count." (".$percentagediff.")</small><br></td></tr>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@@ -209,15 +181,9 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC))
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$newspeciescount=0;
|
$newspeciescount=0;
|
||||||
foreach($detections as $com_name=>$scount)
|
foreach($detections as $com_name=>$stats)
|
||||||
{
|
{
|
||||||
$statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date NOT BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
|
if($stats["is_first_seen"]) {
|
||||||
ensure_db_ok($statement3);
|
|
||||||
$result3 = $statement3->execute();
|
|
||||||
$totalcount = $result3->fetchArray(SQLITE3_ASSOC);
|
|
||||||
$nonthisweekcount = $totalcount['COUNT(*)'];
|
|
||||||
|
|
||||||
if($nonthisweekcount == 0) {
|
|
||||||
$newspeciescount++;
|
$newspeciescount++;
|
||||||
echo "<tr><td>".$com_name."<br><small style=\"font-size:small\">".$scount."</small><br></td></tr>";
|
echo "<tr><td>".$com_name."<br><small style=\"font-size:small\">".$scount."</small><br></td></tr>";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user