2022-04-27 19:07:34 +00:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import datetime
|
2022-08-30 21:27:19 +00:00
|
|
|
from datetime import timedelta
|
2022-05-26 12:50:20 +00:00
|
|
|
|
2022-04-27 19:07:34 +00:00
|
|
|
from app.classes.models.servers import Servers, HelperServers
|
|
|
|
from app.classes.shared.helpers import Helpers
|
2022-05-26 21:19:59 +00:00
|
|
|
from app.classes.shared.main_models import DatabaseShortcuts
|
2022-04-27 19:07:34 +00:00
|
|
|
from app.classes.shared.migration import MigrationManager
|
|
|
|
|
|
|
|
try:
|
|
|
|
from peewee import (
|
|
|
|
SqliteDatabase,
|
|
|
|
Model,
|
|
|
|
ForeignKeyField,
|
|
|
|
CharField,
|
|
|
|
AutoField,
|
|
|
|
DateTimeField,
|
|
|
|
BooleanField,
|
|
|
|
IntegerField,
|
|
|
|
FloatField,
|
2022-05-25 11:16:14 +00:00
|
|
|
DoesNotExist,
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
Helpers.auto_installer_fix(e)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
peewee_logger = logging.getLogger("peewee")
|
|
|
|
peewee_logger.setLevel(logging.INFO)
|
|
|
|
|
2023-02-15 23:01:21 +00:00
|
|
|
|
2022-04-27 19:07:34 +00:00
|
|
|
# **********************************************************************************
|
|
|
|
# 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")
|
2023-09-02 19:51:28 +00:00
|
|
|
icon = CharField(default="")
|
2022-04-27 19:07:34 +00:00
|
|
|
version = CharField(default="")
|
|
|
|
updating = BooleanField(default=False)
|
|
|
|
waiting_start = BooleanField(default=False)
|
|
|
|
first_run = BooleanField(default=True)
|
|
|
|
crashed = BooleanField(default=False)
|
2022-08-17 18:48:05 +00:00
|
|
|
importing = BooleanField(default=False)
|
2022-04-27 19:07:34 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "server_stats"
|
|
|
|
|
|
|
|
|
|
|
|
# **********************************************************************************
|
|
|
|
# Servers_Stats Methods
|
|
|
|
# **********************************************************************************
|
|
|
|
class HelperServerStats:
|
2022-05-26 12:50:20 +00:00
|
|
|
server_id: int
|
|
|
|
database = None
|
|
|
|
|
|
|
|
def __init__(self, server_id):
|
|
|
|
self.server_id = int(server_id)
|
|
|
|
self.init_database(self.server_id)
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def init_database(self, server_id):
|
2022-04-27 19:07:34 +00:00
|
|
|
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,
|
2022-05-19 16:14:30 +00:00
|
|
|
"crafty_server_stats.sqlite",
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-05-26 12:50:20 +00:00
|
|
|
self.database = SqliteDatabase(
|
2022-04-27 19:07:34 +00:00
|
|
|
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
|
2022-05-26 12:50:20 +00:00
|
|
|
migration_manager = MigrationManager(self.database, helper_stats)
|
2022-04-27 19:07:34 +00:00
|
|
|
migration_manager.up() # Automatically runs migrations
|
|
|
|
except Exception as ex:
|
|
|
|
logger.warning(
|
|
|
|
f"Error try to look for the db_stats files for server : {ex}"
|
|
|
|
)
|
2022-05-24 22:48:27 +00:00
|
|
|
return None
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def select_database(self):
|
2022-04-27 19:07:34 +00:00
|
|
|
try:
|
2022-05-25 13:54:35 +00:00
|
|
|
server = HelperServers.get_server_data_by_id(self.server_id)
|
2022-04-27 19:07:34 +00:00
|
|
|
db_file = os.path.join(
|
|
|
|
f"{server['path']}",
|
|
|
|
"db_stats",
|
2022-05-19 16:14:30 +00:00
|
|
|
"crafty_server_stats.sqlite",
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-05-26 12:50:20 +00:00
|
|
|
self.database = SqliteDatabase(
|
2022-04-27 19:07:34 +00:00
|
|
|
db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10}
|
|
|
|
)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.warning(
|
|
|
|
f"Error try to look for the db_stats files for server : {ex}"
|
|
|
|
)
|
2022-05-24 22:48:27 +00:00
|
|
|
return None
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def get_all_servers_stats(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
servers = HelperServers.get_all_defined_servers()
|
|
|
|
server_data = []
|
|
|
|
try:
|
2022-05-24 22:48:27 +00:00
|
|
|
for server in servers:
|
2022-05-30 20:05:36 +00:00
|
|
|
latest = self.get_latest_server_stats()
|
2022-04-27 19:07:34 +00:00
|
|
|
server_data.append(
|
|
|
|
{
|
2022-05-24 22:48:27 +00:00
|
|
|
"server_data": server,
|
2022-05-30 20:05:36 +00:00
|
|
|
"stats": latest,
|
2022-04-27 19:07:34 +00:00
|
|
|
"user_command_permission": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
except IndexError as ex:
|
|
|
|
logger.error(
|
|
|
|
f"Stats collection failed with error: {ex}. Was a server just created?"
|
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
return server_data
|
|
|
|
|
2023-09-19 19:46:31 +00:00
|
|
|
def get_history_stats(self, server_id, num_hours):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2023-09-19 19:46:31 +00:00
|
|
|
max_age = datetime.datetime.now() - timedelta(hours=num_hours)
|
2023-08-27 11:50:22 +00:00
|
|
|
query_stats = (
|
2022-08-21 21:04:23 +00:00
|
|
|
ServerStats.select()
|
|
|
|
.where(ServerStats.created > max_age)
|
|
|
|
.where(ServerStats.server_id == server_id)
|
2023-08-27 12:31:51 +00:00
|
|
|
# .order_by(ServerStats.created.desc())
|
2022-08-21 21:04:23 +00:00
|
|
|
.execute(self.database)
|
|
|
|
)
|
2023-08-27 11:50:22 +00:00
|
|
|
server_stats = []
|
|
|
|
for stat in query_stats:
|
|
|
|
server_stats.append(DatabaseShortcuts.get_data_obj(stat))
|
|
|
|
self.database.close()
|
2022-10-15 08:13:17 +00:00
|
|
|
return server_stats
|
2022-08-21 21:04:23 +00:00
|
|
|
|
2022-06-01 21:52:49 +00:00
|
|
|
def insert_server_stats(self, server_stats):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-05-25 13:15:51 +00:00
|
|
|
server_id = server_stats.get("id", 0)
|
2022-04-27 19:07:34 +00:00
|
|
|
|
|
|
|
if server_id == 0:
|
|
|
|
logger.warning("Stats saving failed with error: Server unknown (id = 0)")
|
|
|
|
return
|
|
|
|
|
|
|
|
ServerStats.insert(
|
|
|
|
{
|
2022-05-25 13:15:51 +00:00
|
|
|
ServerStats.server_id: server_stats.get("id", 0),
|
|
|
|
ServerStats.started: server_stats.get("started", ""),
|
|
|
|
ServerStats.running: server_stats.get("running", False),
|
|
|
|
ServerStats.cpu: server_stats.get("cpu", 0),
|
|
|
|
ServerStats.mem: server_stats.get("mem", 0),
|
|
|
|
ServerStats.mem_percent: server_stats.get("mem_percent", 0),
|
|
|
|
ServerStats.world_name: server_stats.get("world_name", ""),
|
|
|
|
ServerStats.world_size: server_stats.get("world_size", ""),
|
2022-06-01 19:35:11 +00:00
|
|
|
ServerStats.server_port: server_stats.get("server_port", 0),
|
2022-05-25 13:15:51 +00:00
|
|
|
ServerStats.int_ping_results: server_stats.get(
|
|
|
|
"int_ping_results", False
|
|
|
|
),
|
|
|
|
ServerStats.online: server_stats.get("online", False),
|
|
|
|
ServerStats.max: server_stats.get("max", False),
|
|
|
|
ServerStats.players: server_stats.get("players", False),
|
|
|
|
ServerStats.desc: server_stats.get("desc", False),
|
2023-09-02 19:51:28 +00:00
|
|
|
ServerStats.icon: server_stats.get("icon", None),
|
2022-05-25 13:15:51 +00:00
|
|
|
ServerStats.version: server_stats.get("version", False),
|
2022-04-27 19:07:34 +00:00
|
|
|
}
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def remove_old_stats(self, last_week):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-05-25 13:54:35 +00:00
|
|
|
# self.select_database(self.server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
ServerStats.delete().where(ServerStats.created < last_week).execute(
|
|
|
|
self.database
|
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def get_latest_server_stats(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-05-26 12:50:20 +00:00
|
|
|
latest = (
|
2022-04-27 19:07:34 +00:00
|
|
|
ServerStats.select()
|
2022-05-25 13:54:35 +00:00
|
|
|
.where(ServerStats.server_id == self.server_id)
|
2022-04-27 19:07:34 +00:00
|
|
|
.order_by(ServerStats.created.desc())
|
|
|
|
.limit(1)
|
2022-05-26 12:50:20 +00:00
|
|
|
.get(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
|
|
|
|
self.database.close()
|
2022-05-26 12:50:20 +00:00
|
|
|
try:
|
2022-05-26 21:19:59 +00:00
|
|
|
return DatabaseShortcuts.get_data_obj(latest)
|
2022-05-26 12:50:20 +00:00
|
|
|
except IndexError:
|
|
|
|
return {}
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-26 12:50:20 +00:00
|
|
|
def get_server_stats(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
stats = (
|
|
|
|
ServerStats.select()
|
2022-05-25 13:54:35 +00:00
|
|
|
.where(ServerStats.server_id == self.server_id)
|
2022-04-27 19:07:34 +00:00
|
|
|
.order_by(ServerStats.created.desc())
|
|
|
|
.limit(1)
|
2022-05-26 13:39:52 +00:00
|
|
|
.first(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-05-26 21:19:59 +00:00
|
|
|
return DatabaseShortcuts.get_data_obj(stats)
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def server_id_exists(self):
|
|
|
|
if not HelperServers.get_server_data_by_id(self.server_id):
|
2022-04-27 19:07:34 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def sever_crashed(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-05-21 18:26:58 +00:00
|
|
|
ServerStats.update(crashed=True).where(
|
2022-05-25 13:54:35 +00:00
|
|
|
ServerStats.server_id == self.server_id
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-08-17 18:48:05 +00:00
|
|
|
def set_import(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-08-17 18:48:05 +00:00
|
|
|
ServerStats.update(importing=True).where(
|
2022-05-25 13:54:35 +00:00
|
|
|
ServerStats.server_id == self.server_id
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-08-17 18:48:05 +00:00
|
|
|
def finish_import(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-08-17 18:48:05 +00:00
|
|
|
ServerStats.update(importing=False).where(
|
2022-05-25 13:54:35 +00:00
|
|
|
ServerStats.server_id == self.server_id
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-08-17 18:48:05 +00:00
|
|
|
def get_import_status(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-08-17 18:48:05 +00:00
|
|
|
import_status = (
|
2022-05-26 12:50:20 +00:00
|
|
|
ServerStats.select()
|
|
|
|
.where(ServerStats.server_id == self.server_id)
|
|
|
|
.get(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-08-17 18:48:05 +00:00
|
|
|
return import_status.importing
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def server_crash_reset(self):
|
|
|
|
if self.server_id is None:
|
2022-04-27 19:07:34 +00:00
|
|
|
return
|
|
|
|
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-05-21 18:26:58 +00:00
|
|
|
ServerStats.update(crashed=False).where(
|
2022-05-25 13:54:35 +00:00
|
|
|
ServerStats.server_id == self.server_id
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def is_crashed(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-05-26 12:50:20 +00:00
|
|
|
svr: ServerStats = (
|
|
|
|
ServerStats.select()
|
|
|
|
.where(ServerStats.server_id == self.server_id)
|
|
|
|
.get(self.database)
|
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-05-24 22:48:27 +00:00
|
|
|
return svr.crashed
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def set_update(self, value):
|
|
|
|
if self.server_id is None:
|
2022-04-27 19:07:34 +00:00
|
|
|
return
|
2022-10-15 08:13:17 +00:00
|
|
|
|
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
try:
|
|
|
|
# Checks if server even exists
|
2022-05-26 12:50:20 +00:00
|
|
|
ServerStats.select().where(ServerStats.server_id == self.server_id).execute(
|
|
|
|
self.database
|
|
|
|
)
|
2022-05-24 22:48:27 +00:00
|
|
|
except DoesNotExist as ex:
|
2022-04-27 19:07:34 +00:00
|
|
|
logger.error(f"Database entry not found! {ex}")
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-05-24 22:48:27 +00:00
|
|
|
return
|
2022-10-15 08:13:17 +00:00
|
|
|
|
2022-05-21 18:26:58 +00:00
|
|
|
ServerStats.update(updating=value).where(
|
2022-05-25 13:54:35 +00:00
|
|
|
ServerStats.server_id == self.server_id
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def get_update_status(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
update_status = (
|
2022-05-26 12:50:20 +00:00
|
|
|
ServerStats.select()
|
|
|
|
.where(ServerStats.server_id == self.server_id)
|
|
|
|
.get(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
return update_status.updating
|
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def set_first_run(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
# Sets first run to false
|
|
|
|
try:
|
|
|
|
# Checks if server even exists
|
2022-05-26 12:50:20 +00:00
|
|
|
ServerStats.select().where(ServerStats.server_id == self.server_id).execute(
|
|
|
|
self.database
|
|
|
|
)
|
2022-04-27 19:07:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
logger.error(f"Database entry not found! {ex}")
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
return
|
2022-05-21 18:26:58 +00:00
|
|
|
ServerStats.update(first_run=False).where(
|
2022-05-25 13:54:35 +00:00
|
|
|
ServerStats.server_id == self.server_id
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def get_first_run(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-05-25 13:54:35 +00:00
|
|
|
first_run = (
|
2022-05-26 12:50:20 +00:00
|
|
|
ServerStats.select()
|
|
|
|
.where(ServerStats.server_id == self.server_id)
|
|
|
|
.get(self.database)
|
2022-05-25 13:54:35 +00:00
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
return first_run.first_run
|
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def get_ttl_without_player(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
last_stat = (
|
|
|
|
ServerStats.select()
|
2022-05-25 13:54:35 +00:00
|
|
|
.where(ServerStats.server_id == self.server_id)
|
2022-04-27 19:07:34 +00:00
|
|
|
.order_by(ServerStats.created.desc())
|
2022-05-26 12:50:20 +00:00
|
|
|
.first(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
|
|
|
last_stat_with_player = (
|
|
|
|
ServerStats.select()
|
2022-05-25 13:54:35 +00:00
|
|
|
.where(ServerStats.server_id == self.server_id)
|
2022-04-27 19:07:34 +00:00
|
|
|
.where(ServerStats.online > 0)
|
|
|
|
.order_by(ServerStats.created.desc())
|
2022-05-26 12:50:20 +00:00
|
|
|
.first(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
return last_stat.created - last_stat_with_player.created
|
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def can_stop_no_players(self, time_limit):
|
|
|
|
ttl_no_players = self.get_ttl_without_player()
|
2022-05-24 22:48:27 +00:00
|
|
|
return (time_limit == -1) or (ttl_no_players > time_limit)
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def set_waiting_start(self, value):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
try:
|
|
|
|
# Checks if server even exists
|
2022-05-26 13:39:52 +00:00
|
|
|
ServerStats.select().where(ServerStats.server_id == self.server_id).execute(
|
|
|
|
self.database
|
2022-05-24 22:48:27 +00:00
|
|
|
)
|
|
|
|
except DoesNotExist as ex:
|
2022-04-27 19:07:34 +00:00
|
|
|
logger.error(f"Database entry not found! {ex}")
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-05-24 22:48:27 +00:00
|
|
|
return
|
2022-05-21 18:26:58 +00:00
|
|
|
ServerStats.update(waiting_start=value).where(
|
2022-05-25 13:54:35 +00:00
|
|
|
ServerStats.server_id == self.server_id
|
2022-05-26 12:50:20 +00:00
|
|
|
).execute(self.database)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
|
2022-05-25 13:54:35 +00:00
|
|
|
def get_waiting_start(self):
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.connect(reuse_if_open=True)
|
2022-04-27 19:07:34 +00:00
|
|
|
waiting_start = (
|
2022-05-26 12:50:20 +00:00
|
|
|
ServerStats.select()
|
|
|
|
.where(ServerStats.server_id == self.server_id)
|
|
|
|
.get(self.database)
|
2022-04-27 19:07:34 +00:00
|
|
|
)
|
2022-10-15 08:13:17 +00:00
|
|
|
self.database.close()
|
2022-04-27 19:07:34 +00:00
|
|
|
return waiting_start.waiting_start
|