GraphQL 0.11 - Load devices json on Query, _KEEP_ bugfix for old settings

This commit is contained in:
jokob-sk
2024-11-11 12:01:09 +11:00
parent bf9f55355e
commit 60777b2f82
6 changed files with 25 additions and 59 deletions

View File

@@ -378,6 +378,9 @@ function getDeviceStatus(item)
// -----------------------------------------------------------------------------
function initializeDatatable_new (status) {
console.log(tableColumnVisible);
// Build GraphQL query dynamically based on tableColumnVisible
let columnsToFetch = [
'devMac', 'devName', 'devLastConnection', 'devIsArchived', 'devOwner', 'devType',
@@ -481,7 +484,7 @@ $.ajax({
} else {
$(td).html('');
}
}},
}}
// Other columns (Status, MAC, Date, etc.) can be similarly customized.
],

View File

@@ -464,10 +464,10 @@ $settingsJSON_DB = json_encode($settings, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX
// INPUT
// console.log(codeName);
// Parse the setType JSON string into an object
let inputHtml = '';
console.log(codeName);
console.log(setType);
const setTypeObject = JSON.parse(setType.replace(/'/g, '"'));

View File

@@ -79,11 +79,6 @@ def main ():
# This is the main loop of NetAlertX
#===============================================================================
mylog('debug', '[MAIN] Starting GraphQL server')
# Path to your `graphql_server.py` file
flask_app_path = applicationPath + '/server/graphql_server.py'
mylog('debug', '[MAIN] Starting loop')
# Header + init app state

View File

@@ -14,12 +14,6 @@ from const import apiPath
# Define a base URL with the user's home directory
folder = apiPath
# Load your JSON data
with open(folder + 'table_devices.json', 'r') as f:
devices_data = json.load(f)["data"]
# mylog('none', [f'[graphql_schema] devices_data {devices_data}'])
# Device ObjectType
class Device(ObjectType):
rowid = Int()
@@ -55,49 +49,20 @@ class Device(ObjectType):
devSourcePlugin = String() # This should match devSourcePlugin, not devSourcePlugin
# Query ObjectType
class Query(ObjectType):
devices = List(Device)
def resolve_devices(self, info):
# Map the data to match the GraphQL schema's camelCase
mapped_devices = []
for device in devices_data:
mapped_device = {
'rowid': device['rowid'],
'devMac': device['devMac'], # Mapping from snake_case to camelCase
'devName': device['devName'],
'devOwner': device['devOwner'],
'devType': device['devType'],
'devVendor': device['devVendor'],
'devFavorite': device['devFavorite'],
'devGroup': device['devGroup'],
'devComments': device['devComments'],
'devFirstConnection': device['devFirstConnection'],
'devLastConnection': device['devLastConnection'],
'devLastIP': device['devLastIP'],
'devStaticIP': device['devStaticIP'],
'devScan': device['devScan'],
'devLogEvents': device['devLogEvents'],
'devAlertEvents': device['devAlertEvents'],
'devAlertDown': device['devAlertDown'],
'devSkipRepeated': device['devSkipRepeated'],
'devLastNotification': device['devLastNotification'],
'devPresentLastScan': device['devPresentLastScan'],
'devIsNew': device['devIsNew'],
'devLocation': device['devLocation'],
'devIsArchived': device['devIsArchived'],
'devParentMAC': device['devParentMAC'],
'devParentPort': device['devParentPort'],
'devIcon': device['devIcon'],
'devGUID': device['devGUID'],
'devSite': device['devSite'],
'devSSID': device['devSSID'],
'devSyncHubNode': device['devSyncHubNode'],
'devSourcePlugin': device['devSourcePlugin']
}
mapped_devices.append(mapped_device)
return mapped_devices
# Load JSON data only when the query executes
try:
with open(folder + 'table_devices.json', 'r') as f:
devices_data = json.load(f)["data"]
except (FileNotFoundError, json.JSONDecodeError) as e:
mylog('error', f'[graphql_schema] Error loading devices data: {e}')
return []
return devices_data # Directly return the data without mapping
# Schema Definition

View File

@@ -39,7 +39,7 @@ def graphql_endpoint():
def start_server():
"""Function to start the GraphQL server in a background thread."""
mylog('none', [f'[graphql_server] Started'])
mylog('none', [f'[graphql_server] Starting on port "{GRAPHQL_PORT}"'])
# Start the Flask app in a separate thread
thread = threading.Thread(target=lambda: app.run(host="0.0.0.0", port=GRAPHQL_PORT, debug=True, use_reloader=False))

View File

@@ -75,7 +75,7 @@ def update_or_append(settings_list, item_tuple, key):
for index, item in enumerate(settings_list):
if item[0] == key:
mylog('trace', ['[Import Config] OLD TUPLE : ', item])
# Keep values marked as "_KEEP_"
# Keep values marked as "_KEEP_" in existing entries
updated_tuple = tuple(
new_val if new_val != "_KEEP_" else old_val
for old_val, new_val in zip(item, item_tuple)
@@ -85,12 +85,15 @@ def update_or_append(settings_list, item_tuple, key):
mylog('trace', ['[Import Config] FOUND key : ', key])
return settings_list
# Append the item only if no values are "_KEEP_"
if "_KEEP_" not in item_tuple:
settings_list.append(item_tuple)
mylog('trace', ['[Import Config] ADDED key : ', key])
else:
mylog('none', ['[Import Config] Skipped saving _KEEP_ for key : ', key])
return settings_list
#-------------------------------------------------------------------------------
def importConfigs (db, all_plugins):