diff --git a/Common.py b/Common.py index 0288587..8dae0d6 100644 --- a/Common.py +++ b/Common.py @@ -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): diff --git a/WebMain.py b/WebMain.py index 2242873..528f6ac 100644 --- a/WebMain.py +++ b/WebMain.py @@ -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() diff --git a/static/device.js b/static/device.js new file mode 100644 index 0000000..ed98191 --- /dev/null +++ b/static/device.js @@ -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) diff --git a/static/index.js b/static/index.js index 879944f..123bbaf 100644 --- a/static/index.js +++ b/static/index.js @@ -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) \ No newline at end of file diff --git a/templates/device.html b/templates/device.html new file mode 100644 index 0000000..2eb224f --- /dev/null +++ b/templates/device.html @@ -0,0 +1,24 @@ +

机器状态

+ + + + + + + + + + + + + + + + + + + + + +
CPU使用率%
内存使用率/(%)
磁盘使用率/(%)
网络速率/s/s
+ diff --git a/templates/files.html b/templates/files.html index 891e537..9e2e62a 100644 --- a/templates/files.html +++ b/templates/files.html @@ -1,25 +1,24 @@ - 文件 - + {% include 'head.html' %} -

所有录像文件

- - - - - {%for i in files %} - - - - {% endfor %} -
文件名文件大小链接
{{i.name}}{{i.size}}下载文件
+
+

所有录像文件

+ + + + + {%for i in files %} + + + + {% endfor %} +
文件名文件大小链接
{{i.name}}{{i.size}}下载文件
+
+ {% include 'device.html' %} +
\ No newline at end of file diff --git a/templates/head.html b/templates/head.html new file mode 100644 index 0000000..c84b086 --- /dev/null +++ b/templates/head.html @@ -0,0 +1,7 @@ + + + diff --git a/static/index.html b/templates/index.html similarity index 65% rename from static/index.html rename to templates/index.html index 27f0203..761765c 100644 --- a/static/index.html +++ b/templates/index.html @@ -1,14 +1,8 @@ - 录播 - - + {% include 'head.html' %}
@@ -76,25 +70,8 @@
-

机器状态

- - - - - - - - - - - - - - - - -
CPU使用率%
内存使用率/(%)
磁盘使用率/(%)
+ {% include 'device.html' %}
+ - \ No newline at end of file