Merge branch 'sqlite-timeout'

This commit is contained in:
frederik
2024-03-09 13:27:10 +01:00
10 changed files with 104 additions and 205 deletions
+16 -14
View File
@@ -15,24 +15,33 @@ $user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
$user = trim($user); $user = trim($user);
$home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd"); $home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd");
$home = trim($home); $home = trim($home);
if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./scripts/thisrun.txt');
} elseif (file_exists('./scripts/firstrun.ini')) {
$config = parse_ini_file('./scripts/firstrun.ini');
}
function service_mount() { function service_mount() {
global $home; global $home;
$service_mount=trim(shell_exec("systemd-escape -p --suffix=mount ".$home."/BirdSongs/StreamData")); $service_mount=trim(shell_exec("systemd-escape -p --suffix=mount ".$home."/BirdSongs/StreamData"));
return $service_mount; return $service_mount;
} }
if(!isset($_SESSION['behind'])) { $authenticated = isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_PW'] == $config['CADDY_PWD'] && $_SERVER['PHP_AUTH_USER'] == 'birdnet';
$fetch = shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi fetch 2>&1"); 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")); $str = trim(shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi status"));
if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) { if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) {
$num_commits_behind = $matches[1]; $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)) { if (preg_match('/\b(\d+)\b and \b(\d+)\b different commits each/', $str, $matches)) {
$num1 = (int) $matches[1]; $num1 = (int) $matches[1];
$num2 = (int) $matches[2]; $num2 = (int) $matches[2];
$sum = $num1 + $num2; $num_commits_behind = $num1 + $num2;
$_SESSION['behind'] = $sum;
} }
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) {?> if(isset($_SESSION['behind'])&&intval($_SESSION['behind']) >= 99) {?>
<style> <style>
@@ -41,13 +50,6 @@ if(isset($_SESSION['behind'])&&intval($_SESSION['behind']) >= 99) {?>
} }
</style> </style>
<?php } <?php }
if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./scripts/thisrun.txt');
} elseif (file_exists('./scripts/firstrun.ini')) {
$config = parse_ini_file('./scripts/firstrun.ini');
}
if ($config["LATITUDE"] == "0.000" && $config["LONGITUDE"] == "0.000") { if ($config["LATITUDE"] == "0.000" && $config["LONGITUDE"] == "0.000") {
echo "<center style='color:red'><b>WARNING: Your latitude and longitude are not set properly. Please do so now in Tools -> Settings.</center></b>"; echo "<center style='color:red'><b>WARNING: Your latitude and longitude are not set properly. Please do so now in Tools -> Settings.</center></b>";
} }
+10
View File
@@ -0,0 +1,10 @@
<?php
function ensure_db_ok ($sql_stmt) {
if($sql_stmt == False) {
echo "Database is busy";
header("refresh:1;");
exit;
}
}
+2 -1
View File
@@ -251,7 +251,8 @@ if(isset($_GET['sendtest']) && $_GET['sendtest'] == "true") {
$config = parse_ini_file($home.'/BirdNET-Pi/scripts/thisrun.ini'); $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 = explode("\n",$_GET['apprise_config']);
$cf = "'".implode("' '", $cf)."'"; $cf = "'".implode("' '", $cf)."'";
+6 -7
View File
@@ -7,6 +7,7 @@ $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
error_reporting(E_ALL); error_reporting(E_ALL);
ini_set('display_errors',1); ini_set('display_errors',1);
ini_set('display_startup_errors',1); ini_set('display_startup_errors',1);
require_once 'scripts/common.php';
if (file_exists('./scripts/thisrun.txt')) { if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./scripts/thisrun.txt'); $config = parse_ini_file('./scripts/thisrun.txt');
@@ -22,10 +23,11 @@ $theDate = date('Y-m-d');
$chart = "Combo-$theDate.png"; $chart = "Combo-$theDate.png";
$chart2 = "Combo2-$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 $statement1 = $db->prepare("SELECT COUNT(*) FROM detections WHERE Date == \"$theDate\"");
WHERE Date == \"$theDate\""); ensure_db_ok($statement1);
$result1 = $statement1->execute(); $result1 = $statement1->execute();
$totalcount = $result1->fetchArray(SQLITE3_ASSOC); $totalcount = $result1->fetchArray(SQLITE3_ASSOC);
@@ -52,10 +54,7 @@ if(isset($_GET['blocation']) ) {
$starttime = strtotime("12 AM") + (3600*$i); $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"); $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){ ensure_db_ok($statement1);
echo "Database is busy";
header("refresh: 0;");
}
$result1 = $statement1->execute(); $result1 = $statement1->execute();
$detections = []; $detections = [];
+9 -29
View File
@@ -5,6 +5,7 @@ ini_set('session.gc_maxlifetime', 7200);
ini_set('user_agent', 'PHP_Flickr/1.0'); ini_set('user_agent', 'PHP_Flickr/1.0');
session_set_cookie_params(7200); session_set_cookie_params(7200);
session_start(); session_start();
require_once 'scripts/common.php';
if(!isset($_SESSION['my_timezone'])) { if(!isset($_SESSION['my_timezone'])) {
$_SESSION['my_timezone'] = trim(shell_exec('timedatectl show --value --property=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'); $myDate = date('Y-m-d');
$chart = "Combo-$myDate.png"; $chart = "Combo-$myDate.png";
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
if($db == False) { $db->busyTimeout(1000);
echo "Database is busy";
header("refresh: 0;");
}
if (file_exists('./scripts/thisrun.txt')) { if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./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); $home = trim($home);
$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == DATE(\'now\', \'localtime\')'); $statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == DATE(\'now\', \'localtime\')');
if($statement2 == False) { ensure_db_ok($statement2);
echo "Database is busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
$todaycount = $result2->fetchArray(SQLITE3_ASSOC); $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'])) { 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'); $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) { ensure_db_ok($statement4);
echo "Database is busy";
header("refresh: 0;");
}
$result4 = $statement4->execute(); $result4 = $statement4->execute();
if(!isset($_SESSION['images'])) { if(!isset($_SESSION['images'])) {
$_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") { if(isset($_GET['ajax_left_chart']) && $_GET['ajax_left_chart'] == "true") {
$statement = $db->prepare('SELECT COUNT(*) FROM detections'); $statement = $db->prepare('SELECT COUNT(*) FROM detections');
if($statement == False) { ensure_db_ok($statement);
echo "Database is busy";
header("refresh: 0;");
}
$result = $statement->execute(); $result = $statement->execute();
$totalcount = $result->fetchArray(SQLITE3_ASSOC); $totalcount = $result->fetchArray(SQLITE3_ASSOC);
$statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == Date(\'now\', \'localtime\') AND TIME >= TIME(\'now\', \'localtime\', \'-1 hour\')'); $statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == Date(\'now\', \'localtime\') AND TIME >= TIME(\'now\', \'localtime\', \'-1 hour\')');
if($statement3 == False) { ensure_db_ok($statement3);
echo "Database is busy";
header("refresh: 0;");
}
$result3 = $statement3->execute(); $result3 = $statement3->execute();
$hourcount = $result3->fetchArray(SQLITE3_ASSOC); $hourcount = $result3->fetchArray(SQLITE3_ASSOC);
$statement5 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date == Date(\'now\',\'localtime\')'); $statement5 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date == Date(\'now\',\'localtime\')');
if($statement5 == False) { ensure_db_ok($statement5);
echo "Database is busy";
header("refresh: 0;");
}
$result5 = $statement5->execute(); $result5 = $statement5->execute();
$speciestally = $result5->fetchArray(SQLITE3_ASSOC); $speciestally = $result5->fetchArray(SQLITE3_ASSOC);
$statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections'); $statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections');
if($statement6 == False) { ensure_db_ok($statement6);
echo "Database is busy";
header("refresh: 0;");
}
$result6 = $statement6->execute(); $result6 = $statement6->execute();
$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC); $totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC);
+22 -42
View File
@@ -6,12 +6,10 @@ $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
error_reporting(E_ERROR); error_reporting(E_ERROR);
ini_set('display_errors',1); ini_set('display_errors',1);
require_once 'scripts/common.php';
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
if($db == False){ $db->busyTimeout(1000);
echo "Database is busy";
header("refresh: 0;");
}
if (file_exists('./scripts/thisrun.txt')) { if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./scripts/thisrun.txt'); $config = parse_ini_file('./scripts/thisrun.txt');
@@ -29,20 +27,19 @@ if(isset($_GET['deletefile'])) {
$submittedpwd = $_SERVER['PHP_AUTH_PW']; $submittedpwd = $_SERVER['PHP_AUTH_PW'];
$submitteduser = $_SERVER['PHP_AUTH_USER']; $submitteduser = $_SERVER['PHP_AUTH_USER'];
if($submittedpwd == $config['CADDY_PWD'] && $submitteduser == 'birdnet'){ if($submittedpwd == $config['CADDY_PWD'] && $submitteduser == 'birdnet'){
$statement1 = $db->prepare('DELETE FROM detections WHERE File_Name = "'.explode("/",$_GET['deletefile'])[2].'" LIMIT 1'); $db_writable = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READWRITE);
if($statement1 == False){ $db->busyTimeout(1000);
echo "Error"; $statement1 = $db_writable->prepare('DELETE FROM detections WHERE File_Name = :file_name LIMIT 1');
header("refresh: 0;"); 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 { } else {
$file_pointer = $home."/BirdSongs/Extracted/By_Date/".$_GET['deletefile']; echo "Error";
if (!exec("sudo rm $file_pointer && sudo rm $file_pointer.png")) {
echo "OK";
} else {
echo "Error";
}
} }
$result1 = $statement1->execute(); $result1 = $statement1->execute();
$db_writable->close();
die(); die();
} else { } else {
header('WWW-Authenticate: Basic realm="My Realm"'); header('WWW-Authenticate: Basic realm="My Realm"');
@@ -163,10 +160,7 @@ if (!isset($_SERVER['PHP_AUTH_USER'])) {
if(isset($_GET['bydate'])){ if(isset($_GET['bydate'])){
$statement = $db->prepare('SELECT DISTINCT(Date) FROM detections GROUP BY Date ORDER BY Date DESC'); $statement = $db->prepare('SELECT DISTINCT(Date) FROM detections GROUP BY Date ORDER BY Date DESC');
if($statement == False){ ensure_db_ok($statement);
echo "Database is busy";
header("refresh: 0;");
}
$result = $statement->execute(); $result = $statement->execute();
$view = "bydate"; $view = "bydate";
@@ -180,10 +174,7 @@ if(isset($_GET['bydate'])){
} else { } else {
$statement = $db->prepare("SELECT DISTINCT(Com_Name) FROM detections WHERE Date == \"$date\" ORDER BY Com_Name"); $statement = $db->prepare("SELECT DISTINCT(Com_Name) FROM detections WHERE Date == \"$date\" ORDER BY Com_Name");
} }
if($statement == False){ ensure_db_ok($statement);
echo "Database is busy";
header("refresh: 0;");
}
$result = $statement->execute(); $result = $statement->execute();
$view = "date"; $view = "date";
@@ -195,10 +186,7 @@ if(isset($_GET['bydate'])){
$statement = $db->prepare('SELECT DISTINCT(Com_Name) FROM detections ORDER BY Com_Name ASC'); $statement = $db->prepare('SELECT DISTINCT(Com_Name) FROM detections ORDER BY Com_Name ASC');
} }
session_start(); session_start();
if($statement == False){ ensure_db_ok($statement);
echo "Database is busy";
header("refresh: 0;");
}
$result = $statement->execute(); $result = $statement->execute();
$view = "byspecies"; $view = "byspecies";
@@ -208,17 +196,15 @@ if(isset($_GET['bydate'])){
session_start(); session_start();
$_SESSION['species'] = $species; $_SESSION['species'] = $species;
$statement = $db->prepare("SELECT * FROM detections WHERE Com_Name == \"$species\" ORDER BY Com_Name"); $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"); $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){ ensure_db_ok($statement3);
echo "Database is busy";
header("refresh: 0;");
}
$result = $statement->execute(); $result = $statement->execute();
$result3 = $statement3->execute(); $result3 = $statement3->execute();
$view = "species"; $view = "species";
} else { } else {
session_start(); unset($_SESSION['species']);
session_unset(); unset($_SESSION['date']);
$view = "choose"; $view = "choose";
} }
?> ?>
@@ -484,10 +470,7 @@ if(isset($_SESSION['date'])) {
$statement2 = $db->prepare("SELECT * FROM detections where Com_Name == \"$name\" ORDER BY Date DESC, Time DESC"); $statement2 = $db->prepare("SELECT * FROM detections where Com_Name == \"$name\" ORDER BY Date DESC, Time DESC");
} }
} }
if($statement2 == False){ ensure_db_ok($statement2);
echo "Database is busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
$num_rows = 0; $num_rows = 0;
while ($result2->fetchArray(SQLITE3_ASSOC)) { while ($result2->fetchArray(SQLITE3_ASSOC)) {
@@ -574,10 +557,7 @@ echo "<table>
if(isset($_GET['filename'])){ if(isset($_GET['filename'])){
$name = $_GET['filename']; $name = $_GET['filename'];
$statement2 = $db->prepare("SELECT * FROM detections where File_name == \"$name\" ORDER BY Date DESC, Time DESC"); $statement2 = $db->prepare("SELECT * FROM detections where File_name == \"$name\" ORDER BY Date DESC, Time DESC");
if($statement2 == False){ ensure_db_ok($statement2);
echo "Database is busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
echo "<table> echo "<table>
<tr> <tr>
+8 -25
View File
@@ -7,42 +7,28 @@ $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
ini_set('user_agent', 'PHP_Flickr/1.0'); ini_set('user_agent', 'PHP_Flickr/1.0');
error_reporting(0); error_reporting(0);
ini_set('display_errors', 0); ini_set('display_errors', 0);
require_once 'scripts/common.php';
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
if($db == False) { $db->busyTimeout(1000);
echo "Database busy";
header("refresh: 0;");
}
if(isset($_GET['sort']) && $_GET['sort'] == "occurrences") { 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'); $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) { ensure_db_ok($statement);
echo "Database busy";
header("refresh: 0;");
}
$result = $statement->execute(); $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'); $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) { ensure_db_ok($statement2);
echo "Database busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
} else { } 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'); $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) { ensure_db_ok($statement);
echo "Database busy";
header("refresh: 0;");
}
$result = $statement->execute(); $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'); $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) { ensure_db_ok($statement2);
echo "Database busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
} }
@@ -51,10 +37,7 @@ if(isset($_GET['sort']) && $_GET['sort'] == "occurrences") {
if(isset($_GET['species'])){ if(isset($_GET['species'])){
$selection = $_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\""); $statement3 = $db->prepare("SELECT Com_Name, Sci_Name, COUNT(*), MAX(Confidence), File_Name, Date, Time from detections WHERE Com_Name = \"$selection\"");
if($statement3 == False) { ensure_db_ok($statement3);
echo "Database busy";
header("refresh: 0;");
}
$result3 = $statement3->execute(); $result3 = $statement3->execute();
} }
+6 -3
View File
@@ -8,14 +8,17 @@ $fetch = shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi fetch 2>&1");
$str = trim(shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi status")); $str = trim(shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi status"));
if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) { if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) {
$num_commits_behind = $matches[1]; $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)) { if (preg_match('/\b(\d+)\b and \b(\d+)\b different commits each/', $str, $matches)) {
$num1 = (int) $matches[1]; $num1 = (int) $matches[1];
$num2 = (int) $matches[2]; $num2 = (int) $matches[2];
$sum = $num1 + $num2; $num_commits_behind = $num1 + $num2;
$_SESSION['behind'] = $sum;
} }
if (stripos($str, "Your branch is up to date") !== false) {
$num_commits_behind = '0';
}
$_SESSION['behind'] = $num_commits_behind;
$_SESSION['behind_time'] = time();
?><html> ?><html>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<br> <br>
+10 -33
View File
@@ -10,6 +10,7 @@ session_set_cookie_params(7200);
session_start(); session_start();
error_reporting(E_ERROR); error_reporting(E_ERROR);
ini_set('display_errors',1); ini_set('display_errors',1);
require_once 'scripts/common.php';
if (file_exists('./scripts/thisrun.txt')) { if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./scripts/thisrun.txt'); $config = parse_ini_file('./scripts/thisrun.txt');
@@ -28,57 +29,36 @@ if (file_exists('./scripts/thisrun.txt')) {
</div><div class=\"centered\"><h3>$site_name</h3></div><hr>"; </div><div class=\"centered\"><h3>$site_name</h3></div><hr>";
} }
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
if($db == False){ $db->busyTimeout(1000);
echo "Database is busy";
header("refresh: 0;");
}
$statement1 = $db->prepare('SELECT COUNT(*) FROM detections'); $statement1 = $db->prepare('SELECT COUNT(*) FROM detections');
if($statement1 == False){ ensure_db_ok($statement1);
echo "Database is busy";
header("refresh: 0;");
}
$result1 = $statement1->execute(); $result1 = $statement1->execute();
$totalcount = $result1->fetchArray(SQLITE3_ASSOC); $totalcount = $result1->fetchArray(SQLITE3_ASSOC);
$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == DATE(\'now\', \'localtime\')'); $statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == DATE(\'now\', \'localtime\')');
if($statement2 == False){ ensure_db_ok($statement2);
echo "Database is busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
$todaycount = $result2->fetchArray(SQLITE3_ASSOC); $todaycount = $result2->fetchArray(SQLITE3_ASSOC);
$statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == Date(\'now\', \'localtime\') AND TIME >= TIME(\'now\', \'localtime\', \'-1 hour\')'); $statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Date == Date(\'now\', \'localtime\') AND TIME >= TIME(\'now\', \'localtime\', \'-1 hour\')');
if($statement3 == False){ ensure_db_ok($statement3);
echo "Database is busy";
header("refresh: 0;");
}
$result3 = $statement3->execute(); $result3 = $statement3->execute();
$hourcount = $result3->fetchArray(SQLITE3_ASSOC); $hourcount = $result3->fetchArray(SQLITE3_ASSOC);
$statement4 = $db->prepare('SELECT Com_Name, Sci_Name, Time, Confidence FROM detections LIMIT 1'); $statement4 = $db->prepare('SELECT Com_Name, Sci_Name, Time, Confidence FROM detections LIMIT 1');
if($statement4 == False){ ensure_db_ok($statement4);
echo "Database is busy";
header("refresh: 0;");
}
$result4 = $statement4->execute(); $result4 = $statement4->execute();
$mostrecent = $result4->fetchArray(SQLITE3_ASSOC); $mostrecent = $result4->fetchArray(SQLITE3_ASSOC);
$statement5 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date == Date(\'now\', \'localtime\')'); $statement5 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date == Date(\'now\', \'localtime\')');
if($statement5 == False){ ensure_db_ok($statement5);
echo "Database is busy";
header("refresh: 0;");
}
$result5 = $statement5->execute(); $result5 = $statement5->execute();
$todayspeciestally = $result5->fetchArray(SQLITE3_ASSOC); $todayspeciestally = $result5->fetchArray(SQLITE3_ASSOC);
$statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections'); $statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections');
if($statement6 == False){ ensure_db_ok($statement6);
echo "Database is busy";
header("refresh: 0;");
}
$result6 = $statement6->execute(); $result6 = $statement6->execute();
$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC); $totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC);
@@ -205,10 +185,7 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) {
} }
} }
if($statement0 == False){ ensure_db_ok($statement0);
echo "Database is busy";
header("refresh: 0;");
}
$result0 = $statement0->execute(); $result0 = $statement0->execute();
?> <table> ?> <table>
+15 -51
View File
@@ -2,6 +2,7 @@
ini_set('display_errors', 1); ini_set('display_errors', 1);
ini_set('display_startup_errors', 1); ini_set('display_startup_errors', 1);
error_reporting(E_ALL); error_reporting(E_ALL);
require_once 'scripts/common.php';
$startdate = strtotime('last sunday') - (7*86400); $startdate = strtotime('last sunday') - (7*86400);
$enddate = strtotime('last sunday') - (1*86400); $enddate = strtotime('last sunday') - (1*86400);
@@ -10,48 +11,30 @@ $debug = false;
if(isset($_GET['ascii'])) { if(isset($_GET['ascii'])) {
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
if($db == False){ $db->busyTimeout(1000);
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'); $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){ ensure_db_ok($statement1);
echo "Database is busy";
header("refresh: 0;");
}
$result1 = $statement1->execute(); $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).'"'); $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){ ensure_db_ok($statement4);
echo "Database is busy";
header("refresh: 0;");
}
$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 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){ ensure_db_ok($statement5);
echo "Database is busy";
header("refresh: 0;");
}
$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(Com_Name)) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
if($statement6 == False){ ensure_db_ok($statement6);
echo "Database is busy";
header("refresh: 0;");
}
$result6 = $statement6->execute(); $result6 = $statement6->execute();
$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))']; $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)).'"'); $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){ ensure_db_ok($statement7);
echo "Database is busy";
header("refresh: 0;");
}
$result7= $statement7->execute(); $result7= $statement7->execute();
$priortotalspeciestally = $result7->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))']; $priortotalspeciestally = $result7->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))'];
@@ -91,10 +74,7 @@ if(isset($_GET['ascii'])) {
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)).'"'); $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){ ensure_db_ok($statement2);
echo "Database is busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
$totalcount = $result2->fetchArray(SQLITE3_ASSOC); $totalcount = $result2->fetchArray(SQLITE3_ASSOC);
$priorweekcount = $totalcount['COUNT(*)']; $priorweekcount = $totalcount['COUNT(*)'];
@@ -122,10 +102,7 @@ if(isset($_GET['ascii'])) {
foreach($detections as $com_name=>$scount) 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).'"'); $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){ ensure_db_ok($statement3);
echo "Database is busy";
header("refresh: 0;");
}
$result3 = $statement3->execute(); $result3 = $statement3->execute();
$totalcount = $result3->fetchArray(SQLITE3_ASSOC); $totalcount = $result3->fetchArray(SQLITE3_ASSOC);
$nonthisweekcount = $totalcount['COUNT(*)']; $nonthisweekcount = $totalcount['COUNT(*)'];
@@ -153,21 +130,14 @@ if(isset($_GET['ascii'])) {
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 ?></div><?php
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); $db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
if($db == False){ $db->busyTimeout(1000);
echo "Database is busy";
header("refresh: 0;");
}
if($debug == false){ 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'); $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 { } 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'); $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){ ensure_db_ok($statement1);
echo "Database is busy";
header("refresh: 0;");
}
$result1 = $statement1->execute(); $result1 = $statement1->execute();
$detections = []; $detections = [];
@@ -202,10 +172,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC))
$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)).'"'); $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){ ensure_db_ok($statement2);
echo "Database is busy";
header("refresh: 0;");
}
$result2 = $statement2->execute(); $result2 = $statement2->execute();
$totalcount = $result2->fetchArray(SQLITE3_ASSOC); $totalcount = $result2->fetchArray(SQLITE3_ASSOC);
$priorweekcount = $totalcount['COUNT(*)']; $priorweekcount = $totalcount['COUNT(*)'];
@@ -243,10 +210,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC))
foreach($detections as $com_name=>$scount) 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).'"'); $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){ ensure_db_ok($statement3);
echo "Database is busy";
header("refresh: 0;");
}
$result3 = $statement3->execute(); $result3 = $statement3->execute();
$totalcount = $result3->fetchArray(SQLITE3_ASSOC); $totalcount = $result3->fetchArray(SQLITE3_ASSOC);
$nonthisweekcount = $totalcount['COUNT(*)']; $nonthisweekcount = $totalcount['COUNT(*)'];