From 8650c68801db97a383b30c84123eff675bf927e9 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Fri, 5 Aug 2022 16:52:59 +1000 Subject: [PATCH] Optimisation no 5: cache events totals for 5min + sql optimization --- front/php/server/devices.php | 16 ------- front/php/server/events.php | 82 +++++++++++++++++++----------------- front/php/server/util.php | 20 +++++++++ 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/front/php/server/devices.php b/front/php/server/devices.php index a45f587e..4a790e82 100644 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -811,21 +811,5 @@ function getDeviceCondition ($deviceStatus) { } } -//------------------------------------------------------------------------------ -// Simple cookie cache -//------------------------------------------------------------------------------ -function getCache($key) { - if( isset($_COOKIE[$key])) - { - return $_COOKIE[$key]; - }else - { - return ""; - } -} - -function setCache($key, $value) { - setcookie($key, $value, time()+300, "/","", 0); // 5min cache -} ?> diff --git a/front/php/server/events.php b/front/php/server/events.php index 28b714e7..b5450543 100644 --- a/front/php/server/events.php +++ b/front/php/server/events.php @@ -52,51 +52,57 @@ function getEventsTotals() { global $db; // Request Parameters - $periodDate = getDateFromPeriod(); + $periodDate = $_REQUEST['period']; - // SQL - $SQL1 = 'SELECT Count(*) - FROM Events - WHERE eve_DateTime >= '. $periodDate; - - $SQL2 = 'SELECT Count(*) - FROM Sessions '; + $periodDateSQL = ""; + $days = ""; - // All - $result = $db->query($SQL1); - $row = $result -> fetchArray (SQLITE3_NUM); - $eventsAll = $row[0]; + switch ($periodDate) { + case '7 days': + $days = "7"; + break; + case '1 month': + $days = "30"; + break; + case '1 year': + $days = "365"; + break; + case '100 years': + $days = "3650"; //10 years + break; + default: + $days = "1"; + } - // Sessions - $result = $db->query($SQL2. ' WHERE ( ses_DateTimeConnection >= '. $periodDate .' - OR ses_DateTimeDisconnection >= '. $periodDate .' - OR ses_StillConnected = 1 ) '); - $row = $result -> fetchArray (SQLITE3_NUM); - $eventsSessions = $row[0]; + $periodDateSQL = "-".$days." day"; - // Missing - $result = $db->query($SQL2. ' WHERE (ses_DateTimeConnection IS NULL AND ses_DateTimeDisconnection >= '. $periodDate .' ) - OR (ses_DateTimeDisconnection IS NULL AND ses_StillConnected = 0 AND ses_DateTimeConnection >= '. $periodDate .' )' ); - $row = $result -> fetchArray (SQLITE3_NUM); - $eventsMissing = $row[0]; + $resultJSON = ""; - // Voided - $result = $db->query($SQL1. ' AND eve_EventType LIKE "VOIDED%" '); - $row = $result -> fetchArray (SQLITE3_NUM); - $eventsVoided = $row[0]; + // check cache if JSON available in a cookie + if(getCache("getEventsTotals".$days) != "") + { + $resultJSON = getCache("getEventsTotals".$days); + } else + { + // one query to get all numbers, whcih is quicker than multiple queries + $sql = "select + (SELECT Count(*) FROM Events WHERE eve_DateTime >= date('now', '".$periodDateSQL."')) as all_events, + (SELECT Count(*) FROM Sessions as sessions WHERE ( ses_DateTimeConnection >= date('now', '".$periodDateSQL."') OR ses_DateTimeDisconnection >= date('now', '".$periodDateSQL."') OR ses_StillConnected = 1 )) as sessions, + (SELECT Count(*) FROM Sessions WHERE ((ses_DateTimeConnection IS NULL AND ses_DateTimeDisconnection >= date('now', '".$periodDateSQL."' )) OR (ses_DateTimeDisconnection IS NULL AND ses_StillConnected = 0 AND ses_DateTimeConnection >= date('now', '".$periodDateSQL."' )))) as missing, + (SELECT Count(*) FROM Events WHERE eve_DateTime >= date('now', '".$periodDateSQL."') AND eve_EventType LIKE 'VOIDED%' ) as voided, + (SELECT Count(*) FROM Events WHERE eve_DateTime >= date('now', '".$periodDateSQL."') AND eve_EventType LIKE 'New Device' ) as new, + (SELECT Count(*) FROM Events WHERE eve_DateTime >= date('now', '".$periodDateSQL."') AND eve_EventType LIKE 'Device Down' ) as down"; - // New - $result = $db->query($SQL1. ' AND eve_EventType LIKE "New Device" '); - $row = $result -> fetchArray (SQLITE3_NUM); - $eventsNew = $row[0]; + $result = $db->query($sql); + $row = $result -> fetchArray (SQLITE3_NUM); + $resultJSON = json_encode (array ($row[0], $row[1], $row[2], $row[3], $row[4], $row[5])); - // Down - $result = $db->query($SQL1. ' AND eve_EventType LIKE "Device Down" '); - $row = $result -> fetchArray (SQLITE3_NUM); - $eventsDown = $row[0]; - - // Return json - echo (json_encode (array ($eventsAll, $eventsSessions, $eventsMissing, $eventsVoided, $eventsNew, $eventsDown))); + // save JSON result to cache + setCache("getEventsTotals".$days, $resultJSON ); + } + + // Return json + echo ($resultJSON); } diff --git a/front/php/server/util.php b/front/php/server/util.php index c87da3ae..925f86e4 100644 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -73,4 +73,24 @@ function getNetworkTypes(){ return $array; } + +//------------------------------------------------------------------------------ +// Simple cookie cache +//------------------------------------------------------------------------------ +function getCache($key) { + if( isset($_COOKIE[$key])) + { + return $_COOKIE[$key]; + }else + { + return ""; + } +} + +function setCache($key, $value) { + setcookie($key, $value, time()+300, "/","", 0); // 5min cache +} + + + ?>