populating dashboard with real data

This commit is contained in:
Phillip Tarrant 2020-08-24 13:08:17 -04:00
parent 632a791de3
commit a958b6dea0
7 changed files with 109 additions and 141 deletions

View File

@ -29,7 +29,24 @@ class Controller:
servers = db_helper.get_all_defined_servers()
for s in servers:
# if this server path no longer exists - let's warn and bomb out
if not helper.check_path_exits(s['path']):
logger.warning("Unable to find server {} at path {}. Skipping this server".format(s['server_name'],
s['path']))
console.warning("Unable to find server {} at path {}. Skipping this server".format(s['server_name'],
s['path']))
continue
settings_file = os.path.join(s['path'], 'server.properties')
# if the properties file isn't there, let's warn
if not helper.check_file_exists(settings_file):
logger.error("Unable to find {}. Skipping this server.".format(settings_file))
console.error("Unable to find {}. Skipping this server.".format(settings_file))
continue
settings = ServerProps(settings_file)
temp_server_dict = {

View File

@ -10,7 +10,7 @@ import datetime
from app.classes.shared.helpers import helper
from app.classes.minecraft.mc_ping import ping
from app.classes.minecraft.controller import controller
from app.classes.shared.models import Host_Stats
from app.classes.shared.models import Host_Stats, Server_Stats
logger = logging.getLogger(__name__)
@ -204,10 +204,10 @@ class Stats:
return server_stats_list
# todo: add stats to db
def record_stats(self):
stats_to_send = self.get_node_stats()
node_stats = stats_to_send.get('node_stats')
Host_Stats.insert({
Host_Stats.boot_time: node_stats.get('boot_time', "Unknown"),
Host_Stats.cpu_usage: round(node_stats.get('cpu_usage', 0), 2),
@ -220,10 +220,27 @@ class Stats:
Host_Stats.disk_json: node_stats.get('disk_data', '{}')
}).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', ""),
Server_Stats.cpu: server.get('cpu', ""),
Server_Stats.mem: server.get('mem', ""),
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', ""),
}).execute()
# delete 1 week old data
max_age = int(helper.get_setting("CRAFTY", "history_max_age"))
now = datetime.datetime.now()
last_week = now.day - max_age
Host_Stats.delete().where(Host_Stats.time < last_week).execute()
Server_Stats.delete().where(Server_Stats.created < last_week).execute()
stats = Stats()

View File

@ -75,11 +75,27 @@ class Servers(BaseModel):
stop_command = CharField(default="stop")
server_port = IntegerField(default=25565)
class Meta:
table_name = "servers"
class Server_Stats(BaseModel):
stats_id = AutoField()
created = DateTimeField(default=datetime.datetime.now)
server_id = ForeignKeyField(Servers, backref='server')
started = CharField(default="")
running = BooleanField(default=False)
cpu = FloatField(default=0)
mem = FloatField(default=0)
world_name = CharField(default="")
world_size = CharField(default="")
server_port = IntegerField(default=25565)
int_ping_results = CharField(default="")
class Meta:
table_name = "server_stats"
class Webhooks(BaseModel):
id = AutoField()
name = CharField(max_length=64, unique=True)
@ -113,6 +129,7 @@ class db_builder:
Host_Stats,
Webhooks,
Servers,
Server_Stats
])
@staticmethod
@ -156,6 +173,10 @@ class db_shortcuts:
query = Servers.select()
return self.return_rows(query)
def get_all_servers_stats(self):
query = Server_Stats.select().order_by(Server_Stats.stats_id.desc()).limit(1)
return self.return_rows(query)
def get_latest_hosts_stats(self):
query = Host_Stats.select().order_by(Host_Stats.id.desc()).get()
return model_to_dict(query)

View File

@ -10,7 +10,6 @@ from app.classes.web.base_handler import BaseHandler
from app.classes.minecraft.controller import controller
from app.classes.shared.models import db_helper
logger = logging.getLogger(__name__)
@ -23,15 +22,17 @@ class PanelHandler(BaseHandler):
template = "panel/denied.html"
defined_servers = controller.list_defined_servers()
page_data = {
'version_data': "version_data_here",
'user_data': user_data,
'server_stats': {
'total': len(controller.list_defined_servers()),
'total': len(defined_servers),
'running': len(controller.list_running_servers()),
'stopped': (len(controller.list_defined_servers()) - len(controller.list_running_servers()))
},
'menu_servers': defined_servers,
'hosts_data': db_helper.get_latest_hosts_stats()
}
@ -46,9 +47,11 @@ class PanelHandler(BaseHandler):
template = "panel/denied.html"
elif page == 'dashboard':
page_data['servers'] = db_helper.get_all_servers_stats()
template = "panel/dashboard.html"
self.render(
template,
data=page_data
)
)

View File

@ -31,7 +31,6 @@ class ServerHandler(BaseHandler):
template = "public/404.html"
page_data = {
'version_data': "version_data_here",
'user_data': user_data,

View File

@ -21,21 +21,12 @@
</a>
<div class="collapse" id="page-layouts">
<ul class="nav flex-column sub-menu">
<li class="nav-item">
<a class="nav-link" href="pages/layout/boxed-layout.html">Bungee Proxy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="pages/layout/rtl-layout.html">Lobby</a>
</li>
<li class="nav-item">
<a class="nav-link" href="pages/layout/rtl-layout.html">Bedwarz</a>
</li>
<li class="nav-item">
<a class="nav-link" href="pages/layout/rtl-layout.html">Creative</a>
</li>
<li class="nav-item">
<a class="nav-link" href="pages/layout/rtl-layout.html">Survival</a>
</li>
{% for s in data['menu_servers'] %}
<li class="nav-item">
<a class="nav-link" href="/panel/server_detail?id={{s['server_id']}}">{{s['server_name']}}</a>
</li>
{% end %}
</ul>
</div>
</li>

View File

@ -44,8 +44,8 @@
</div>
<div class="wrapper my-auto ml-auto ml-lg-4">
<p class="mb-0 text-success">{{ data['server_stats']['total'] }} {{_('Online')}}</p>
<p class="mb-0 text-warning"> {{ data['server_stats']['running'] }} {{_('Shutdown')}}</p>
<p class="mb-0 text-success">{{ data['server_stats']['running'] }} {{_('Online')}}</p>
<p class="mb-0 text-warning"> {{ data['server_stats']['stopped'] }} {{_('Shutdown')}}</p>
</div>
</div>
</div>
@ -94,144 +94,62 @@
<thead>
<tr class="rounded">
<th>Server</th>
<th>Actions</th>
<th>CPU</th>
<th>Memory</th>
<th>Disk</th>
<th>World</th>
<th>Players</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for server in data['servers'] %}
<tr>
<td>
<i class="fas fa-server"></i> Bedwarz </td>
<i class="fas fa-server"></i>
<a href="/panel/server_detail?id={{s['server_id']}}">
{{ server['server_id']['server_name'] }}
</a>
</td>
<td>
{% if server['started'] == True %}
<a hidden data-id="server['server_id']['server_id']" class="play_button hidden"><i class="fas fa-play"></i></a> &nbsp;
<a data-id="server['server_id']['server_id']" class="stop_button"><i class="fas fa-stop"></i></a> &nbsp;
<a data-id="server['server_id']['server_id']" class="restart_button"><i class="fas fa-sync"></i></a> &nbsp;
{% else %}
<a data-id="server['server_id']['server_id']" class="play_button"><i class="fas fa-play"></i></a> &nbsp;
<a hidden data-id="server['server_id']['server_id']" class="stop_button hidden"><i class="fas fa-stop"></i></a> &nbsp;
<a hidden data-id="server['server_id']['server_id']" class="restart_button hidden"><i class="fas fa-sync"></i></a> &nbsp;
{% end %}
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 24%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
<div class="progress-bar bg-success" role="progressbar" style="width: {{server['cpu']}}%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="{{server['mem']}}%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
{{ server['world_name'] }} : {{ server['world_size'] }}
</td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 75%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
7
{{ server['int_ping_results'] }}
</td>
<td>
{% if server['started'] == True %}
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
{% else %}
<i class="fas fa-thumbs-down"></i> <span class="text-danger">Offline</span>
{% end %}
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Lobby </td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 60%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
3
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Bedwarz </td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 10%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 12%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
0
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Creative </td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 60%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
5
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
<tr>
<td>
<i class="fas fa-server"></i> Survival </td>
<td>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 15%" aria-valuenow="15" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-danger" role="progressbar" style="width: 85%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 40%" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</td>
<td>
3
</td>
<td>
<i class="fas fa-thumbs-up"></i> <span class="text-success">Online</span>
</td>
</tr>
{% end %}
</tbody>
</table>
@ -241,6 +159,8 @@
</div>
</div>
</div>
<!-- content-wrapper ends -->