optimisations

This commit is contained in:
jokob-sk
2022-08-04 00:17:47 +10:00
parent c9bc27531a
commit 86ffe8ba36
4 changed files with 35 additions and 29 deletions

View File

@@ -0,0 +1,7 @@
<?php
// Cache the contents to a cache file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?>

View File

@@ -0,0 +1,15 @@
<?php
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
$cachetime = 18000;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
readfile($cachefile);
exit;
}
ob_start(); // Start the output buffer
?>

View File

@@ -480,37 +480,20 @@ function PiaToggleArpScan() {
function getDevicesTotals() {
global $db;
// All
$result = $db->query('SELECT COUNT(*) FROM Devices '. getDeviceCondition ('all'));
$row = $result -> fetchArray (SQLITE3_NUM);
$devices = $row[0];
// On-Line
$result = $db->query('SELECT COUNT(*) FROM Devices '. getDeviceCondition ('connected') );
$row = $result -> fetchArray (SQLITE3_NUM);
$connected = $row[0];
// Favorites
$result = $db->query('SELECT COUNT(*) FROM Devices '. getDeviceCondition ('favorites') );
$row = $result -> fetchArray (SQLITE3_NUM);
$favorites = $row[0];
// New
$result = $db->query('SELECT COUNT(*) FROM Devices '. getDeviceCondition ('new') );
$row = $result -> fetchArray (SQLITE3_NUM);
$newDevices = $row[0];
// Down Alerts
$result = $db->query('SELECT COUNT(*) FROM Devices '. getDeviceCondition ('down'));
$row = $result -> fetchArray (SQLITE3_NUM);
$downAlert = $row[0];
// combined query
$result = $db->query(
'SELECT
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('all').') as devices,
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('connected').') as connected,
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('favorites').') as favorites,
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('new').') as new,
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('down').') as down,
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('archived').') as archived
');
// Archived
$result = $db->query('SELECT COUNT(*) FROM Devices '. getDeviceCondition ('archived'));
$row = $result -> fetchArray (SQLITE3_NUM);
$archived = $row[0];
$row = $result -> fetchArray (SQLITE3_NUM);
echo (json_encode (array ($devices, $connected, $favorites, $newDevices, $downAlert, $archived)));
echo (json_encode (array ($row[0], $row[1], $row[2], $row[3], $row[4], $row[5])));
}

View File

@@ -29,4 +29,5 @@ function pia_graph_devices_data($Pia_Graph_Array) {
}
}
$db->close();
?>