Skip to content

A live bird collage card for your Home Assistant dashboard.

Weekly Report start

ehpersonal389c58e58d90b8
diff --git a/homepage/views.php b/homepage/views.php
index dd75f001..5618dad0 100644
--- a/homepage/views.php
+++ b/homepage/views.php
@@ -46,6 +46,9 @@ body::-webkit-scrollbar {
<form action="" method="GET" id="views">
<button type="submit" name="view" value="Recordings" form="views">Recordings</button>
</form>
+<form action="" method="GET" id="views">
+ <button type="submit" name="view" value="Weekly Report" form="views">Weekly Report</button>
+</form>
<form action="" method="GET" id="views">
<button type="submit" name="view" value="View Log" form="views">View Log</button>
</form>
@@ -90,6 +93,7 @@ if(isset($_GET['view'])){
if($_GET['view'] == "Overview"){include('overview.php');}
if($_GET['view'] == "Today's Detections"){include('todays_detections.php');}
if($_GET['view'] == "Species Stats"){include('stats.php');}
+ if($_GET['view'] == "Weekly Report"){include('weekly_report.php');}
if($_GET['view'] == "Streamlit"){echo "<iframe src=\"/stats\"></iframe>";}
if($_GET['view'] == "Daily Charts"){include('history.php');}
if($_GET['view'] == "Tools"){
diff --git a/scripts/update_birdnet_snippets.sh b/scripts/update_birdnet_snippets.sh
index c22d0bff..b03a0d7f 100755
--- a/scripts/update_birdnet_snippets.sh
+++ b/scripts/update_birdnet_snippets.sh
@@ -94,6 +94,7 @@ if [[ "$pytest_installation_status" = "not installed" ]];then
$HOME/BirdNET-Pi/birdnet/bin/pip3 install pytest==7.1.2 pytest-mock==3.7.0
fi
+[ -L ~/BirdSongs/Extracted/weekly_report.php ] || ln -sf ~/BirdNET-Pi/scripts/weekly_report.php ~/BirdSongs/Extracted
sudo systemctl daemon-reload
restart_services.sh
diff --git a/scripts/weekly_report.php b/scripts/weekly_report.php
new file mode 100644
index 00000000..0d8b262d
--- /dev/null
+++ b/scripts/weekly_report.php
@@ -0,0 +1,67 @@
+<?php
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+
+$startdate = strtotime('last sunday') - (7*86400);
+$enddate = strtotime('last sunday') - (1*86400);
+?>
+<div class="brbanner"> <?php
+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_CREATE | SQLITE3_OPEN_READWRITE);
+if($db == False){
+ echo "Database is busy";
+ header("refresh: 0;");
+}
+
+
+$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 LIMIT 10');
+if($statement1 == False){
+ echo "Database is busy";
+ header("refresh: 0;");
+}
+$result1 = $statement1->execute();
+
+?>
+<br>
+<table>
+<thead>
+ <tr>
+ <th><?php echo "Top 10 Species: <br>"; ?></th>
+ </tr>
+</thead>
+<tbody>
+<?php
+
+while($detection=$result1->fetchArray(SQLITE3_ASSOC))
+{
+
+$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$detection["Com_Name"].'" AND Date BETWEEN "'.date("Y-m-d",$startdate - (7*86400)).'" AND "'.date("Y-m-d",$enddate - (7*86400)).'"');
+if($statement2 == False){
+ echo "Database is busy";
+ header("refresh: 0;");
+}
+$result2 = $statement2->execute();
+$totalcount = $result2->fetchArray(SQLITE3_ASSOC);
+$priorweekcount = $totalcount['COUNT(*)'];
+
+$percentagediff = round((1 - $priorweekcount / $detection["COUNT(*)"]) * 100);
+
+if($percentagediff > 0) {
+ $percentagediff = "<span style='color:green;font-size:small'>+".$percentagediff."%</span>";
+} else {
+ $percentagediff = "<span style='color:red;font-size:small'>-".abs($percentagediff)."%</span>";
+}
+
+echo "<tr><td>".$detection["Com_Name"]."<br><small style=\"font-size:small\">".$detection["COUNT(*)"]." (".$percentagediff.")</small><br></td></tr>";
+}
+?>
+</tbody>
+</table>
+
+<br>
+<div style="text-align:center">
+ <small style="font-size:small">* percentages are calculated relative to week <?php echo date('W', $enddate) - 1; ?></small>
+</div>
\ No newline at end of file