From d96127c93e343767c94a6a7508601cd7d2afae0c Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Mon, 12 Feb 2024 22:54:49 +1100 Subject: [PATCH] My Devices filter #548 --- dockerfiles/start.sh | 2 +- front/devices.php | 106 +++++++++++++++++------- front/php/server/devices.php | 2 +- front/php/templates/language/en_us.json | 6 +- pialert/conf.py | 3 +- pialert/initialise.py | 1 + 6 files changed, 85 insertions(+), 35 deletions(-) diff --git a/dockerfiles/start.sh b/dockerfiles/start.sh index 6757ecf4..3a920640 100755 --- a/dockerfiles/start.sh +++ b/dockerfiles/start.sh @@ -148,7 +148,7 @@ fi # Activate the virtual python environment source myenv/bin/activate -echo "[INSTALL] 🚀 Starting app - navigate to your :$LISTEN_ADDR " +echo "[INSTALL] 🚀 Starting app - navigate to your :$LISTEN_ADDR" # Start the PiAlert python script python $INSTALL_DIR/pialert/pialert/ diff --git a/front/devices.php b/front/devices.php index fb0364ba..370b11d9 100755 --- a/front/devices.php +++ b/front/devices.php @@ -42,9 +42,9 @@
- +
-

--

+

--

@@ -291,10 +291,9 @@ function main () { } // Initialize components with parameters - initializeDatatable(); + initializeDatatable('my'); + - // query data - getDevicesTotals(); // check if dat outdated and show spinner if so handleLoadingDialog() @@ -319,13 +318,77 @@ function mapIndx(oldIndex) } } +//------------------------------------------------------------------------------ +// Query total numbers of Devices by status +//------------------------------------------------------------------------------ +function getDevicesTotals(devicesData) { + + let resultJSON = ""; + + if (getCache("getDevicesTotals") !== "") { + resultJSON = getCache("getDevicesTotals"); + } else { + // combined query + const devices = filterDataByStatus(devicesData, 'my'); + const connectedDevices = filterDataByStatus(devicesData, 'connected'); + const favoritesDevices = filterDataByStatus(devicesData, 'favorites'); + const newDevices = filterDataByStatus(devicesData, 'new'); + const downDevices = filterDataByStatus(devicesData, 'down'); + const archivedDevices = filterDataByStatus(devicesData, 'archived'); + + + $('#devicesMy').html (devices.length); + $('#devicesConnected').html (connectedDevices.length); + $('#devicesFavorites').html (favoritesDevices.length); + $('#devicesNew').html (newDevices.length); + $('#devicesDown').html (downDevices.length); + $('#devicesArchived').html (archivedDevices.length); + + // save to cache + setCache("getDevicesTotals", resultJSON); + } + + console.log(resultJSON); +} + // ----------------------------------------------------------------------------- // Define a function to filter data based on deviceStatus function filterDataByStatus(data, status) { return data.filter(function(item) { switch (status) { - case 'all': - return true; // Include all items for 'all' status + case 'my': + to_display = getSetting('UI_MY_DEVICES') + + console.log(to_display) + + result = false; + + if (to_display.includes('online') && item.dev_PresentLastScan === 1) + { + result = true; + } + + if (to_display.includes('offline') && item.dev_PresentLastScan === 0) + { + result = true; + } + + if (to_display.includes('archived') && item.dev_Archived === 1) + { + result = true; + } + + if (to_display.includes('new') && item.dev_NewDevice === 1) + { + result = true; + } + + if (to_display.includes('down') && item.dev_PresentLastScan === 0 && item.dev_AlertDeviceDown !== 0) + { + result = true; + } + + return result; // Include all items for 'my' status case 'connected': return item.dev_PresentLastScan === 1; case 'favorites': @@ -378,7 +441,7 @@ function initializeDatatable (status) { // Define color & title for the status selected switch (deviceStatus) { - case 'all': tableTitle = getString('Device_Shortcut_AllDevices'); color = 'aqua'; break; + case 'my': tableTitle = getString('Device_Shortcut_AllDevices'); color = 'aqua'; break; case 'connected': tableTitle = getString('Device_Shortcut_Connected'); color = 'green'; break; case 'favorites': tableTitle = getString('Device_Shortcut_Favorites'); color = 'yellow'; break; case 'new': tableTitle = getString('Device_Shortcut_NewDevices'); color = 'yellow'; break; @@ -402,7 +465,10 @@ function initializeDatatable (status) { } } - $.get('api/table_devices.json?nocache=' + Date.now(), function(result) { + $.get('api/table_devices.json?nocache=' + Date.now(), function(result) { + + // query data + getDevicesTotals(result.data); // Filter the data based on deviceStatus var filteredData = filterDataByStatus(result.data, deviceStatus); @@ -550,7 +616,7 @@ function initializeDatatable (status) { // Random MAC {targets: [mapIndx(9)], 'createdCell': function (td, cellData, rowData, row, col) { - console.log(cellData) + // console.log(cellData) if (cellData == 1){ $(td).html (''); } else { @@ -638,26 +704,6 @@ function getDevicesFromTable(table) return JSON.stringify (result) } -// ----------------------------------------------------------------------------- -function getDevicesTotals () { - // stop timer - stopTimerRefreshData(); - - // get totals and put in boxes - $.get('php/server/devices.php?action=getDevicesTotals', function(data) { - var totalsDevices = JSON.parse(data); - - $('#devicesAll').html (totalsDevices[0].toLocaleString()); - $('#devicesConnected').html (totalsDevices[1].toLocaleString()); - $('#devicesFavorites').html (totalsDevices[2].toLocaleString()); - $('#devicesNew').html (totalsDevices[3].toLocaleString()); - $('#devicesDown').html (totalsDevices[4].toLocaleString()); - $('#devicesArchived').html (totalsDevices[5].toLocaleString()); - - // Timer for refresh data - newTimerRefreshData (getDevicesTotals); - } ); -} // ----------------------------------------------------------------------------- function getNumberOfChildren(mac, devices) diff --git a/front/php/server/devices.php b/front/php/server/devices.php index a5f617c0..c9674e17 100755 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -541,7 +541,7 @@ function getDevicesTotals() { } else { global $db; - +f // combined query $result = $db->query( 'SELECT diff --git a/front/php/templates/language/en_us.json b/front/php/templates/language/en_us.json index 94dbdeed..5bf94eb8 100755 --- a/front/php/templates/language/en_us.json +++ b/front/php/templates/language/en_us.json @@ -175,7 +175,7 @@ "DevDetail_button_Reset": "Reset Changes", "DevDetail_button_Save": "Save", "Device_Searchbox": "Search", - "Device_Shortcut_AllDevices": "All Devices", + "Device_Shortcut_AllDevices": "My Devices", "Device_Shortcut_Archived": "Archived", "Device_Shortcut_Connected": "Connected", "Device_Shortcut_Devices": "Devices", @@ -498,7 +498,7 @@ "Presence_CalHead_year": "year", "Presence_CallHead_Devices": "Devices", "Presence_Loading": "Loading...", - "Presence_Shortcut_AllDevices": "All Devices", + "Presence_Shortcut_AllDevices": "My Devices", "Presence_Shortcut_Archived": "Archived", "Presence_Shortcut_Connected": "Connected", "Presence_Shortcut_Devices": "Devices", @@ -597,6 +597,8 @@ "UI_LANG_name": "UI Language", "UI_PRESENCE_description": "Select what statuses should be shown in the Device presence chart in the
Devices page. (CTRL + Click to select/deselect)", "UI_PRESENCE_name": "Show in presence chart", + "UI_MY_DEVICES_description": "Devices of which statuses should be shown in the default My Devices view. (CTRL + Click to select/deselect)", + "UI_MY_DEVICES_name": "Show in My Devices view", "devices_old": "Refreshing...", "general_event_description": "The event you have triggered might take a while until background processes finish. The execution ended once the below execution queue empties (Check the error log if you encounter issues).

Execution queue:", "general_event_title": "Executing an ad-hoc event", diff --git a/pialert/conf.py b/pialert/conf.py index cebe8fc3..19fdedb7 100755 --- a/pialert/conf.py +++ b/pialert/conf.py @@ -33,7 +33,8 @@ SCAN_SUBNETS = ['192.168.1.0/24 --interface=eth1', '192.168.1.0/24 --interfac LOG_LEVEL = 'verbose' TIMEZONE = 'Europe/Berlin' UI_LANG = 'English' -UI_PRESENCE = ['online', 'offline', 'archived'] +UI_PRESENCE = ['online', 'offline', 'archived'] +UI_MY_DEVICES = ['online', 'offline', 'archived', 'new', 'down'] PIALERT_WEB_PROTECTION = False PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92' DAYS_TO_KEEP_EVENTS = 90 diff --git a/pialert/initialise.py b/pialert/initialise.py index e0bde510..57d78534 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -107,6 +107,7 @@ def importConfigs (db): conf.REPORT_DASHBOARD_URL = ccd('REPORT_DASHBOARD_URL', 'http://pi.alert/' , c_d, 'PiAlert URL', 'text', '', 'General') conf.UI_LANG = ccd('UI_LANG', 'English' , c_d, 'Language Interface', 'text.select', "['English', 'German', 'Spanish']", 'General') conf.UI_PRESENCE = ccd('UI_PRESENCE', ['online', 'offline', 'archived'] , c_d, 'Include in presence', 'text.multiselect', "['online', 'offline', 'archived']", 'General') + conf.UI_MY_DEVICES = ccd('UI_MY_DEVICES', ['online', 'offline', 'archived', 'new', 'down'] , c_d, 'Include in My Devices', 'text.multiselect', "['online', 'offline', 'archived', 'new', 'down']", 'General') conf.DAYS_TO_KEEP_EVENTS = ccd('DAYS_TO_KEEP_EVENTS', 90 , c_d, 'Delete events days', 'integer', '', 'General') conf.HRS_TO_KEEP_NEWDEV = ccd('HRS_TO_KEEP_NEWDEV', 0 , c_d, 'Keep new devices for', 'integer', "0", 'General') conf.API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE dev_PresentLastScan = 0' , c_d, 'Custom endpoint', 'text', '', 'General')