new: [app:backup] Added backup feature that saves exercise progress every 5sec

This commit is contained in:
Sami Mokaddem 2024-07-08 10:21:59 +02:00
parent 6178592d10
commit 1277dbb132
5 changed files with 38 additions and 5 deletions

1
.gitignore vendored
View file

@ -2,6 +2,7 @@ __pycache__
venv venv
config.py config.py
misp_cache.sqlite misp_cache.sqlite
backup.json
# Logs # Logs
logs logs

4
db.py
View file

@ -5,14 +5,14 @@ import collections
USER_ID_TO_EMAIL_MAPPING = {} USER_ID_TO_EMAIL_MAPPING = {}
USER_ID_TO_AUTHKEY_MAPPING = {} USER_ID_TO_AUTHKEY_MAPPING = {}
ALL_EXERCISES = [] ALL_EXERCISES = []
SELECTED_EXERCISES = [] SELECTED_EXERCISES = []
INJECT_BY_UUID = {} INJECT_BY_UUID = {}
INJECT_SEQUENCE_BY_INJECT_UUID = {} INJECT_SEQUENCE_BY_INJECT_UUID = {}
INJECT_REQUIREMENTS_BY_INJECT_UUID = {} INJECT_REQUIREMENTS_BY_INJECT_UUID = {}
EXERCISES_STATUS = {} EXERCISES_STATUS = {}
PROGRESS = {
}
NOTIFICATION_BUFFER_SIZE = 30 NOTIFICATION_BUFFER_SIZE = 30
NOTIFICATION_MESSAGES = collections.deque([], NOTIFICATION_BUFFER_SIZE) NOTIFICATION_MESSAGES = collections.deque([], NOTIFICATION_BUFFER_SIZE)

View file

@ -60,6 +60,29 @@ def read_exercise_dir():
return exercises 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: def is_validate_exercises(exercises: list) -> bool:
exercises_uuid = set() exercises_uuid = set()
tasks_uuid = set() tasks_uuid = set()
@ -181,6 +204,7 @@ def resetAllExerciseProgress():
for exercise_status in db.EXERCISES_STATUS.values(): for exercise_status in db.EXERCISES_STATUS.values():
for task in exercise_status['tasks'].values(): for task in exercise_status['tasks'].values():
mark_task_incomplete(user_id, exercise_status['uuid'], task['uuid']) mark_task_incomplete(user_id, exercise_status['uuid'], task['uuid'])
backup_exercises_progress()
def get_completed_tasks_for_user(user_id: int): def get_completed_tasks_for_user(user_id: int):

View file

@ -51,7 +51,6 @@ zsocket.setsockopt_string(zmq.SUBSCRIBE, '')
# Initialize Socket.IO server # Initialize Socket.IO server
# sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet')
sio = socketio.AsyncServer(cors_allowed_origins='*', async_mode='aiohttp') sio = socketio.AsyncServer(cors_allowed_origins='*', async_mode='aiohttp')
app = web.Application() app = web.Application()
sio.attach(app) sio.attach(app)
@ -212,6 +211,12 @@ async def keepalive():
await sio.emit('keep_alive', payload) 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 # Function to forward zmq messages to Socket.IO
async def forward_zmq_to_socketio(): async def forward_zmq_to_socketio():
global ZMQ_MESSAGE_COUNT, ZMQ_LAST_TIME 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(forward_zmq_to_socketio)
sio.start_background_task(keepalive) sio.start_background_task(keepalive)
sio.start_background_task(notification_history) sio.start_background_task(notification_history)
sio.start_background_task(backup_exercises_progress)
return app return app
@ -245,4 +251,6 @@ if __name__ == "__main__":
logger.critical('Could not load exercises') logger.critical('Could not load exercises')
sys.exit(1) sys.exit(1)
exercise_model.restore_exercices_progress()
web.run_app(init_app(), host=config.server_host, port=config.server_port) web.run_app(init_app(), host=config.server_host, port=config.server_port)

View file

@ -9,8 +9,8 @@
const verbose = ref(false) const verbose = ref(false)
const api_query = ref(false) const api_query = ref(false)
const chartInitSeries = [ const chartInitSeries = [
{data: Array.apply(null, {length: 180}).map(Function.call, Math.random)} // {data: Array.apply(null, {length: 180}).map(Function.call, Math.random)}
// {data: Array.from(Array(120)).map(()=> 0)} {data: Array.from(Array(12*20)).map(()=> 0)}
] ]
const notificationHistorySeries = computed(() => { const notificationHistorySeries = computed(() => {