mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Host stats are now realtime(d) by websockets
This commit is contained in:
parent
7e0f592330
commit
2595e6950d
@ -10,6 +10,7 @@ logger = logging.getLogger(__name__)
|
||||
from app.classes.shared.console import console
|
||||
from app.classes.shared.helpers import helper
|
||||
from app.classes.shared.tasks import tasks_manager
|
||||
from app.classes.web.websocket_helper import websocket_helper
|
||||
|
||||
try:
|
||||
import requests
|
||||
@ -43,7 +44,9 @@ class MainPrompt(cmd.Cmd):
|
||||
def do_exit(self, line):
|
||||
logger.info("Stopping all server daemons / threads")
|
||||
console.info("Stopping all server daemons / threads - This may take a few seconds")
|
||||
websocket_helper.disconnect_all()
|
||||
self._clean_shutdown()
|
||||
console.info('Waiting for main thread to stop')
|
||||
while True:
|
||||
if tasks_manager.get_main_thread_run_status():
|
||||
sys.exit(0)
|
||||
|
@ -4,11 +4,12 @@ import json
|
||||
import time
|
||||
import logging
|
||||
import threading
|
||||
import asyncio
|
||||
|
||||
from app.classes.shared.helpers import helper
|
||||
from app.classes.shared.console import console
|
||||
from app.classes.web.tornado import webserver
|
||||
from app.classes.web.websocket_handler import WebSocketHandler
|
||||
from app.classes.web.websocket_helper import websocket_helper
|
||||
|
||||
from app.classes.minecraft.stats import stats
|
||||
from app.classes.shared.controller import controller
|
||||
@ -40,7 +41,7 @@ class TasksManager:
|
||||
self.command_thread = threading.Thread(target=self.command_watcher, daemon=True, name="command_watcher")
|
||||
self.command_thread.start()
|
||||
|
||||
self.realtime_thread = threading.Thread(target=self.realtime_thread, daemon=True, name="realtime")
|
||||
self.realtime_thread = threading.Thread(target=self.realtime, daemon=True, name="realtime")
|
||||
self.realtime_thread.start()
|
||||
|
||||
def get_main_thread_run_status(self):
|
||||
@ -137,8 +138,40 @@ class TasksManager:
|
||||
schedule.every(12).hours.do(server_jar_obj.refresh_cache)
|
||||
|
||||
@staticmethod
|
||||
def realtime_thread():
|
||||
console.debug('realtime zero')
|
||||
def realtime():
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
console.debug('realtime start')
|
||||
host_stats = db_helper.get_latest_hosts_stats()
|
||||
|
||||
while True:
|
||||
|
||||
if host_stats.get('cpu_usage') != \
|
||||
db_helper.get_latest_hosts_stats().get('cpu_usage') or \
|
||||
host_stats.get('mem_percent') != \
|
||||
db_helper.get_latest_hosts_stats().get('mem_percent'):
|
||||
# Stats are different
|
||||
|
||||
console.debug('realtime 1')
|
||||
host_stats = db_helper.get_latest_hosts_stats()
|
||||
if len(websocket_helper.clients) > 0:
|
||||
# There are clients
|
||||
console.debug('realtime 11')
|
||||
websocket_helper.broadcast('update_host_stats', {
|
||||
'cpu_usage': host_stats.get('cpu_usage'),
|
||||
'cpu_cores': host_stats.get('cpu_cores'),
|
||||
'cpu_cur_freq': host_stats.get('cpu_cur_freq'),
|
||||
'cpu_max_freq': host_stats.get('cpu_max_freq'),
|
||||
'mem_percent': host_stats.get('mem_percent'),
|
||||
'mem_usage': host_stats.get('mem_usage')
|
||||
})
|
||||
time.sleep(4)
|
||||
else:
|
||||
# Stats are same
|
||||
console.debug('realtime 0')
|
||||
time.sleep(8)
|
||||
|
||||
|
||||
|
||||
tasks_manager = TasksManager()
|
||||
|
@ -2,37 +2,23 @@ import json
|
||||
|
||||
import tornado.websocket
|
||||
from app.classes.shared.console import console
|
||||
from app.classes.shared.models import db_helper
|
||||
from app.classes.web.websocket_helper import websocket_helper
|
||||
|
||||
|
||||
class WebSocketHandler(tornado.websocket.WebSocketHandler):
|
||||
connections = set()
|
||||
|
||||
def open(self):
|
||||
self.connections.add(self)
|
||||
websocket_helper.addClient(self)
|
||||
console.debug('Opened WebSocket connection')
|
||||
self.broadcast('client_joined', {
|
||||
'foo': 'bar',
|
||||
})
|
||||
websocket_helper.broadcast('client_joined', {})
|
||||
|
||||
def on_message(self, rawMessage):
|
||||
# broadcast
|
||||
# for client in self.connections:
|
||||
# client.write_message(message)
|
||||
|
||||
# send message to client this message was sent by
|
||||
# self.write_message
|
||||
|
||||
console.debug('Got message from WebSocket connection {}'.format(rawMessage))
|
||||
message = json.loads(rawMessage)
|
||||
console.debug('Type: {}, Data: {}'.format(message['type'], message['data']))
|
||||
|
||||
def on_close(self):
|
||||
self.connections.remove(self)
|
||||
websocket_helper.removeClient(self)
|
||||
console.debug('Closed WebSocket connection')
|
||||
|
||||
def broadcast(self, message_type: str, data):
|
||||
console.debug('Sending: ' + str(json.dumps({'type': message_type, 'data': data})))
|
||||
message = str(json.dumps({'type': message_type, 'data': data}))
|
||||
for client in self.connections:
|
||||
client.write_message(message)
|
||||
|
29
app/classes/web/websocket_helper.py
Normal file
29
app/classes/web/websocket_helper.py
Normal file
@ -0,0 +1,29 @@
|
||||
import json
|
||||
|
||||
from app.classes.shared.console import console
|
||||
|
||||
class WebSocketHelper:
|
||||
clients = set()
|
||||
|
||||
def addClient(self, client):
|
||||
self.clients.add(client)
|
||||
|
||||
def removeClient(self, client):
|
||||
self.clients.add(client)
|
||||
|
||||
def broadcast(self, message_type: str, data):
|
||||
console.debug('Sending: ' + str(json.dumps({'type': message_type, 'data': data})))
|
||||
message = str(json.dumps({'type': message_type, 'data': data}))
|
||||
for client in self.clients:
|
||||
try:
|
||||
client.write_message(message)
|
||||
except:
|
||||
pass
|
||||
|
||||
def disconnect_all(self):
|
||||
console.info('Disconnecting WebSocket clients')
|
||||
for client in self.clients:
|
||||
client.close()
|
||||
console.info('Disconnected WebSocket clients')
|
||||
|
||||
websocket_helper = WebSocketHelper()
|
@ -34,12 +34,11 @@
|
||||
|
||||
</div>
|
||||
<div class="wrapper my-auto ml-auto ml-lg-4">
|
||||
<p class="mb-0 text-success" data-toggle="tooltip" data-placement="top" data-html="true" title="CPU Cores: {{ data.get('hosts_data').get('cpu_cores') }} <br /> CPU Cur Freq: {{ data.get('hosts_data').get('cpu_cur_freq') }} <br /> CPU Max Freq: {{ data.get('hosts_data').get('cpu_max_freq') }}" >
|
||||
{{ data.get('hosts_data').get('cpu_usage') }} {{ _('CPU Usage') }}
|
||||
|
||||
<p id="cpu_data" class="mb-0 text-success" data-toggle="tooltip" data-placement="top" data-html="true" title="CPU Cores: {{ data.get('hosts_data').get('cpu_cores') }} <br /> CPU Cur Freq: {{ data.get('hosts_data').get('cpu_cur_freq') }} <br /> CPU Max Freq: {{ data.get('hosts_data').get('cpu_max_freq') }}" >
|
||||
<span id="cpu_usage">{{ data.get('hosts_data').get('cpu_usage') }}</span> {{ _('CPU Usage') }}
|
||||
</p>
|
||||
<p class="mb-0 text-danger" data-toggle="tooltip" data-placement="top" title="Memory Usage: {{ data.get('hosts_data').get('mem_usage') }}" >
|
||||
{{ data.get('hosts_data').get('mem_percent') }}% {{ _('Memory Usage') }}
|
||||
<p id="mem_usage" class="mb-0 text-danger" data-toggle="tooltip" data-placement="top" title="Memory Usage: {{ data.get('hosts_data').get('mem_usage') }}" >
|
||||
<span id="mem_percent">{{ data.get('hosts_data').get('mem_percent') }}%</span> {{ _('Memory Usage') }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
@ -268,6 +267,20 @@ $( document ).ready(function() {
|
||||
});
|
||||
});
|
||||
|
||||
cpu_data = document.getElementById('cpu_data');
|
||||
cpu_usage = document.getElementById('cpu_usage');
|
||||
mem_usage = document.getElementById('mem_usage');
|
||||
mem_percent = document.getElementById('mem_percent');
|
||||
|
||||
webSocket.on('update_host_stats', function (hostStats) {
|
||||
var cpuDataTitle = `CPU Cores: ${hostStats.cpu_cores} <br /> CPU Cur Freq: ${hostStats.cpu_cur_freq} <br /> CPU Max Freq: ${hostStats.cpu_max_freq}`;
|
||||
cpu_data.setAttribute('data-original-title', cpuDataTitle);
|
||||
cpu_usage.textContent = hostStats.cpu_usage;
|
||||
mem_usage.setAttribute('data-original-title', `Memory Usage: ${hostStats.mem_usage}`);
|
||||
mem_percent.textContent = hostStats.mem_percent + '%';
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user