From 14d9c2d76a6a305d83952cd1ffb6cbd79642ccd3 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 22 Apr 2022 17:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=B9=E9=87=8F=E5=8D=95=E4=BD=8D=E5=8F=8A?= =?UTF-8?q?=E5=B0=8F=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/api/collector_blueprint.py | 25 +++++++++++++++++++++++-- requirements.txt | 1 + templates/index.html | 12 ++++++------ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/controller/api/collector_blueprint.py b/controller/api/collector_blueprint.py index 2a31267..e794229 100644 --- a/controller/api/collector_blueprint.py +++ b/controller/api/collector_blueprint.py @@ -16,8 +16,8 @@ def _get_disk_info(path): return { 'exist': True, 'percent': disk_info.percent, - 'free': disk_info.free / (1024 ** 2), - 'total': disk_info.total / (1024 ** 2) + 'free': _better_size_unit(disk_info.free), + 'total': _better_size_unit(disk_info.total) } else: return { @@ -27,6 +27,27 @@ def _get_disk_info(path): 'total': 0 } + +def _better_size_unit(bytes: int) -> str: + if bytes > 8*1024: + kbytes = bytes / 1024 + if kbytes > 8*1024: + mbytes = kbytes / 1024 + if mbytes > 4*1024: + gbytes = mbytes / 1024 + if gbytes > 4*1024: + tbytes = gbytes / 1024 + return "{:.2f}TB".format(tbytes) + else: + return "{:.2f}GB".format(gbytes) + else: + return "{:.2f}MB".format(mbytes) + else: + return "{:.1f}KB".format(kbytes) + else: + return "{}B".format(bytes) + + @blueprint.get("/") def collect_basic_status(): return jsonify({ diff --git a/requirements.txt b/requirements.txt index 817f56d..56061a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,4 @@ SQLAlchemy==1.4.35 Werkzeug==2.1.1 wheel==0.37.1 zipp==3.8.0 +psutil==5.9.0 \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index abee353..d1d5eb3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -128,7 +128,7 @@