From 33fbbb8be25566efdf0aa65b1fcb360e558dc740 Mon Sep 17 00:00:00 2001 From: ehpersonal38 <103586016+ehpersonal38@users.noreply.github.com> Date: Tue, 19 Apr 2022 11:27:47 -0400 Subject: [PATCH] DB optimizations for overview.php --- scripts/overview.php | 109 +++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 56 deletions(-) diff --git a/scripts/overview.php b/scripts/overview.php index 0a71954..4eba52d 100644 --- a/scripts/overview.php +++ b/scripts/overview.php @@ -8,60 +8,21 @@ if($db == False) { header("refresh: 0;"); } -$statement = $db->prepare('SELECT COUNT(*) FROM detections'); -if($statement == False) { - echo "Database is busy"; - header("refresh: 0;"); -} -$result = $statement->execute(); -$totalcount = $result->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;"); -} -$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;"); -} -$result3 = $statement3->execute(); -$hourcount = $result3->fetchArray(SQLITE3_ASSOC); - -$statement4 = $db->prepare('SELECT Com_Name, Sci_Name, Date, Time, Confidence, File_Name FROM detections ORDER BY Date DESC, Time DESC LIMIT 1'); -if($statement4 == False) { - echo "Database is busy"; - header("refresh: 0;"); -} -$result4 = $statement4->execute(); -$mostrecent = $result4->fetchArray(SQLITE3_ASSOC); -$comname = preg_replace('/ /', '_', $mostrecent['Com_Name']); -$sciname = preg_replace('/ /', '_', $mostrecent['Sci_Name']); -$comname = preg_replace('/\'/', '', $comname); -$filename = "/By_Date/".$mostrecent['Date']."/".$comname."/".$mostrecent['File_Name']; - -$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;"); -} -$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;"); -} -$result6 = $statement6->execute(); -$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC); - - 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 1'); + if($statement4 == False) { + echo "Database is busy"; + header("refresh: 0;"); + } + $result4 = $statement4->execute(); + $mostrecent = $result4->fetchArray(SQLITE3_ASSOC); + $comname = preg_replace('/ /', '_', $mostrecent['Com_Name']); + $sciname = preg_replace('/ /', '_', $mostrecent['Sci_Name']); + $comname = preg_replace('/\'/', '', $comname); + $filename = "/By_Date/".$mostrecent['Date']."/".$comname."/".$mostrecent['File_Name']; + + if($_GET['previous_detection_identifier'] != $filename || $_GET['previous_detection_identifier'] == "undefined") { // check to make sure the image actually exists, sometimes it takes a minute to be created if (isset($_SERVER['HTTPS']) && @@ -112,22 +73,58 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" && isse die(); } +$totalcount = 0; +$todaycount = 0; +$hourcount = 0; +$statement = $db->prepare('SELECT * FROM detections ORDER BY Date DESC, Time DESC'); +if($statement == False) { + echo "Database is busy"; + header("refresh: 0;"); +} +$result = $statement->execute(); +while($detection=$result->fetchArray(SQLITE3_ASSOC)) +{ + $totalcount++; + if(strtotime($detection["Date"]." ".$detection["Time"]) > (time() - 3600)){ + $hourcount++; + } if(strtotime($detection["Date"]." ".$detection["Time"]) > (time() - (time() - strtotime("today"))) ){ + $todaycount++; + } +} + +$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;"); +} +$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;"); +} +$result6 = $statement6->execute(); +$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC); + + if(isset($_GET['ajax_left_chart']) && $_GET['ajax_left_chart'] == "true") { ?>
| Total | -+ | |
|---|---|---|
| Today | -+ | |
| Last Hour | -+ | |
| Species Detected Today |