diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index e63ede37..232da6a6 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -106,6 +106,14 @@ class Users_Controller: @staticmethod def user_id_exists(user_id): return users_helper.user_id_exists(user_id) + + @staticmethod + def set_prepare(user_id): + return users_helper.set_prepare(user_id) + + @staticmethod + def stop_prepare(user_id): + return users_helper.stop_prepare(user_id) @staticmethod def get_user_id_by_api_token(token: str) -> str: diff --git a/app/classes/models/users.py b/app/classes/models/users.py index 78966556..0e817dad 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -37,6 +37,7 @@ class Users(Model): support_logs = CharField(default = '') valid_tokens_from = DateTimeField(default=datetime.datetime.now) server_order = CharField(default="") + preparing = BooleanField(default=False) class Meta: table_name = "users" @@ -196,6 +197,18 @@ class helper_users: @staticmethod def set_support_path(user_id, support_path): Users.update(support_logs = support_path).where(Users.user_id == user_id).execute() + + @staticmethod + def set_prepare(user_id): + Users.update(preparing = True).where(Users.user_id == user_id).execute() + + @staticmethod + def stop_prepare(user_id): + Users.update(preparing = False).where(Users.user_id == user_id).execute() + + @staticmethod + def clear_support_status(): + Users.update(preparing = False).where(Users.preparing == True).execute() @staticmethod def user_id_exists(user_id): diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index c3d5f0a1..b1669aba 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -6,6 +6,7 @@ import time import logging import tempfile from typing import Union +from urllib import request from app.classes.controllers.crafty_perms_controller import Crafty_Perms_Controller from app.classes.controllers.management_controller import Management_Controller @@ -28,6 +29,9 @@ from app.classes.web.websocket_helper import websocket_helper try: from peewee import DoesNotExist + #TZLocal is set as a hidden import on win pipeline + from tzlocal import get_localzone + from apscheduler.schedulers.background import BackgroundScheduler except ModuleNotFoundError as err: helper.auto_installer_fix(err) @@ -45,6 +49,9 @@ class Controller: self.server_perms = Server_Perms_Controller() self.servers = Servers_Controller() self.users = Users_Controller() + tz = get_localzone() + self.support_scheduler = BackgroundScheduler(timezone=str(tz)) + self.support_scheduler.start() def check_server_loaded(self, server_id_to_check: int): @@ -128,6 +135,9 @@ class Controller: def package_support_logs(self, exec_user): + if exec_user['preparing']: + return + self.users.set_prepare(exec_user['user_id']) #pausing so on screen notifications can run for user time.sleep(7) websocket_helper.broadcast_user(exec_user['user_id'], 'notification', 'Preparing your support logs') @@ -161,13 +171,21 @@ class Controller: #Copy crafty logs to archive dir full_log_name = os.path.join(crafty_path, 'logs') file_helper.copy_dir(os.path.join(self.project_root, 'logs'), full_log_name) + self.support_scheduler.add_job(self.log_status, 'interval', seconds=1, id="logs_"+str(exec_user['user_id']), args = [full_temp, + tempZipStorage +'.zip', exec_user]) file_helper.make_archive(tempZipStorage, tempDir) + + if len(websocket_helper.clients) > 0: + websocket_helper.broadcast_user(exec_user['user_id'], 'support_status_update', helper.calc_percent(full_temp, tempZipStorage +'.zip')) tempZipStorage += '.zip' websocket_helper.broadcast_user(exec_user['user_id'], 'send_logs_bootbox', { }) self.users.set_support_path(exec_user['user_id'], tempZipStorage) + + self.users.stop_prepare(exec_user['user_id']) + self.support_scheduler.remove_job('logs_'+str(exec_user["user_id"])) @staticmethod def add_system_user(): @@ -189,6 +207,22 @@ class Controller: svr.start_crash_detection() else: svr.stop_crash_detection() + + def log_status(self, source_path, dest_path, exec_user): + results = helper.calc_percent(source_path, dest_path) + self.log_stats = results + + if len(websocket_helper.clients) > 0: + websocket_helper.broadcast_user(exec_user['user_id'], 'support_status_update', results) + + def send_log_status(self): + try: + return self.log_stats + except: + return { + 'percent': 0, + 'total_files': 0 + } def get_server_obj(self, server_id: Union[str, int]) -> Union[bool, Server]: for s in self.servers_list: @@ -603,3 +637,7 @@ class Controller: @staticmethod def clear_unexecuted_commands(): helpers_management.clear_unexecuted_commands() + + @staticmethod + def clear_support_status(): + helper_users.clear_support_status() diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index e638fa59..99082925 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -623,6 +623,7 @@ class Server: 'backupStarted', users_helper.get_user_lang_by_id(user)).format(self.name)) time.sleep(3) conf = management_helper.get_backup_config(self.server_id) + helper.ensure_dir_exists(self.settings['backup_path']) try: backup_filename = f"{self.settings['backup_path']}/{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}" logger.info(f"Creating backup of server '{self.settings['server_name']}'" + diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 57ea806a..1e1a6937 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -184,6 +184,7 @@ class PanelHandler(BaseHandler): # pylint: disable=unused-variable api_key, token_data, exec_user = self.current_user superuser = exec_user['superuser'] + preparing = exec_user['preparing'] if api_key is not None: superuser = superuser and api_key.superuser @@ -254,6 +255,7 @@ class PanelHandler(BaseHandler): 'lang': self.controller.users.get_user_lang_by_id(exec_user["user_id"]), 'lang_page': helper.getLangPage(self.controller.users.get_user_lang_by_id(exec_user["user_id"])), 'super_user': superuser, + 'preparing': preparing, 'api_key': { 'name': api_key.name, 'created': api_key.created, diff --git a/app/frontend/templates/base.html b/app/frontend/templates/base.html index bc81718d..fb701e4c 100644 --- a/app/frontend/templates/base.html +++ b/app/frontend/templates/base.html @@ -262,6 +262,17 @@ }) }); } + if (webSocket) { + webSocket.on('support_status_update', function (logs) { + if(logs.percent >= 100){ + document.getElementById('logs_progress_bar').innerHTML = '100%'; + document.getElementById('logs_progress_bar').style.width = '100%'; + }else{ + document.getElementById('logs_progress_bar').innerHTML = logs.percent +'%'; + document.getElementById('logs_progress_bar').style.width = logs.percent + '%'; + } + }); + } if (webSocket) { webSocket.on('send_logs_bootbox', function (server_id) { var x = document.querySelector('.bootbox'); diff --git a/app/frontend/templates/notify.html b/app/frontend/templates/notify.html index a0182928..583791a2 100644 --- a/app/frontend/templates/notify.html +++ b/app/frontend/templates/notify.html @@ -32,7 +32,14 @@ {% end %}

Email: {{ data['user_data']['email'] }}

+ {% if data['preparing'] %} + {{ translate('notify', 'supportLogs', data['lang']) }}

+
+
0%
+
+ {% else %} {{ translate('notify', 'supportLogs', data['lang']) }} + {% end %} {% if data['superuser'] %} {{ translate('notify', 'activityLog', data['lang']) }} {% end %} diff --git a/app/migrations/20220312_support_log_status.py b/app/migrations/20220312_support_log_status.py new file mode 100644 index 00000000..c843007b --- /dev/null +++ b/app/migrations/20220312_support_log_status.py @@ -0,0 +1,16 @@ +# Generated by database migrator +import peewee + +def migrate(migrator, database, **kwargs): + migrator.add_columns('users', preparing=peewee.BooleanField(default=False)) + """ + Write your migrations here. + """ + + + +def rollback(migrator, database, **kwargs): + migrator.drop_columns('users', ['preparing']) + """ + Write your rollback migrations here. + """ diff --git a/main.py b/main.py index e11cc8b8..cd4a9a65 100644 --- a/main.py +++ b/main.py @@ -148,6 +148,7 @@ if __name__ == '__main__': project_root = os.path.dirname(__file__) controller.set_project_root(project_root) controller.clear_unexecuted_commands() + controller.clear_support_status() def sigterm_handler(*sig): print() # for newline