diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index f417837a..3ee5ea18 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -7,6 +7,7 @@ from app.classes.models.server_permissions import ( from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.roles import HelperRoles from app.classes.models.servers import HelperServers +from app.classes.models.server_stats import HelperServerStats from app.classes.shared.main_models import DatabaseShortcuts logger = logging.getLogger(__name__) @@ -111,7 +112,7 @@ class ServerPermsController: ) for server in authorized_servers: - latest = HelperServers.get_latest_server_stats(server.get("server_id")) + latest = HelperServerStats.get_latest_server_stats(server.get("server_id")) server_data.append( { "server_data": server, diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index a691cec2..ccbe975f 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -4,6 +4,7 @@ import json from app.classes.controllers.roles_controller import RolesController from app.classes.models.servers import HelperServers +from app.classes.models.server_stats import HelperServerStats from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.server_permissions import ( PermissionsServers, @@ -58,15 +59,15 @@ class ServersController: @staticmethod def set_download(server_id): - return HelperServers.set_download(server_id) + return HelperServerStats.set_download(server_id) @staticmethod def finish_download(server_id): - return HelperServers.finish_download(server_id) + return HelperServerStats.finish_download(server_id) @staticmethod def get_download_status(server_id): - return HelperServers.get_download_status(server_id) + return HelperServerStats.get_download_status(server_id) def remove_server(self, server_id): roles_list = PermissionsServers.get_roles_from_server(server_id) @@ -104,7 +105,7 @@ class ServersController: @staticmethod def get_all_servers_stats(): - return HelperServers.get_all_servers_stats() + return HelperServerStats.get_all_servers_stats() @staticmethod def get_authorized_servers_stats_api_key(api_key: ApiKeys): @@ -114,7 +115,7 @@ class ServersController: ) for server in authorized_servers: - latest = HelperServers.get_latest_server_stats(server.get("server_id")) + latest = HelperServerStats.get_latest_server_stats(server.get("server_id")) key_permissions = PermissionsServers.get_api_key_permissions_list( api_key, server.get("server_id") ) @@ -137,7 +138,7 @@ class ServersController: authorized_servers = ServersController.get_authorized_servers(user_id) for server in authorized_servers: - latest = HelperServers.get_latest_server_stats(server.get("server_id")) + latest = HelperServerStats.get_latest_server_stats(server.get("server_id")) # TODO user_permissions = PermissionsServers.get_user_id_permissions_list( user_id, server.get("server_id") @@ -165,11 +166,11 @@ class ServersController: # ********************************************************************************** @staticmethod def get_server_stats_by_id(server_id): - return HelperServers.get_server_stats_by_id(server_id) + return HelperServerStats.get_server_stats_by_id(server_id) @staticmethod def server_id_exists(server_id): - return HelperServers.server_id_exists(server_id) + return HelperServerStats.server_id_exists(server_id) @staticmethod def get_server_type_by_id(server_id): @@ -188,7 +189,7 @@ class ServersController: @staticmethod def is_crashed(server_id): - return HelperServers.is_crashed(server_id) + return HelperServerStats.is_crashed(server_id) @staticmethod def server_id_authorized_api_key(server_id: str, api_key: ApiKeys) -> bool: @@ -197,34 +198,36 @@ class ServersController: # There is no view server permission # permission_helper.both_have_perm(api_key) - def set_update(self, server_id, value): - return self.servers_helper.set_update(server_id, value) + @staticmethod + def set_update(server_id, value): + return HelperServerStats.set_update(server_id, value) @staticmethod def get_ttl_without_player(server_id): - return HelperServers.get_ttl_without_player(server_id) + return HelperServerStats.get_ttl_without_player(server_id) @staticmethod def can_stop_no_players(server_id, time_limit): - return HelperServers.can_stop_no_players(server_id, time_limit) + return HelperServerStats.can_stop_no_players(server_id, time_limit) - def set_waiting_start(self, server_id, value): - self.servers_helper.set_waiting_start(server_id, value) + @staticmethod + def set_waiting_start(server_id, value): + HelperServerStats.set_waiting_start(server_id, value) @staticmethod def get_waiting_start(server_id): - return HelperServers.get_waiting_start(server_id) + return HelperServerStats.get_waiting_start(server_id) @staticmethod def get_update_status(server_id): - return HelperServers.get_update_status(server_id) + return HelperServerStats.get_update_status(server_id) # ********************************************************************************** # Servers Helpers Methods # ********************************************************************************** @staticmethod def get_banned_players(server_id): - stats = HelperServers.get_server_stats_by_id(server_id) + stats = HelperServerStats.get_server_stats_by_id(server_id) server_path = stats["server_id"]["path"] path = os.path.join(server_path, "banned-players.json") diff --git a/app/classes/minecraft/stats.py b/app/classes/minecraft/stats.py index ff861854..5886eb5d 100644 --- a/app/classes/minecraft/stats.py +++ b/app/classes/minecraft/stats.py @@ -210,36 +210,9 @@ class Stats: } ).execute() - # server_stats = stats_to_send.get("servers") - # for server in server_stats: - # Server_Stats.insert( - # { - # Server_Stats.server_id: server.get("id", 0), - # Server_Stats.started: server.get("started", ""), - # Server_Stats.running: server.get("running", False), - # Server_Stats.cpu: server.get("cpu", 0), - # Server_Stats.mem: server.get("mem", 0), - # Server_Stats.mem_percent: server.get("mem_percent", 0), - # Server_Stats.world_name: server.get("world_name", ""), - # Server_Stats.world_size: server.get("world_size", ""), - # Server_Stats.server_port: server.get("server_port", ""), - # Server_Stats.int_ping_results: server.get( - # "int_ping_results", False - # ), - # Server_Stats.online: server.get("online", False), - # Server_Stats.max: server.get("max", False), - # Server_Stats.players: server.get("players", False), - # Server_Stats.desc: server.get("desc", False), - # Server_Stats.version: server.get("version", False), - # } - # ).execute() - # delete old data max_age = self.helper.get_setting("history_max_age") now = datetime.datetime.now() last_week = now.day - max_age HostStats.delete().where(HostStats.time < last_week).execute() - - -# Server_Stats.delete().where(Server_Stats.created < last_week).execute() diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py new file mode 100644 index 00000000..03358c78 --- /dev/null +++ b/app/classes/models/server_stats.py @@ -0,0 +1,355 @@ +import os +import logging +import datetime + +from app.classes.models.servers import Servers, HelperServers +from app.classes.shared.helpers import Helpers +from app.classes.shared.main_models import DatabaseShortcuts +from app.classes.shared.migration import MigrationManager + +try: + from peewee import ( + SqliteDatabase, + Model, + DatabaseProxy, + ForeignKeyField, + CharField, + AutoField, + DateTimeField, + BooleanField, + IntegerField, + FloatField, + ) + +except ModuleNotFoundError as e: + Helpers.auto_installer_fix(e) + +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger("peewee") +peewee_logger.setLevel(logging.INFO) +database_stats_proxy = DatabaseProxy() + + +# ********************************************************************************** +# Servers Stats Class +# ********************************************************************************** +class ServerStats(Model): + stats_id = AutoField() + created = DateTimeField(default=datetime.datetime.now) + server_id = ForeignKeyField(Servers, backref="server", index=True) + started = CharField(default="") + running = BooleanField(default=False) + cpu = FloatField(default=0) + mem = FloatField(default=0) + mem_percent = FloatField(default=0) + world_name = CharField(default="") + world_size = CharField(default="") + server_port = IntegerField(default=25565) + int_ping_results = CharField(default="") + online = IntegerField(default=0) + max = IntegerField(default=0) + players = CharField(default="") + desc = CharField(default="Unable to Connect") + version = CharField(default="") + updating = BooleanField(default=False) + waiting_start = BooleanField(default=False) + first_run = BooleanField(default=True) + crashed = BooleanField(default=False) + downloading = BooleanField(default=False) + + class Meta: + table_name = "server_stats" + database = database_stats_proxy + + +# ********************************************************************************** +# Servers_Stats Methods +# ********************************************************************************** +class HelperServerStats: + def __init__(self, database): + self.database = database + + @staticmethod + def init_database(server_id): + try: + server = HelperServers.get_server_data_by_id(server_id) + db_folder = os.path.join(f"{server['path']}", "db_stats") + db_file = os.path.join( + db_folder, + f"{server['server_name']}" + ".sqlite", + ) + database = SqliteDatabase( + db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10} + ) + if not os.path.exists(db_file): + try: + os.mkdir(db_folder) + except Exception as ex: + logger.warning( + f"Error try to create the db_stats folder for server : {ex}" + ) + helper_stats = Helpers() + helper_stats.migration_dir = os.path.join( + f"{helper_stats.migration_dir}", "stats" + ) + helper_stats.db_path = db_file + database_stats_proxy.initialize(database) + migration_manager = MigrationManager(database, helper_stats) + migration_manager.up() # Automatically runs migrations + database_stats_proxy.initialize(database) + except Exception as ex: + logger.warning( + f"Error try to look for the db_stats files for server : {ex}" + ) + + @staticmethod + def select_database(server_id): + try: + server = HelperServers.get_server_data_by_id(server_id) + db_file = os.path.join( + f"{server['path']}", + "db_stats", + f"{server['server_name']}" + ".sqlite", + ) + database = SqliteDatabase( + db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10} + ) + database_stats_proxy.initialize(database) + except Exception as ex: + logger.warning( + f"Error try to look for the db_stats files for server : {ex}" + ) + + @staticmethod + def get_all_servers_stats(): + servers = HelperServers.get_all_defined_servers() + server_data = [] + try: + for s in servers: + HelperServerStats.select_database(s.get("server_id")) + latest = ( + ServerStats.select() + .where(ServerStats.server_id == s.get("server_id")) + .order_by(ServerStats.created.desc()) + .limit(1) + ) + server_data.append( + { + "server_data": s, + "stats": DatabaseShortcuts.return_rows(latest)[0], + "user_command_permission": True, + } + ) + except IndexError as ex: + logger.error( + f"Stats collection failed with error: {ex}. Was a server just created?" + ) + return server_data + + @staticmethod + def insert_server_stats(server): + server_id = server.get("id", 0) + HelperServerStats.select_database(server_id) + + if server_id == 0: + logger.warning("Stats saving failed with error: Server unknown (id = 0)") + return + + ServerStats.insert( + { + ServerStats.server_id: server.get("id", 0), + ServerStats.started: server.get("started", ""), + ServerStats.running: server.get("running", False), + ServerStats.cpu: server.get("cpu", 0), + ServerStats.mem: server.get("mem", 0), + ServerStats.mem_percent: server.get("mem_percent", 0), + ServerStats.world_name: server.get("world_name", ""), + ServerStats.world_size: server.get("world_size", ""), + ServerStats.server_port: server.get("server_port", ""), + ServerStats.int_ping_results: server.get("int_ping_results", False), + ServerStats.online: server.get("online", False), + ServerStats.max: server.get("max", False), + ServerStats.players: server.get("players", False), + ServerStats.desc: server.get("desc", False), + ServerStats.version: server.get("version", False), + } + ).execute() + + @staticmethod + def remove_old_stats(server_id, last_week): + HelperServerStats.select_database(server_id) + ServerStats.delete().where(ServerStats.created < last_week).execute() + + @staticmethod + def get_latest_server_stats(server_id): + HelperServerStats.select_database(server_id) + return ( + ServerStats.select() + .where(ServerStats.server_id == server_id) + .order_by(ServerStats.created.desc()) + .limit(1) + ) + + @staticmethod + def get_server_stats_by_id(server_id): + HelperServerStats.select_database(server_id) + stats = ( + ServerStats.select() + .where(ServerStats.server_id == server_id) + .order_by(ServerStats.created.desc()) + .limit(1) + ) + return DatabaseShortcuts.return_rows(stats)[0] + + @staticmethod + def server_id_exists(server_id): + HelperServerStats.select_database(server_id) + if not HelperServers.get_server_data_by_id(server_id): + return False + return True + + @staticmethod + def sever_crashed(server_id): + HelperServerStats.select_database(server_id) + with database_stats_proxy.atomic(): + ServerStats.update(crashed=True).where( + ServerStats.server_id == server_id + ).execute() + + @staticmethod + def set_download(server_id): + HelperServerStats.select_database(server_id) + with database_stats_proxy.atomic(): + ServerStats.update(downloading=True).where( + ServerStats.server_id == server_id + ).execute() + + @staticmethod + def finish_download(server_id): + HelperServerStats.select_database(server_id) + with database_stats_proxy.atomic(): + ServerStats.update(downloading=False).where( + ServerStats.server_id == server_id + ).execute() + + @staticmethod + def get_download_status(server_id): + HelperServerStats.select_database(server_id) + download_status = ( + ServerStats.select().where(ServerStats.server_id == server_id).get() + ) + return download_status.downloading + + @staticmethod + def server_crash_reset(server_id): + if server_id is None: + return + + HelperServerStats.select_database(server_id) + with database_stats_proxy.atomic(): + ServerStats.update(crashed=False).where( + ServerStats.server_id == server_id + ).execute() + + @staticmethod + def is_crashed(server_id): + HelperServerStats.select_database(server_id) + svr = ServerStats.select().where(ServerStats.server_id == server_id).get() + # pylint: disable=singleton-comparison + if svr.crashed == True: + return True + else: + return False + + @staticmethod + def set_update(server_id, value): + if server_id is None: + return + + HelperServerStats.select_database(server_id) + try: + # Checks if server even exists + ServerStats.select().where(ServerStats.server_id == server_id) + except Exception as ex: + logger.error(f"Database entry not found! {ex}") + with database_stats_proxy.atomic(): + ServerStats.update(updating=value).where( + ServerStats.server_id == server_id + ).execute() + + @staticmethod + def get_update_status(server_id): + HelperServerStats.select_database(server_id) + update_status = ( + ServerStats.select().where(ServerStats.server_id == server_id).get() + ) + return update_status.updating + + @staticmethod + def set_first_run(server_id): + HelperServerStats.select_database(server_id) + # Sets first run to false + try: + # Checks if server even exists + ServerStats.select().where(ServerStats.server_id == server_id) + except Exception as ex: + logger.error(f"Database entry not found! {ex}") + return + with database_stats_proxy.atomic(): + ServerStats.update(first_run=False).where( + ServerStats.server_id == server_id + ).execute() + + @staticmethod + def get_first_run(server_id): + HelperServerStats.select_database(server_id) + first_run = ServerStats.select().where(ServerStats.server_id == server_id).get() + return first_run.first_run + + @staticmethod + def get_TTL_without_player(server_id): + HelperServerStats.select_database(server_id) + last_stat = ( + ServerStats.select() + .where(ServerStats.server_id == server_id) + .order_by(ServerStats.created.desc()) + .first() + ) + last_stat_with_player = ( + ServerStats.select() + .where(ServerStats.server_id == server_id) + .where(ServerStats.online > 0) + .order_by(ServerStats.created.desc()) + .first() + ) + return last_stat.created - last_stat_with_player.created + + @staticmethod + def can_stop_no_players(server_id, time_limit): + HelperServerStats.select_database(server_id) + can = False + ttl_no_players = HelperServerStats.get_TTL_without_player(server_id) + if (time_limit == -1) or (ttl_no_players > time_limit): + can = True + return can + + @staticmethod + def set_waiting_start(server_id, value): + HelperServerStats.select_database(server_id) + try: + # Checks if server even exists + ServerStats.select().where(ServerStats.server_id == server_id) + except Exception as ex: + logger.error(f"Database entry not found! {ex}") + with database_stats_proxy.atomic(): + ServerStats.update(waiting_start=value).where( + ServerStats.server_id == server_id + ).execute() + + @staticmethod + def get_waiting_start(server_id): + HelperServerStats.select_database(server_id) + waiting_start = ( + ServerStats.select().where(ServerStats.server_id == server_id).get() + ) + return waiting_start.waiting_start diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index aa6744c0..fb0a1658 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -1,13 +1,11 @@ import logging import datetime from peewee import ( - ForeignKeyField, CharField, AutoField, DateTimeField, BooleanField, IntegerField, - FloatField, ) from app.classes.shared.main_models import DatabaseShortcuts @@ -42,37 +40,6 @@ class Servers(BaseModel): table_name = "servers" -# ********************************************************************************** -# Servers Stats Class -# ********************************************************************************** -class ServerStats(BaseModel): - stats_id = AutoField() - created = DateTimeField(default=datetime.datetime.now) - server_id = ForeignKeyField(Servers, backref="server", index=True) - started = CharField(default="") - running = BooleanField(default=False) - cpu = FloatField(default=0) - mem = FloatField(default=0) - mem_percent = FloatField(default=0) - world_name = CharField(default="") - world_size = CharField(default="") - server_port = IntegerField(default=25565) - int_ping_results = CharField(default="") - online = IntegerField(default=0) - max = IntegerField(default=0) - players = CharField(default="") - desc = CharField(default="Unable to Connect") - version = CharField(default="") - updating = BooleanField(default=False) - waiting_start = BooleanField(default=False) - first_run = BooleanField(default=True) - crashed = BooleanField(default=False) - downloading = BooleanField(default=False) - - class Meta: - table_name = "server_stats" - - # ********************************************************************************** # Servers Class # ********************************************************************************** @@ -147,31 +114,6 @@ class HelperServers: query = Servers.select() return DatabaseShortcuts.return_rows(query) - @staticmethod - def get_all_servers_stats(): - servers = HelperServers.get_all_defined_servers() - server_data = [] - try: - for s in servers: - latest = ( - ServerStats.select() - .where(ServerStats.server_id == s.get("server_id")) - .order_by(ServerStats.created.desc()) - .limit(1) - ) - server_data.append( - { - "server_data": s, - "stats": DatabaseShortcuts.return_rows(latest)[0], - "user_command_permission": True, - } - ) - except IndexError as ex: - logger.error( - f"Stats collection failed with error: {ex}. Was a server just created?" - ) - return server_data - @staticmethod def get_server_friendly_name(server_id): server_data = HelperServers.get_server_data_by_id(server_id) @@ -180,149 +122,3 @@ class HelperServers: f"with ID: {server_data.get('server_id', 0)}" ) return friendly_name - - # ********************************************************************************** - # Servers_Stats Methods - # ********************************************************************************** - @staticmethod - def get_latest_server_stats(server_id): - return ( - ServerStats.select() - .where(ServerStats.server_id == server_id) - .order_by(ServerStats.created.desc()) - .limit(1) - ) - - @staticmethod - def get_server_stats_by_id(server_id): - stats = ( - ServerStats.select() - .where(ServerStats.server_id == server_id) - .order_by(ServerStats.created.desc()) - .limit(1) - ) - return DatabaseShortcuts.return_rows(stats)[0] - - @staticmethod - def server_id_exists(server_id): - if not HelperServers.get_server_data_by_id(server_id): - return False - return True - - @staticmethod - def sever_crashed(server_id): - ServerStats.update(crashed=True).where( - ServerStats.server_id == server_id - ).execute() - - @staticmethod - def set_download(server_id): - ServerStats.update(downloading=True).where( - ServerStats.server_id == server_id - ).execute() - - @staticmethod - def finish_download(server_id): - ServerStats.update(downloading=False).where( - ServerStats.server_id == server_id - ).execute() - - @staticmethod - def get_download_status(server_id): - download_status = ( - ServerStats.select().where(ServerStats.server_id == server_id).get() - ) - return download_status.downloading - - @staticmethod - def server_crash_reset(server_id): - ServerStats.update(crashed=False).where( - ServerStats.server_id == server_id - ).execute() - - @staticmethod - def is_crashed(server_id): - svr = ServerStats.select().where(ServerStats.server_id == server_id).get() - if svr.crashed is True: - return True - else: - return False - - @staticmethod - def set_update(server_id, value): - try: - # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == server_id) - except Exception as ex: - logger.error(f"Database entry not found! {ex}") - ServerStats.update(updating=value).where( - ServerStats.server_id == server_id - ).execute() - - @staticmethod - def get_update_status(server_id): - update_status = ( - ServerStats.select().where(ServerStats.server_id == server_id).get() - ) - return update_status.updating - - @staticmethod - def set_first_run(server_id): - # Sets first run to false - try: - # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == server_id) - except Exception as ex: - logger.error(f"Database entry not found! {ex}") - return - ServerStats.update(first_run=False).where( - ServerStats.server_id == server_id - ).execute() - - @staticmethod - def get_first_run(server_id): - first_run = ServerStats.select().where(ServerStats.server_id == server_id).get() - return first_run.first_run - - @staticmethod - def get_ttl_without_player(server_id): - last_stat = ( - ServerStats.select() - .where(ServerStats.server_id == server_id) - .order_by(ServerStats.created.desc()) - .first() - ) - last_stat_with_player = ( - ServerStats.select() - .where(ServerStats.server_id == server_id) - .where(ServerStats.online > 0) - .order_by(ServerStats.created.desc()) - .first() - ) - return last_stat.created - last_stat_with_player.created - - @staticmethod - def can_stop_no_players(server_id, time_limit): - can = False - ttl_no_players = HelperServers.get_ttl_without_player(server_id) - if (time_limit == -1) or (ttl_no_players > time_limit): - can = True - return can - - @staticmethod - def set_waiting_start(server_id, value): - try: - # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == server_id) - except Exception as ex: - logger.error(f"Database entry not found! {ex}") - ServerStats.update(waiting_start=value).where( - ServerStats.server_id == server_id - ).execute() - - @staticmethod - def get_waiting_start(server_id): - waiting_start = ( - ServerStats.select().where(ServerStats.server_id == server_id).get() - ) - return waiting_start.waiting_start diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index af0073cf..7be5fad4 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -17,7 +17,8 @@ from apscheduler.schedulers.background import BackgroundScheduler from app.classes.minecraft.stats import Stats from app.classes.minecraft.mc_ping import ping, ping_bedrock -from app.classes.models.servers import ServerStats, HelperServers +from app.classes.models.servers import HelperServers +from app.classes.models.server_stats import HelperServerStats from app.classes.models.management import HelpersManagement from app.classes.models.users import HelperUsers from app.classes.models.server_permissions import PermissionsServers @@ -118,8 +119,8 @@ class Server: ) self.is_backingup = False # Reset crash and update at initialization - HelperServers.server_crash_reset(self.server_id) - HelperServers.set_update(self.server_id, False) + HelperServerStats.server_crash_reset(self.server_id) + HelperServerStats.set_update(self.server_id, False) # ********************************************************************************** # Minecraft Server Management @@ -142,6 +143,7 @@ class Server: self.name = server_name self.settings = server_data_obj + HelperServerStats.init_database(server_id) self.record_server_stats() # build our server run command @@ -163,7 +165,7 @@ class Server: Console.info(f"Starting server ID: {self.server_id} - {self.name}") logger.info(f"Starting server ID: {self.server_id} - {self.name}") # Sets waiting start to false since we're attempting to start the server. - HelperServers.set_waiting_start(self.server_id, False) + HelperServerStats.set_waiting_start(self.server_id, False) self.run_threaded_server(None) # remove the scheduled job since it's ran @@ -230,7 +232,7 @@ class Server: else: user_lang = HelperUsers.get_user_lang_by_id(user_id) - if HelperServers.get_download_status(self.server_id): + if HelperServerStats.get_download_status(self.server_id): if user_id: self.helper.websocket_helper.broadcast_user( user_id, @@ -413,7 +415,7 @@ class Server: ).start() self.is_crashed = False - HelperServers.server_crash_reset(self.server_id) + HelperServerStats.server_crash_reset(self.server_id) self.start_time = str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) @@ -421,7 +423,7 @@ class Server: logger.info(f"Server {self.name} running with PID {self.process.pid}") Console.info(f"Server {self.name} running with PID {self.process.pid}") self.is_crashed = False - HelperServers.server_crash_reset(self.server_id) + HelperServerStats.server_crash_reset(self.server_id) self.record_server_stats() check_internet_thread = threading.Thread( target=self.check_internet_thread, @@ -434,11 +436,11 @@ class Server: ) check_internet_thread.start() # Checks if this is the servers first run. - if HelperServers.get_first_run(self.server_id): - HelperServers.set_first_run(self.server_id) - loc_server_port = HelperServers.get_server_stats_by_id(self.server_id)[ - "server_port" - ] + if HelperServerStats.get_first_run(self.server_id): + HelperServerStats.set_first_run(self.server_id) + loc_server_port = HelperServerStats.get_server_stats_by_id( + self.server_id + )["server_port"] # Sends port reminder message. self.helper.websocket_helper.broadcast_user( user_id, @@ -718,7 +720,7 @@ class Server: self.server_scheduler.remove_job("c_" + str(self.server_id)) return - HelperServers.sever_crashed(self.server_id) + HelperServerStats.sever_crashed(self.server_id) # if we haven't tried to restart more 3 or more times if self.restart_count <= 3: @@ -742,7 +744,7 @@ class Server: self.restart_count = 0 self.is_crashed = True - HelperServers.sever_crashed(self.server_id) + HelperServerStats.sever_crashed(self.server_id) # cancel the watcher task self.server_scheduler.remove_job("c_" + str(self.server_id)) @@ -972,7 +974,7 @@ class Server: return [] def jar_update(self): - HelperServers.set_update(self.server_id, True) + HelperServerStats.set_update(self.server_id, True) update_thread = threading.Thread( target=self.a_jar_update, daemon=True, name=f"exe_update_{self.name}" ) @@ -980,7 +982,7 @@ class Server: def check_update(self): - if HelperServers.get_server_stats_by_id(self.server_id)["updating"]: + if HelperServerStats.get_server_stats_by_id(self.server_id)["updating"]: return True else: return False @@ -1053,11 +1055,11 @@ class Server: self.settings["executable_update_url"], current_executable ) - while HelperServers.get_server_stats_by_id(self.server_id)["updating"]: + while HelperServerStats.get_server_stats_by_id(self.server_id)["updating"]: if downloaded and not self.is_backingup: logger.info("Executable updated successfully. Starting Server") - HelperServers.set_update(self.server_id, False) + HelperServerStats.set_update(self.server_id, False) if len(self.helper.websocket_helper.clients) > 0: # There are clients self.check_update() @@ -1453,29 +1455,11 @@ class Server: def record_server_stats(self): server = self.get_servers_stats() - ServerStats.insert( - { - ServerStats.server_id: server.get("id", 0), - ServerStats.started: server.get("started", ""), - ServerStats.running: server.get("running", False), - ServerStats.cpu: server.get("cpu", 0), - ServerStats.mem: server.get("mem", 0), - ServerStats.mem_percent: server.get("mem_percent", 0), - ServerStats.world_name: server.get("world_name", ""), - ServerStats.world_size: server.get("world_size", ""), - ServerStats.server_port: server.get("server_port", ""), - ServerStats.int_ping_results: server.get("int_ping_results", False), - ServerStats.online: server.get("online", False), - ServerStats.max: server.get("max", False), - ServerStats.players: server.get("players", False), - ServerStats.desc: server.get("desc", False), - ServerStats.version: server.get("version", False), - } - ).execute() + HelperServerStats.insert_server_stats(server) # delete old data max_age = self.helper.get_setting("history_max_age") now = datetime.datetime.now() last_week = now.day - max_age - ServerStats.delete().where(ServerStats.created < last_week).execute() + HelperServerStats.remove_old_stats(server.get("id", 0), last_week) diff --git a/app/migrations/stats/20220427_init.py b/app/migrations/stats/20220427_init.py new file mode 100644 index 00000000..75c1a39e --- /dev/null +++ b/app/migrations/stats/20220427_init.py @@ -0,0 +1,42 @@ +import peewee +import datetime + +from app.classes.models.servers import Servers + + +def migrate(migrator, database, **kwargs): + db = database + + class ServerStats(peewee.Model): + stats_id = peewee.AutoField() + created = peewee.DateTimeField(default=datetime.datetime.now) + server_id = peewee.ForeignKeyField(Servers, backref="server", index=True) + started = peewee.CharField(default="") + running = peewee.BooleanField(default=False) + cpu = peewee.FloatField(default=0) + mem = peewee.FloatField(default=0) + mem_percent = peewee.FloatField(default=0) + world_name = peewee.CharField(default="") + world_size = peewee.CharField(default="") + server_port = peewee.IntegerField(default=25565) + int_ping_results = peewee.CharField(default="") + online = peewee.IntegerField(default=0) + max = peewee.IntegerField(default=0) + players = peewee.CharField(default="") + desc = peewee.CharField(default="Unable to Connect") + version = peewee.CharField(default="") + updating = peewee.BooleanField(default=False) + waiting_start = peewee.BooleanField(default=False) + first_run = peewee.BooleanField(default=True) + crashed = peewee.BooleanField(default=False) + downloading = peewee.BooleanField(default=False) + + class Meta: + table_name = "server_stats" + database = db + + migrator.create_table(ServerStats) + + +def rollback(migrator, database, **kwargs): + migrator.drop_table("server_stats") diff --git a/main.py b/main.py index 6a083ef2..7a0239a0 100644 --- a/main.py +++ b/main.py @@ -130,7 +130,7 @@ if __name__ == "__main__": else: Console.debug("Existing install detected") - # now the tables are created, we can load the tasks_manger and server controller + # now the tables are created, we can load the tasks_manager and server controller controller = Controller(database, helper) tasks_manager = TasksManager(helper, controller) tasks_manager.start_webserver()