diff --git a/.gitignore b/.gitignore index ffce1bd..ed7da0e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__ venv config.py misp_cache.sqlite +backup.json # Logs logs diff --git a/db.py b/db.py index 5efc942..f4ec4dd 100644 --- a/db.py +++ b/db.py @@ -5,14 +5,14 @@ import collections USER_ID_TO_EMAIL_MAPPING = {} USER_ID_TO_AUTHKEY_MAPPING = {} + ALL_EXERCISES = [] SELECTED_EXERCISES = [] INJECT_BY_UUID = {} INJECT_SEQUENCE_BY_INJECT_UUID = {} INJECT_REQUIREMENTS_BY_INJECT_UUID = {} EXERCISES_STATUS = {} -PROGRESS = { -} + NOTIFICATION_BUFFER_SIZE = 30 NOTIFICATION_MESSAGES = collections.deque([], NOTIFICATION_BUFFER_SIZE) diff --git a/exercise.py b/exercise.py index f00ecdc..002a88c 100644 --- a/exercise.py +++ b/exercise.py @@ -60,6 +60,29 @@ def read_exercise_dir(): return exercises +def backup_exercises_progress(): + with open('backup.json', 'w') as f: + toBackup = { + 'EXERCISES_STATUS': db.EXERCISES_STATUS, + 'SELECTED_EXERCISES': db.SELECTED_EXERCISES, + 'USER_ID_TO_EMAIL_MAPPING': db.USER_ID_TO_EMAIL_MAPPING, + 'USER_ID_TO_AUTHKEY_MAPPING': db.USER_ID_TO_AUTHKEY_MAPPING, + } + json.dump(toBackup, f) + + +def restore_exercices_progress(): + try: + with open('backup.json', 'r') as f: + data = json.load(f) + db.EXERCISES_STATUS = data['EXERCISES_STATUS'] + db.SELECTED_EXERCISES = data['SELECTED_EXERCISES'] + db.USER_ID_TO_EMAIL_MAPPING = data['USER_ID_TO_EMAIL_MAPPING'] + db.USER_ID_TO_AUTHKEY_MAPPING = data['USER_ID_TO_AUTHKEY_MAPPING'] + except: + logger.info('Could not restore exercise progress') + + def is_validate_exercises(exercises: list) -> bool: exercises_uuid = set() tasks_uuid = set() @@ -181,6 +204,7 @@ def resetAllExerciseProgress(): for exercise_status in db.EXERCISES_STATUS.values(): for task in exercise_status['tasks'].values(): mark_task_incomplete(user_id, exercise_status['uuid'], task['uuid']) + backup_exercises_progress() def get_completed_tasks_for_user(user_id: int): diff --git a/server.py b/server.py index 2b3f7ff..d2137d6 100755 --- a/server.py +++ b/server.py @@ -51,7 +51,6 @@ zsocket.setsockopt_string(zmq.SUBSCRIBE, '') # Initialize Socket.IO server -# sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet') sio = socketio.AsyncServer(cors_allowed_origins='*', async_mode='aiohttp') app = web.Application() sio.attach(app) @@ -212,6 +211,12 @@ async def keepalive(): await sio.emit('keep_alive', payload) +async def backup_exercises_progress(): + while True: + await sio.sleep(5) + exercise_model.backup_exercises_progress() + + # Function to forward zmq messages to Socket.IO async def forward_zmq_to_socketio(): global ZMQ_MESSAGE_COUNT, ZMQ_LAST_TIME @@ -232,6 +237,7 @@ async def init_app(): sio.start_background_task(forward_zmq_to_socketio) sio.start_background_task(keepalive) sio.start_background_task(notification_history) + sio.start_background_task(backup_exercises_progress) return app @@ -245,4 +251,6 @@ if __name__ == "__main__": logger.critical('Could not load exercises') sys.exit(1) + exercise_model.restore_exercices_progress() + web.run_app(init_app(), host=config.server_host, port=config.server_port) diff --git a/src/components/TheLiveLogs.vue b/src/components/TheLiveLogs.vue index c1b64cc..b3f3ca5 100644 --- a/src/components/TheLiveLogs.vue +++ b/src/components/TheLiveLogs.vue @@ -9,8 +9,8 @@ const verbose = ref(false) const api_query = ref(false) const chartInitSeries = [ - {data: Array.apply(null, {length: 180}).map(Function.call, Math.random)} - // {data: Array.from(Array(120)).map(()=> 0)} + // {data: Array.apply(null, {length: 180}).map(Function.call, Math.random)} + {data: Array.from(Array(12*20)).map(()=> 0)} ] const notificationHistorySeries = computed(() => {