Skip to content

Commit fa22523

Browse files
committed
Refactor device tiles SQL logic to use get_sql_devices_tiles function for improved maintainability Feature Request - Flapping and Sleeping nuances
Fixes #1567
1 parent 7569923 commit fa22523

File tree

3 files changed

+51
-37
lines changed

3 files changed

+51
-37
lines changed

server/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
sql_language_strings,
1919
sql_notifications_all,
2020
sql_online_history,
21-
sql_devices_tiles,
2221
sql_devices_filters,
2322
)
23+
from db.db_helper import get_sql_devices_tiles
2424
from logger import mylog
2525
from helper import write_file, get_setting_value
2626
from utils.datetime_utils import timeNowUTC
@@ -67,7 +67,7 @@ def update_api(
6767
["plugins_language_strings", sql_language_strings],
6868
["notifications", sql_notifications_all],
6969
["online_history", sql_online_history],
70-
["devices_tiles", sql_devices_tiles],
70+
["devices_tiles", get_sql_devices_tiles()],
7171
["devices_filters", sql_devices_filters],
7272
["custom_endpoint", conf.API_CUSTOM_SQL],
7373
]

server/const.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,41 +68,6 @@
6868
"""
6969

7070
sql_appevents = """select * from AppEvents order by dateTimeCreated desc"""
71-
# The below query calculates counts of devices in various categories:
72-
# (connected/online, offline, down, new, archived),
73-
# as well as a combined count for devices that match any status listed in the UI_MY_DEVICES setting
74-
sql_devices_tiles = """
75-
WITH Statuses AS (
76-
SELECT setValue
77-
FROM Settings
78-
WHERE setKey = 'UI_MY_DEVICES'
79-
),
80-
MyDevicesFilter AS (
81-
SELECT
82-
-- Build a dynamic filter for devices matching any status in UI_MY_DEVICES
83-
devPresentLastScan, devAlertDown, devIsNew, devIsArchived
84-
FROM Devices
85-
WHERE
86-
(instr((SELECT setValue FROM Statuses), 'online') > 0 AND devPresentLastScan = 1) OR
87-
(instr((SELECT setValue FROM Statuses), 'offline') > 0 AND devPresentLastScan = 0 AND devIsArchived = 0) OR
88-
(instr((SELECT setValue FROM Statuses), 'down') > 0 AND devPresentLastScan = 0 AND devAlertDown = 1) OR
89-
(instr((SELECT setValue FROM Statuses), 'new') > 0 AND devIsNew = 1) OR
90-
(instr((SELECT setValue FROM Statuses), 'archived') > 0 AND devIsArchived = 1)
91-
)
92-
SELECT
93-
-- Counts for each individual status
94-
(SELECT COUNT(*) FROM Devices WHERE devPresentLastScan = 1) AS connected,
95-
(SELECT COUNT(*) FROM Devices WHERE devPresentLastScan = 0) AS offline,
96-
(SELECT COUNT(*) FROM Devices WHERE devPresentLastScan = 0 AND devAlertDown = 1) AS down,
97-
(SELECT COUNT(*) FROM Devices WHERE devIsNew = 1) AS new,
98-
(SELECT COUNT(*) FROM Devices WHERE devIsArchived = 1) AS archived,
99-
(SELECT COUNT(*) FROM Devices WHERE devFavorite = 1) AS favorites,
100-
(SELECT COUNT(*) FROM Devices) AS "all",
101-
(SELECT COUNT(*) FROM Devices) AS "all_devices",
102-
-- My Devices count
103-
(SELECT COUNT(*) FROM MyDevicesFilter) AS my_devices
104-
FROM Statuses;
105-
"""
10671
sql_devices_filters = """
10772
SELECT DISTINCT 'devSite' AS columnName, devSite AS columnValue
10873
FROM Devices WHERE devSite NOT IN ('', 'null') AND devSite IS NOT NULL

server/db/db_helper.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,55 @@ def get_device_condition_by_status(device_status):
6666
return get_device_conditions().get(device_status, "WHERE 1=0")
6767

6868

69+
# -------------------------------------------------------------------------------
70+
def get_sql_devices_tiles():
71+
"""Build the device tiles count SQL using get_device_conditions() to avoid duplicating filter logic."""
72+
conds = get_device_conditions()
73+
74+
def f(key):
75+
"""Strip 'WHERE ' prefix for use inside SELECT subqueries."""
76+
return conds[key][len("WHERE "):]
77+
78+
# UI_MY_DEVICES setting values mapped to their device_conditions keys
79+
my_devices_setting_map = [
80+
("online", "connected"),
81+
("offline", "offline"),
82+
("down", "down"),
83+
("new", "new"),
84+
("archived", "archived"),
85+
]
86+
87+
my_devices_clauses = "\n OR ".join(
88+
f"(instr((SELECT setValue FROM Statuses), '{sk}') > 0 AND {f(ck)})"
89+
for sk, ck in my_devices_setting_map
90+
)
91+
92+
return f"""
93+
WITH Statuses AS (
94+
SELECT setValue
95+
FROM Settings
96+
WHERE setKey = 'UI_MY_DEVICES'
97+
),
98+
MyDevicesFilter AS (
99+
SELECT devMac
100+
FROM Devices
101+
WHERE
102+
{my_devices_clauses}
103+
)
104+
SELECT
105+
(SELECT COUNT(*) FROM Devices WHERE {f('connected')}) AS connected,
106+
(SELECT COUNT(*) FROM Devices WHERE {f('offline')}) AS offline,
107+
(SELECT COUNT(*) FROM Devices WHERE {f('down')}) AS down,
108+
(SELECT COUNT(*) FROM Devices WHERE {f('new')}) AS new,
109+
(SELECT COUNT(*) FROM Devices WHERE {f('archived')}) AS archived,
110+
(SELECT COUNT(*) FROM Devices WHERE {f('favorites')}) AS favorites,
111+
(SELECT COUNT(*) FROM Devices WHERE {f('all')}) AS "all",
112+
(SELECT COUNT(*) FROM Devices) AS "all_devices",
113+
(SELECT COUNT(*) FROM MyDevicesFilter) AS my_devices
114+
FROM Statuses;
115+
"""
116+
117+
69118
# -------------------------------------------------------------------------------
70119
# Creates a JSON-like dictionary from a database row
71120
def row_to_json(names, row):

0 commit comments

Comments
 (0)