支持显示网速

This commit is contained in:
Jerry Yan 2019-04-09 13:48:46 +08:00
parent a6662450de
commit a6fa88b981
8 changed files with 140 additions and 83 deletions

View File

@ -1,6 +1,6 @@
import queue
from datetime import datetime
import psutil
from api import XiGuaLiveApi
import json
@ -9,6 +9,62 @@ config = json.load(_config_fp)
_config_fp.close()
network = {
"currentTime": datetime.now(),
"out":{
"currentByte":psutil.net_io_counters().bytes_sent,
},
"in":{
"currentByte": psutil.net_io_counters().bytes_recv,
}
}
def updateNetwork():
global network
network = {
"currentTime": datetime.now(),
"out":{
"currentByte":psutil.net_io_counters().bytes_sent,
},
"in":{
"currentByte": psutil.net_io_counters().bytes_recv,
}
}
def getTimeDelta(a, b):
sec = (a - b).seconds
ms = (a - b).microseconds
return sec+(ms/100000.0)
def getCurrentStatus():
_disk = psutil.disk_usage("/")
_mem = psutil.virtual_memory()
_net = psutil.net_io_counters()
if 60 > (datetime.now() - network["currentTime"]).seconds > 0:
_outSpeed = (_net.bytes_sent - network["out"]["currentByte"])/getTimeDelta(datetime.now(),network["currentTime"])
else:
_outSpeed = 0
if 60 > (datetime.now() - network["currentTime"]).seconds > 0:
_inSpeed = (_net.bytes_recv - network["in"]["currentByte"])/getTimeDelta(datetime.now(),network["currentTime"])
else:
_inSpeed = 0
updateNetwork()
return {
"memTotal": parseSize(_mem.total),
"memUsed": parseSize(_mem.used),
"memUsage": _mem.percent,
"diskTotal": parseSize(_disk.total),
"diskUsed": parseSize(_disk.used),
"diskUsage": _disk.percent,
"cpu": psutil.cpu_percent(),
"outSpeed": parseSize(_outSpeed),
"inSpeed": parseSize(_inSpeed),
}
def reloadConfig():
global config, _config_fp
_config_fp = open("config.json", "r", encoding="utf8")
@ -46,7 +102,7 @@ def parseSize(size):
else:
return "{:.2f}MB".format(M)
else:
return "{:.2f}MB".format(K)
return "{:.2f}KB".format(K)
def appendUploadStatus(obj):

View File

@ -6,7 +6,6 @@ from flask import Flask, jsonify, request, redirect, render_template, Response
import Common
import threading
from liveDownloader import run as RUN
import psutil
app = Flask("liveStatus")
app.config['JSON_AS_ASCII'] = False
@ -17,7 +16,8 @@ CORS(app, supports_credentials=True)
@app.route("/")
def index():
return redirect("/static/index.html")
return render_template("index.html")
@app.route("/config", methods=["GET"])
def readConfig():
@ -90,7 +90,6 @@ def finishUpload():
@app.route("/stats", methods=["GET"])
def getAllStats():
_disk = psutil.disk_usage("/")
return jsonify({"message":"ok","code":200,"status":0,"data":{
"download":Common.downloadStatus,
"encode": Common.encodeStatus,
@ -115,18 +114,8 @@ def getAllStats():
@app.route("/stats/device", methods=["GET"])
def getDeviceStatus():
_disk = psutil.disk_usage("/")
_mem = psutil.virtual_memory()
return jsonify({"message":"ok","code":200,"status":0,"data":{
"status": {
"memTotal": Common.parseSize(_mem.total),
"memUsed": Common.parseSize(_mem.used),
"memUsage": _mem.percent,
"diskTotal": Common.parseSize(_disk.total),
"diskUsed": Common.parseSize(_disk.used),
"diskUsage": _disk.percent,
"cpu": psutil.cpu_percent(),
},
"status": Common.getCurrentStatus(),
}})
@ -149,6 +138,7 @@ def getConfigStats():
"forceNotBroadcasting": Common.forceNotBroadcasting,
"forceNotDownload": Common.forceNotDownload,
"forceNotUpload": Common.forceNotUpload,
"forceNotEncode": Common.forceNotEncode,
}
}})
@ -212,6 +202,6 @@ def SubThread():
t.start()
p = threading.Thread(target = SubThread)
p.setDaemon(True)
p.start()
# p = threading.Thread(target=SubThread)
# p.setDaemon(True)
# p.start()

24
static/device.js Normal file
View File

@ -0,0 +1,24 @@
function deviceUpdate(){
$.ajax(
"/stats/device",
{
success: function (res){
$("#memTotal").text(res.data.status.memTotal)
$("#memUsed").text(res.data.status.memUsed)
$("#memUsage").text(res.data.status.memUsage)
$("#diskTotal").text(res.data.status.diskTotal)
$("#diskUsed").text(res.data.status.diskUsed)
$("#diskUsage").text(res.data.status.diskUsage)
$("#cpu").text(res.data.status.cpu)
$("#memUsageP").val(res.data.status.memUsage)
$("#diskUsageP").val(res.data.status.diskUsage)
$("#cpuP").val(res.data.status.cpu)
$("#inSpeed").text(res.data.status.inSpeed)
$("#outSpeed").text(res.data.status.outSpeed)
}
}
)
}
deviceUpdate()
setInterval(deviceUpdate,2000)

View File

@ -43,26 +43,6 @@ function taskUpdate(){
}
)
}
function deviceUpdate(){
$.ajax(
"/stats/device",
{
success: function (res){
$("#memTotal").text(res.data.status.memTotal)
$("#memUsed").text(res.data.status.memUsed)
$("#memUsage").text(res.data.status.memUsage)
$("#diskTotal").text(res.data.status.diskTotal)
$("#diskUsed").text(res.data.status.diskUsed)
$("#diskUsage").text(res.data.status.diskUsage)
$("#cpu").text(res.data.status.cpu)
$("#memUsageP").val(res.data.status.memUsage)
$("#diskUsageP").val(res.data.status.diskUsage)
$("#cpuP").val(res.data.status.cpu)
}
}
)
}
taskUpdate()
deviceUpdate()
setInterval(taskUpdate,10000)
setInterval(deviceUpdate,5000)

24
templates/device.html Normal file
View File

@ -0,0 +1,24 @@
<h1>机器状态</h1>
<table>
<tr>
<td>CPU使用率</td>
<td><progress id="cpuP" max="100" value="0"></progress></td>
<td><span id="cpu"></span>%</td>
</tr>
<tr>
<td>内存使用率</td>
<td><progress id="memUsageP" max="100" value="0"></progress></td>
<td><span id="memUsed"></span>/<span id="memTotal"></span>(<span id="memUsage"></span>%)</td>
</tr>
<tr>
<td>磁盘使用率</td>
<td><progress id="diskUsageP" max="100" value="0"></progress></td>
<td><span id="diskUsed"></span>/<span id="diskTotal"></span>(<span id="diskUsage"></span>%)</td>
</tr>
<tr>
<td>网络速率</td>
<td><span id="inSpeed"></span>/s</td>
<td><span id="outSpeed"></span>/s</td>
</tr>
</table>
<script src="/static/device.js"></script>

View File

@ -1,15 +1,11 @@
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>文件</title>
<style>
td{
border: solid 1px lightgray;
}
</style>
{% include 'head.html' %}
</head>
<body>
<div>
<h1>所有录像文件</h1>
<table>
<tr>
@ -21,5 +17,8 @@
</tr>
{% endfor %}
</table>
<hr/>
{% include 'device.html' %}
</div>
</body>
</html>

7
templates/head.html Normal file
View File

@ -0,0 +1,7 @@
<meta charset="UTF-8">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<style>
td{
border: solid 1px lightgray;
}
</style>

View File

@ -1,14 +1,8 @@
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>录播</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
td {
border: solid 1px lightgrey;
}
</style>
{% include 'head.html' %}
</head>
<body>
<div>
@ -76,25 +70,8 @@
</tr>
</table>
<hr/>
<h1>机器状态</h1>
<table>
<tr>
<td>CPU使用率</td>
<td><progress id="cpuP" max="100" value="0"></progress></td>
<td><span id="cpu"></span>%</td>
</tr>
<tr>
<td>内存使用率</td>
<td><progress id="memUsageP" max="100" value="0"></progress></td>
<td><span id="memUsed"></span>/<span id="memTotal"></span>(<span id="memUsage"></span>%)</td>
</tr>
<tr>
<td>磁盘使用率</td>
<td><progress id="diskUsageP" max="100" value="0"></progress></td>
<td><span id="diskUsed"></span>/<span id="diskTotal"></span>(<span id="diskUsage"></span>%)</td>
</tr>
</table>
{% include 'device.html' %}
</div>
<script src="../static/index.js"></script>
</body>
<script src="index.js"></script>
</html>