Loading spinner + app_state.json, settings work

This commit is contained in:
Jokob-sk
2023-09-16 21:14:34 +10:00
parent 40c6c65ee5
commit 44856f9c04
8 changed files with 117 additions and 52 deletions

View File

@@ -37,17 +37,37 @@ def timeNow():
#-------------------------------------------------------------------------------
# A class to manage the application state and to provide a frontend accessible API point
class app_state_class:
def __init__(self, currentState):
# json file containing the state to communicate with teh frontend
def __init__(self, currentState, settingsSaved=None, settingsImported=None, showSpinner=False):
# json file containing the state to communicate with the frontend
stateFile = apiPath + '/app_state.json'
# update self
# Update self
self.currentState = currentState
self.lastUpdated = str(timeNowTZ())
# update .json file
write_file(stateFile , json.dumps(self, cls=AppStateEncoder))
# Check if the file exists and init values
if os.path.exists(stateFile):
with open(stateFile, 'r') as json_file:
previousState = json.load(json_file)
self.settingsSaved = previousState.get("settingsSaved", 0)
self.settingsImported = previousState.get("settingsImported", 0)
self.showSpinner = previousState.get("showSpinner", False)
else:
self.settingsSaved = 0
self.settingsImported = 0
self.showSpinner = False
# Overwrite with provided parameters if not None
if settingsSaved is not None:
self.settingsSaved = settingsSaved
if settingsImported is not None:
self.settingsImported = settingsImported
if showSpinner is not None:
self.showSpinner = showSpinner
# Update .json file
with open(stateFile, 'w') as json_file:
json.dump(self, json_file, cls=AppStateEncoder, indent=4)
def isSet(self):
@@ -69,9 +89,9 @@ class AppStateEncoder(json.JSONEncoder):
return super().default(obj)
#-------------------------------------------------------------------------------
def updateState(newState):
def updateState(newState, settingsSaved = None, settingsImported = None, showSpinner = False):
state = app_state_class(newState)
state = app_state_class(newState, settingsSaved, settingsImported, showSpinner)
#-------------------------------------------------------------------------------
def updateSubnets(scan_subnets):

View File

@@ -74,7 +74,7 @@ def importConfigs (db):
return
# Header
updateState("Import config")
updateState("Import config", showSpinner = True)
mylog('debug', ['[Import Config] importing config file'])
conf.mySettings = [] # reset settings
@@ -170,15 +170,15 @@ def importConfigs (db):
#cron_instance = Cron()
# timestamps of last execution times
conf.startTime = conf.time_started
now_minus_24h = conf.time_started - datetime.timedelta(hours = 24)
conf.startTime = conf.time_started
now_minus_24h = conf.time_started - datetime.timedelta(hours = 24)
# set these times to the past to force the first run
conf.last_internet_IP_scan = now_minus_24h
conf.last_scan_run = now_minus_24h
conf.last_cleanup = now_minus_24h
conf.last_update_vendors = conf.time_started - datetime.timedelta(days = 6) # update vendors 24h after first run and then once a week
conf.last_version_check = now_minus_24h
conf.last_internet_IP_scan = now_minus_24h
conf.last_scan_run = now_minus_24h
conf.last_cleanup = now_minus_24h
conf.last_update_vendors = conf.time_started - datetime.timedelta(days = 6) # update vendors 24h after first run and then once a week
conf.last_version_check = now_minus_24h
# TODO cleanup later ----------------------------------------------------------------------------------
@@ -254,8 +254,6 @@ def importConfigs (db):
conf.plugins_once_run = False
# -----------------
# Plugins END
@@ -279,9 +277,9 @@ def importConfigs (db):
run_plugin_scripts(db, 'before_config_save' )
# Used to determine the next import
conf.lastImportedConfFile = os.path.getmtime(config_file)
#TO DO this creates a circular reference between API and HELPER !
conf.lastImportedConfFile = os.path.getmtime(config_file)
updateState("Config imported", conf.lastImportedConfFile, conf.lastImportedConfFile, False)
mylog('minimal', '[Config] Imported new config')