容量单位及小数

This commit is contained in:
2022-04-22 17:52:07 +08:00
parent 04b9f39fb9
commit 14d9c2d76a
3 changed files with 30 additions and 8 deletions

View File

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