From be8a98bf59f73803ce6ba5561f35fb5445656c27 Mon Sep 17 00:00:00 2001 From: frederik Date: Mon, 4 Mar 2024 18:29:12 +0100 Subject: [PATCH 1/7] fix: do not unset the session --- scripts/play.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/play.php b/scripts/play.php index 062e326..6239a3b 100644 --- a/scripts/play.php +++ b/scripts/play.php @@ -217,8 +217,8 @@ if(isset($_GET['bydate'])){ $result3 = $statement3->execute(); $view = "species"; } else { - session_start(); - session_unset(); + unset($_SESSION['species']); + unset($_SESSION['date']); $view = "choose"; } ?> From 90d53a3a43839886c5628f4c66c12de8df35f54f Mon Sep 17 00:00:00 2001 From: frederik Date: Fri, 8 Mar 2024 18:55:41 +0100 Subject: [PATCH 2/7] ensure_db_ok (): on error: do not refresh immediately, but wait for one second. put helpers in common.php --- scripts/common.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 scripts/common.php diff --git a/scripts/common.php b/scripts/common.php new file mode 100644 index 0000000..7b5910c --- /dev/null +++ b/scripts/common.php @@ -0,0 +1,10 @@ + Date: Mon, 4 Mar 2024 14:03:54 +0100 Subject: [PATCH 3/7] add busyTimeout() to deal with concurrency. open read-write only when needed --- scripts/config.php | 3 +- scripts/history.php | 13 ++++--- scripts/overview.php | 38 +++++--------------- scripts/play.php | 39 ++++++--------------- scripts/stats.php | 33 +++++------------- scripts/todays_detections.php | 43 ++++++----------------- scripts/weekly_report.php | 66 ++++++++--------------------------- 7 files changed, 60 insertions(+), 175 deletions(-) diff --git a/scripts/config.php b/scripts/config.php index bbb5772..b5f8f57 100644 --- a/scripts/config.php +++ b/scripts/config.php @@ -251,7 +251,8 @@ if(isset($_GET['sendtest']) && $_GET['sendtest'] == "true") { $config = parse_ini_file($home.'/BirdNET-Pi/scripts/thisrun.ini'); } - $db = new SQLite3($home."/BirdNET-Pi/scripts/birds.db", SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); + $db = new SQLite3($home."/BirdNET-Pi/scripts/birds.db", SQLITE3_OPEN_READONLY); + $db->busyTimeout(1000); $cf = explode("\n",$_GET['apprise_config']); $cf = "'".implode("' '", $cf)."'"; diff --git a/scripts/history.php b/scripts/history.php index 7b639f8..0a2992d 100644 --- a/scripts/history.php +++ b/scripts/history.php @@ -7,6 +7,7 @@ $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); error_reporting(E_ALL); ini_set('display_errors',1); ini_set('display_startup_errors',1); +require_once 'scripts/common.php'; if (file_exists('./scripts/thisrun.txt')) { $config = parse_ini_file('./scripts/thisrun.txt'); @@ -22,10 +23,11 @@ $theDate = date('Y-m-d'); $chart = "Combo-$theDate.png"; $chart2 = "Combo2-$theDate.png"; -$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); +$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY); +$db->busyTimeout(1000); -$statement1 = $db->prepare("SELECT COUNT(*) FROM detections - WHERE Date == \"$theDate\""); +$statement1 = $db->prepare("SELECT COUNT(*) FROM detections WHERE Date == \"$theDate\""); +ensure_db_ok($statement1); $result1 = $statement1->execute(); $totalcount = $result1->fetchArray(SQLITE3_ASSOC); @@ -52,10 +54,7 @@ if(isset($_GET['blocation']) ) { $starttime = strtotime("12 AM") + (3600*$i); $statement1 = $db->prepare("SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date == \"$theDate\" AND Time > '".date("H:i", $starttime)."' AND Time < '".date("H:i",$starttime + 3600)."' AND Confidence > 0.75 GROUP By Com_Name ORDER BY COUNT(*) DESC"); - if($statement1 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement1); $result1 = $statement1->execute(); $detections = []; diff --git a/scripts/overview.php b/scripts/overview.php index fd7e5b4..1abdf8a 100644 --- a/scripts/overview.php +++ b/scripts/overview.php @@ -5,6 +5,7 @@ ini_set('session.gc_maxlifetime', 7200); ini_set('user_agent', 'PHP_Flickr/1.0'); session_set_cookie_params(7200); session_start(); +require_once 'scripts/common.php'; if(!isset($_SESSION['my_timezone'])) { $_SESSION['my_timezone'] = trim(shell_exec('timedatectl show --value --property=Timezone')); @@ -13,11 +14,8 @@ date_default_timezone_set($_SESSION['my_timezone']); $myDate = date('Y-m-d'); $chart = "Combo-$myDate.png"; -$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); -if($db == False) { - echo "Database is busy"; - header("refresh: 0;"); -} +$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY); +$db->busyTimeout(1000); if (file_exists('./scripts/thisrun.txt')) { $config = parse_ini_file('./scripts/thisrun.txt'); @@ -30,10 +28,7 @@ $home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd"); $home = trim($home); $statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == DATE(\'now\', \'localtime\')'); -if($statement2 == False) { - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement2); $result2 = $statement2->execute(); $todaycount = $result2->fetchArray(SQLITE3_ASSOC); @@ -88,10 +83,7 @@ if(isset($_GET['fetch_chart_string']) && $_GET['fetch_chart_string'] == "true") if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" && isset($_GET['previous_detection_identifier'])) { $statement4 = $db->prepare('SELECT Com_Name, Sci_Name, Date, Time, Confidence, File_Name FROM detections ORDER BY Date DESC, Time DESC LIMIT 15'); - if($statement4 == False) { - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement4); $result4 = $statement4->execute(); if(!isset($_SESSION['images'])) { $_SESSION['images'] = []; @@ -246,34 +238,22 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" && isse if(isset($_GET['ajax_left_chart']) && $_GET['ajax_left_chart'] == "true") { $statement = $db->prepare('SELECT COUNT(*) FROM detections'); -if($statement == False) { - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement); $result = $statement->execute(); $totalcount = $result->fetchArray(SQLITE3_ASSOC); $statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == Date(\'now\', \'localtime\') AND TIME >= TIME(\'now\', \'localtime\', \'-1 hour\')'); -if($statement3 == False) { - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement3); $result3 = $statement3->execute(); $hourcount = $result3->fetchArray(SQLITE3_ASSOC); $statement5 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date == Date(\'now\',\'localtime\')'); -if($statement5 == False) { - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement5); $result5 = $statement5->execute(); $speciestally = $result5->fetchArray(SQLITE3_ASSOC); $statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections'); -if($statement6 == False) { - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement6); $result6 = $statement6->execute(); $totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC); diff --git a/scripts/play.php b/scripts/play.php index 6239a3b..4ffc31d 100644 --- a/scripts/play.php +++ b/scripts/play.php @@ -6,12 +6,10 @@ $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); error_reporting(E_ERROR); ini_set('display_errors',1); +require_once 'scripts/common.php'; -$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); -if($db == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY); +$db->busyTimeout(1000); if (file_exists('./scripts/thisrun.txt')) { $config = parse_ini_file('./scripts/thisrun.txt'); @@ -163,10 +161,7 @@ if (!isset($_SERVER['PHP_AUTH_USER'])) { if(isset($_GET['bydate'])){ $statement = $db->prepare('SELECT DISTINCT(Date) FROM detections GROUP BY Date ORDER BY Date DESC'); - if($statement == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement); $result = $statement->execute(); $view = "bydate"; @@ -180,10 +175,7 @@ if(isset($_GET['bydate'])){ } else { $statement = $db->prepare("SELECT DISTINCT(Com_Name) FROM detections WHERE Date == \"$date\" ORDER BY Com_Name"); } - if($statement == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement); $result = $statement->execute(); $view = "date"; @@ -195,10 +187,7 @@ if(isset($_GET['bydate'])){ $statement = $db->prepare('SELECT DISTINCT(Com_Name) FROM detections ORDER BY Com_Name ASC'); } session_start(); - if($statement == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement); $result = $statement->execute(); $view = "byspecies"; @@ -208,11 +197,9 @@ if(isset($_GET['bydate'])){ session_start(); $_SESSION['species'] = $species; $statement = $db->prepare("SELECT * FROM detections WHERE Com_Name == \"$species\" ORDER BY Com_Name"); + ensure_db_ok($statement); $statement3 = $db->prepare("SELECT Date, Time, Sci_Name, MAX(Confidence), File_Name FROM detections WHERE Com_Name == \"$species\" ORDER BY Com_Name"); - if($statement == False || $statement3 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement3); $result = $statement->execute(); $result3 = $statement3->execute(); $view = "species"; @@ -484,10 +471,7 @@ if(isset($_SESSION['date'])) { $statement2 = $db->prepare("SELECT * FROM detections where Com_Name == \"$name\" ORDER BY Date DESC, Time DESC"); } } -if($statement2 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement2); $result2 = $statement2->execute(); $num_rows = 0; while ($result2->fetchArray(SQLITE3_ASSOC)) { @@ -574,10 +558,7 @@ echo " if(isset($_GET['filename'])){ $name = $_GET['filename']; $statement2 = $db->prepare("SELECT * FROM detections where File_name == \"$name\" ORDER BY Date DESC, Time DESC"); - if($statement2 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement2); $result2 = $statement2->execute(); echo "
diff --git a/scripts/stats.php b/scripts/stats.php index 58add61..831a8f0 100644 --- a/scripts/stats.php +++ b/scripts/stats.php @@ -7,42 +7,28 @@ $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); ini_set('user_agent', 'PHP_Flickr/1.0'); error_reporting(0); ini_set('display_errors', 0); +require_once 'scripts/common.php'; -$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); -if($db == False) { - echo "Database busy"; - header("refresh: 0;"); -} +$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY); +$db->busyTimeout(1000); if(isset($_GET['sort']) && $_GET['sort'] == "occurrences") { $statement = $db->prepare('SELECT Date, Time, File_Name, Com_Name, COUNT(*), MAX(Confidence) FROM detections GROUP BY Com_Name ORDER BY COUNT(*) DESC'); - if($statement == False) { - echo "Database busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement); $result = $statement->execute(); $statement2 = $db->prepare('SELECT Date, Time, File_Name, Com_Name, COUNT(*), MAX(Confidence) FROM detections GROUP BY Com_Name ORDER BY COUNT(*) DESC'); - if($statement == False) { - echo "Database busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement2); $result2 = $statement2->execute(); } else { $statement = $db->prepare('SELECT Date, Time, File_Name, Com_Name, COUNT(*), MAX(Confidence) FROM detections GROUP BY Com_Name ORDER BY Com_Name ASC'); - if($statement == False) { - echo "Database busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement); $result = $statement->execute(); $statement2 = $db->prepare('SELECT Date, Time, File_Name, Com_Name, COUNT(*), MAX(Confidence) FROM detections GROUP BY Com_Name ORDER BY Com_Name ASC'); - if($statement == False) { - echo "Database busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement2); $result2 = $statement2->execute(); } @@ -51,10 +37,7 @@ if(isset($_GET['sort']) && $_GET['sort'] == "occurrences") { if(isset($_GET['species'])){ $selection = $_GET['species']; $statement3 = $db->prepare("SELECT Com_Name, Sci_Name, COUNT(*), MAX(Confidence), File_Name, Date, Time from detections WHERE Com_Name = \"$selection\""); - if($statement3 == False) { - echo "Database busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement3); $result3 = $statement3->execute(); } diff --git a/scripts/todays_detections.php b/scripts/todays_detections.php index 2238e65..78245a3 100644 --- a/scripts/todays_detections.php +++ b/scripts/todays_detections.php @@ -10,6 +10,7 @@ session_set_cookie_params(7200); session_start(); error_reporting(E_ERROR); ini_set('display_errors',1); +require_once 'scripts/common.php'; if (file_exists('./scripts/thisrun.txt')) { $config = parse_ini_file('./scripts/thisrun.txt'); @@ -28,57 +29,36 @@ if (file_exists('./scripts/thisrun.txt')) {

$site_name


"; } -$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); -if($db == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY); +$db->busyTimeout(1000); $statement1 = $db->prepare('SELECT COUNT(*) FROM detections'); -if($statement1 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement1); $result1 = $statement1->execute(); $totalcount = $result1->fetchArray(SQLITE3_ASSOC); $statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == DATE(\'now\', \'localtime\')'); -if($statement2 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement2); $result2 = $statement2->execute(); $todaycount = $result2->fetchArray(SQLITE3_ASSOC); $statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == Date(\'now\', \'localtime\') AND TIME >= TIME(\'now\', \'localtime\', \'-1 hour\')'); -if($statement3 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement3); $result3 = $statement3->execute(); $hourcount = $result3->fetchArray(SQLITE3_ASSOC); $statement4 = $db->prepare('SELECT Com_Name, Sci_Name, Time, Confidence FROM detections LIMIT 1'); -if($statement4 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement4); $result4 = $statement4->execute(); $mostrecent = $result4->fetchArray(SQLITE3_ASSOC); $statement5 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date == Date(\'now\', \'localtime\')'); -if($statement5 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement5); $result5 = $statement5->execute(); $todayspeciestally = $result5->fetchArray(SQLITE3_ASSOC); $statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections'); -if($statement6 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement6); $result6 = $statement6->execute(); $totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC); @@ -205,10 +185,7 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) { } } - if($statement0 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement0); $result0 = $statement0->execute(); ?>
diff --git a/scripts/weekly_report.php b/scripts/weekly_report.php index e625d3a..1a7ecd6 100644 --- a/scripts/weekly_report.php +++ b/scripts/weekly_report.php @@ -2,6 +2,7 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); +require_once 'scripts/common.php'; $startdate = strtotime('last sunday') - (7*86400); $enddate = strtotime('last sunday') - (1*86400); @@ -10,48 +11,30 @@ $debug = false; if(isset($_GET['ascii'])) { - $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); - if($db == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY); + $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'); - if($statement1 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement1); $result1 = $statement1->execute(); $statement4 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"'); - if($statement4 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement4); $result4 = $statement4->execute(); $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)).'"'); - if($statement5 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement5); $result5 = $statement5->execute(); $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).'"'); - if($statement6 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement6); $result6 = $statement6->execute(); $totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_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)).'"'); - if($statement7 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement7); $result7= $statement7->execute(); $priortotalspeciestally = $result7->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))']; @@ -91,10 +74,7 @@ if(isset($_GET['ascii'])) { 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)).'"'); - if($statement2 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement2); $result2 = $statement2->execute(); $totalcount = $result2->fetchArray(SQLITE3_ASSOC); $priorweekcount = $totalcount['COUNT(*)']; @@ -122,10 +102,7 @@ if(isset($_GET['ascii'])) { 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;"); - } + ensure_db_ok($statement3); $result3 = $statement3->execute(); $totalcount = $result3->fetchArray(SQLITE3_ASSOC); $nonthisweekcount = $totalcount['COUNT(*)']; @@ -153,21 +130,14 @@ if(isset($_GET['ascii'])) { echo "

Week ".date('W', $enddate)." Report

".date('F jS, Y',$startdate)." — ".date('F jS, Y',$enddate)."
"; ?>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'); } -if($statement1 == False){ - echo "Database is busy"; - header("refresh: 0;"); -} +ensure_db_ok($statement1); $result1 = $statement1->execute(); $detections = []; @@ -202,10 +172,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) $i++; 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)).'"'); - if($statement2 == False){ - echo "Database is busy"; - header("refresh: 0;"); - } + ensure_db_ok($statement2); $result2 = $statement2->execute(); $totalcount = $result2->fetchArray(SQLITE3_ASSOC); $priorweekcount = $totalcount['COUNT(*)']; @@ -243,10 +210,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC)) 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;"); - } + ensure_db_ok($statement3); $result3 = $statement3->execute(); $totalcount = $result3->fetchArray(SQLITE3_ASSOC); $nonthisweekcount = $totalcount['COUNT(*)']; From ed42cbf1e283f4282d7f2d67df3e5c9eed87e9be Mon Sep 17 00:00:00 2001 From: frederik Date: Mon, 4 Mar 2024 14:24:56 +0100 Subject: [PATCH 4/7] rework deletion: we _do_ need to open read-write here --- scripts/play.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/scripts/play.php b/scripts/play.php index 4ffc31d..bf08d29 100644 --- a/scripts/play.php +++ b/scripts/play.php @@ -27,20 +27,19 @@ if(isset($_GET['deletefile'])) { $submittedpwd = $_SERVER['PHP_AUTH_PW']; $submitteduser = $_SERVER['PHP_AUTH_USER']; if($submittedpwd == $config['CADDY_PWD'] && $submitteduser == 'birdnet'){ - $statement1 = $db->prepare('DELETE FROM detections WHERE File_Name = "'.explode("/",$_GET['deletefile'])[2].'" LIMIT 1'); - if($statement1 == False){ - echo "Error"; - header("refresh: 0;"); + $db_writable = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READWRITE); + $db->busyTimeout(1000); + $statement1 = $db_writable->prepare('DELETE FROM detections WHERE File_Name = :file_name LIMIT 1'); + ensure_db_ok($statement1); + $statement1->bindValue(':file_name', explode("/", $_GET['deletefile'])[2]); + $file_pointer = $home."/BirdSongs/Extracted/By_Date/".$_GET['deletefile']; + if (!exec("sudo rm $file_pointer && sudo rm $file_pointer.png")) { + echo "OK"; } else { - $file_pointer = $home."/BirdSongs/Extracted/By_Date/".$_GET['deletefile']; - if (!exec("sudo rm $file_pointer && sudo rm $file_pointer.png")) { - echo "OK"; - } else { - echo "Error"; - } - + echo "Error"; } $result1 = $statement1->execute(); + $db_writable->close(); die(); } else { header('WWW-Authenticate: Basic realm="My Realm"'); From 1d2c2d6faa0552e9b92f0c3c82d70dc9724d57dc Mon Sep 17 00:00:00 2001 From: frederik Date: Mon, 4 Mar 2024 19:18:58 +0100 Subject: [PATCH 5/7] make casual git checking non-blocking-y, also check every 24h --- homepage/views.php | 17 ++++++++++------- scripts/system_controls.php | 9 ++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/homepage/views.php b/homepage/views.php index 8b0ea32..c31b3a5 100644 --- a/homepage/views.php +++ b/homepage/views.php @@ -20,19 +20,22 @@ function service_mount() { $service_mount=trim(shell_exec("systemd-escape -p --suffix=mount ".$home."/BirdSongs/StreamData")); return $service_mount; } -if(!isset($_SESSION['behind'])) { - $fetch = shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi fetch 2>&1"); +if(!isset($_SESSION['behind']) || !isset($_SESSION['behind_time']) || time() > $_SESSION['behind_time'] + 86400) { + shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi fetch > /dev/null 2>/dev/null &"); $str = trim(shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi status")); if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) { $num_commits_behind = $matches[1]; - $_SESSION['behind'] = $num_commits_behind; } if (preg_match('/\b(\d+)\b and \b(\d+)\b different commits each/', $str, $matches)) { - $num1 = (int) $matches[1]; - $num2 = (int) $matches[2]; - $sum = $num1 + $num2; - $_SESSION['behind'] = $sum; + $num1 = (int) $matches[1]; + $num2 = (int) $matches[2]; + $num_commits_behind = $num1 + $num2; } + if (stripos($str, "Your branch is up to date") !== false) { + $num_commits_behind = '0'; + } + $_SESSION['behind'] = $num_commits_behind; + $_SESSION['behind_time'] = time(); } if(isset($_SESSION['behind'])&&intval($_SESSION['behind']) >= 99) {?> WARNING: Your latitude and longitude are not set properly. Please do so now in Tools -> Settings."; } From 68e61d2517402b466db8196144a339f9051be846 Mon Sep 17 00:00:00 2001 From: frederik Date: Tue, 5 Mar 2024 09:27:28 +0100 Subject: [PATCH 7/7] only check git when logged in --- homepage/views.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homepage/views.php b/homepage/views.php index ef1cbe8..879ef37 100644 --- a/homepage/views.php +++ b/homepage/views.php @@ -25,7 +25,8 @@ function service_mount() { $service_mount=trim(shell_exec("systemd-escape -p --suffix=mount ".$home."/BirdSongs/StreamData")); return $service_mount; } -if(!isset($_SESSION['behind']) || !isset($_SESSION['behind_time']) || time() > $_SESSION['behind_time'] + 86400) { +$authenticated = isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_PW'] == $config['CADDY_PWD'] && $_SERVER['PHP_AUTH_USER'] == 'birdnet'; +if($authenticated && (!isset($_SESSION['behind']) || !isset($_SESSION['behind_time']) || time() > $_SESSION['behind_time'] + 86400)) { shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi fetch > /dev/null 2>/dev/null &"); $str = trim(shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi status")); if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) {