From 23c35e4fe0c83a47182ff73ee7b9b17b4af72ff4 Mon Sep 17 00:00:00 2001 From: ehpersonal38 <103586016+ehpersonal38@users.noreply.github.com> Date: Wed, 29 Jun 2022 09:01:51 -0400 Subject: [PATCH] fixing formatting on email --- scripts/weekly_report.php | 77 +++++++++++++++++++++--------------- scripts/weekly_report.sh | 2 +- templates/weekly_report.cron | 2 +- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/scripts/weekly_report.php b/scripts/weekly_report.php index 9231ed3..c0966e6 100644 --- a/scripts/weekly_report.php +++ b/scripts/weekly_report.php @@ -1,7 +1,4 @@ fetchArray(SQLITE3_ASSOC)) { - array_push($detections, $detection); + $detections[$detection["Com_Name"]] = $detection["COUNT(*)"]; } echo "# Week ".date('W', $enddate)." Report (".date('F jS, Y',$startdate)." — ".date('F jS, Y',$enddate).")\n"; - echo "= Top 10 Species =\n"; + echo "= Top 10 Species =
"; $i = 0; - foreach($detections as $detection) + foreach($detections as $com_name=>$scount) { $i++; if($i <= 10) { - $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)).'"'); + $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)).'"'); if($statement2 == False){ echo "Database is busy"; header("refresh: 0;"); @@ -49,18 +46,41 @@ if(isset($_GET['ascii'])) { $totalcount = $result2->fetchArray(SQLITE3_ASSOC); $priorweekcount = $totalcount['COUNT(*)']; - $percentagediff = round((1 - $priorweekcount / $detection["COUNT(*)"]) * 100); + $percentagediff = round((1 - $priorweekcount / $scount) * 100); if($percentagediff > 0) { - $percentagediff = "+".$percentagediff."%"; + $percentagediff = "+".$percentagediff."%"; } else { - $percentagediff = "-".abs($percentagediff)."%"; + $percentagediff = "-".abs($percentagediff)."%"; } - echo $detection["Com_Name"]." - ".$detection["COUNT(*)"]." (".$percentagediff.")\n"; + echo $com_name." - ".$scount." (".$percentagediff.")
"; } } + echo "
= Species Detected for the First Time: =
"; + + $newspeciescount=0; + foreach($detections as $com_name=>$scount) + { + $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($statement3 == False){ + echo "Database is busy"; + header("refresh: 0;"); + } + $result3 = $statement3->execute(); + $totalcount = $result3->fetchArray(SQLITE3_ASSOC); + $nonthisweekcount = $totalcount['COUNT(*)']; + + if($nonthisweekcount == 0) { + $newspeciescount++; + echo $com_name." - ".$scount."
"; + } + } + if($newspeciescount == 0) { + echo "No new species were seen this week."; + } + die(); } @@ -96,7 +116,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) } } $i++; - array_push($detections, $detection); + $detections[$detection["Com_Name"]] = $detection["COUNT(*)"]; } ?> @@ -113,12 +133,11 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) $scount) { $i++; - if($i <= 10) { - $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)).'"'); + $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)).'"'); if($statement2 == False){ echo "Database is busy"; header("refresh: 0;"); @@ -127,7 +146,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) $totalcount = $result2->fetchArray(SQLITE3_ASSOC); $priorweekcount = $totalcount['COUNT(*)']; - $percentagediff = round((1 - $priorweekcount / $detection["COUNT(*)"]) * 100); + $percentagediff = round((1 - $priorweekcount / $scount) * 100); if($percentagediff > 0) { $percentagediff = "+".$percentagediff."%"; @@ -135,7 +154,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) $percentagediff = "-".abs($percentagediff)."%"; } - echo "".$detection["Com_Name"]."
".$detection["COUNT(*)"]." (".$percentagediff.")
"; + echo "".$com_name."
".$scount." (".$percentagediff.")
"; } } ?> @@ -146,23 +165,16 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) - + $val) { - if ($val['Com_Name'] === $id) { - return $key; - } - } - return null; - } - foreach($detections as $detection) + $newspeciescount=0; + foreach($detections as $com_name=>$scount) { - $statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$detection["Com_Name"].'" AND Date NOT BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"'); + $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($statement3 == False){ echo "Database is busy"; header("refresh: 0;"); @@ -171,13 +183,14 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) $totalcount = $result3->fetchArray(SQLITE3_ASSOC); $nonthisweekcount = $totalcount['COUNT(*)']; - // TODO: add a "no species were seen for the first time this week", if applicable - if($nonthisweekcount == 0) { - $key = array_search($detection["Com_Name"], array_column($detections, 'Com_Name')); - echo ""; + $newspeciescount++; + echo ""; } } + if($newspeciescount == 0) { + echo ""; + } ?>
"; ?>"; ?>
".$detection["Com_Name"]."
".$detections[$key]["COUNT(*)"]."
".$com_name."
".$scount."
No new species were seen this week.
diff --git a/scripts/weekly_report.sh b/scripts/weekly_report.sh index db605d2..8bfaee7 100755 --- a/scripts/weekly_report.sh +++ b/scripts/weekly_report.sh @@ -5,5 +5,5 @@ if [ ${APPRISE_WEEKLY_REPORT} == 1 ];then NOTIFICATION=${NOTIFICATION#*#} firstLine=`echo "${NOTIFICATION}" | head -1` NOTIFICATION=`echo "${NOTIFICATION}" | tail -n +2` - $HOME/BirdNET-Pi/birdnet/bin/apprise -vv -t "${firstLine}" -b "${NOTIFICATION}" --config=$HOME/BirdNET-Pi/apprise.txt + $HOME/BirdNET-Pi/birdnet/bin/apprise -vv -t "${firstLine}" -b "${NOTIFICATION}" --input-format=html --config=$HOME/BirdNET-Pi/apprise.txt fi \ No newline at end of file diff --git a/templates/weekly_report.cron b/templates/weekly_report.cron index 466c749..1475f25 100644 --- a/templates/weekly_report.cron +++ b/templates/weekly_report.cron @@ -1,2 +1,2 @@ #birdnet -0 7 * * 1 $USER /usr/local/bin/weekly_report.sh >/dev/null 2>&1 \ No newline at end of file +0 1 * * 1 $USER /usr/local/bin/weekly_report.sh >/dev/null 2>&1 \ No newline at end of file