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 @@