mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Adding API route for history stats
Fixing close db connexion for stats
This commit is contained in:
parent
79449082ff
commit
29f7399708
@ -144,13 +144,17 @@ class HelperServerStats:
|
||||
def get_history_stats(self, server_id, num_days):
|
||||
self.database.connect(reuse_if_open=True)
|
||||
max_age = datetime.datetime.now() - timedelta(days=num_days)
|
||||
server_stats = (
|
||||
query_stats = (
|
||||
ServerStats.select()
|
||||
.where(ServerStats.created > max_age)
|
||||
.where(ServerStats.server_id == server_id)
|
||||
#.order_by(ServerStats.created.desc())
|
||||
.execute(self.database)
|
||||
)
|
||||
self.database.connect(reuse_if_open=True)
|
||||
server_stats = []
|
||||
for stat in query_stats:
|
||||
server_stats.append(DatabaseShortcuts.get_data_obj(stat))
|
||||
self.database.close()
|
||||
return server_stats
|
||||
|
||||
def insert_server_stats(self, server_stats):
|
||||
|
@ -33,7 +33,6 @@ from app.classes.shared.helpers import Helpers
|
||||
from app.classes.shared.file_helpers import FileHelpers
|
||||
from app.classes.shared.null_writer import NullWriter
|
||||
from app.classes.shared.websocket_manager import WebSocketManager
|
||||
from app.classes.shared.websocket_manager import WebSocketManager
|
||||
|
||||
with redirect_stderr(NullWriter()):
|
||||
import psutil
|
||||
@ -1805,3 +1804,7 @@ class ServerInstance:
|
||||
minimum_to_exist = now - datetime.timedelta(days=max_age)
|
||||
|
||||
self.stats_helper.remove_old_stats(minimum_to_exist)
|
||||
|
||||
def get_server_history(self):
|
||||
history = self.stats_helper.get_history_stats(self.server_id, 3)
|
||||
return history
|
@ -22,6 +22,7 @@ from app.classes.web.routes.api.servers.server.public import (
|
||||
ApiServersServerPublicHandler,
|
||||
)
|
||||
from app.classes.web.routes.api.servers.server.stats import ApiServersServerStatsHandler
|
||||
from app.classes.web.routes.api.servers.server.history import ApiServersServerHistoryHandler
|
||||
from app.classes.web.routes.api.servers.server.stdin import ApiServersServerStdinHandler
|
||||
from app.classes.web.routes.api.servers.server.tasks.index import (
|
||||
ApiServersServerTasksIndexHandler,
|
||||
@ -132,6 +133,11 @@ def api_handlers(handler_args):
|
||||
ApiServersServerStatsHandler,
|
||||
handler_args,
|
||||
),
|
||||
(
|
||||
r"/api/v2/servers/([0-9]+)/history/?",
|
||||
ApiServersServerHistoryHandler,
|
||||
handler_args,
|
||||
),
|
||||
(
|
||||
r"/api/v2/servers/([0-9]+)/action/([a-z_]+)/?",
|
||||
ApiServersServerActionHandler,
|
||||
|
28
app/classes/web/routes/api/servers/server/history.py
Normal file
28
app/classes/web/routes/api/servers/server/history.py
Normal file
@ -0,0 +1,28 @@
|
||||
import logging
|
||||
from app.classes.web.base_api_handler import BaseApiHandler
|
||||
from app.classes.controllers.servers_controller import ServersController
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ApiServersServerHistoryHandler(BaseApiHandler):
|
||||
def get(self, server_id: str):
|
||||
auth_data = self.authenticate_user()
|
||||
if not auth_data:
|
||||
return
|
||||
|
||||
if server_id not in [str(x["server_id"]) for x in auth_data[0]]:
|
||||
# if the user doesn't have access to the server, return an error
|
||||
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
|
||||
|
||||
srv = ServersController().get_server_instance_by_id(server_id)
|
||||
history = srv.get_server_history()
|
||||
|
||||
self.finish_json(
|
||||
200,
|
||||
{
|
||||
"status": "ok",
|
||||
"data": history,
|
||||
},
|
||||
)
|
@ -89,11 +89,11 @@
|
||||
const cpu = []
|
||||
{% for item in data['history_stats'] %}
|
||||
{% if 'minecraft-java' in data['server_stats']['server_type'] %}
|
||||
players.push("{{ item.online }}");
|
||||
players.push("{{ item.get('online') }}");
|
||||
{% end %}
|
||||
dates.push("{{ item.created.strftime('%Y/%m/%d, %H:%M:%S') }}");
|
||||
ram.push("{{ item.mem_percent }}")
|
||||
cpu.push("{{ item.cpu }}")
|
||||
dates.push("{{ item.get('created').strftime('%Y/%m/%d, %H:%M:%S') }}");
|
||||
ram.push("{{ item.get('mem_percent') }}")
|
||||
cpu.push("{{ item.get('cpu') }}")
|
||||
{% end %}
|
||||
var hist_chart = new Chart(ctxL, {
|
||||
type: 'line',
|
||||
|
Loading…
Reference in New Issue
Block a user