opt.: disk size (#252)

This commit is contained in:
lollipopkit
2024-01-21 18:01:11 +08:00
parent 362dcdf288
commit 50d6ed919b
11 changed files with 42 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
extension NumX on num { extension NumX on num {
String get convertBytes { String get bytes2Str {
const suffix = ['B', 'KB', 'MB', 'GB', 'TB']; const suffix = ['B', 'KB', 'MB', 'GB', 'TB'];
double value = toDouble(); double value = toDouble();
int squareTimes = 0; int squareTimes = 0;
@@ -12,10 +12,12 @@ extension NumX on num {
} }
return '$finalValue ${suffix[squareTimes]}'; return '$finalValue ${suffix[squareTimes]}';
} }
String get kb2Str => (this * 1024).bytes2Str;
} }
extension BigIntX on BigInt { extension BigIntX on BigInt {
String get convertBytes { String get bytes2Str {
const suffix = ['B', 'KB', 'MB', 'GB', 'TB']; const suffix = ['B', 'KB', 'MB', 'GB', 'TB'];
double value = toDouble(); double value = toDouble();
int squareTimes = 0; int squareTimes = 0;
@@ -28,4 +30,6 @@ extension BigIntX on BigInt {
} }
return '$finalValue ${suffix[squareTimes]}'; return '$finalValue ${suffix[squareTimes]}';
} }
String get kb2Str => (this * BigInt.from(1024)).bytes2Str;
} }

View File

@@ -214,7 +214,7 @@ const _statusCmds = [
'cat /proc/stat | grep cpu', 'cat /proc/stat | grep cpu',
'uptime', 'uptime',
'cat /proc/net/snmp', 'cat /proc/net/snmp',
'df -h', 'df -B 1K',
"cat /proc/meminfo | grep -E 'Mem|Swap'", "cat /proc/meminfo | grep -E 'Mem|Swap'",
'cat /sys/class/thermal/thermal_zone*/type', 'cat /sys/class/thermal/thermal_zone*/type',
'cat /sys/class/thermal/thermal_zone*/temp', 'cat /sys/class/thermal/thermal_zone*/temp',
@@ -246,7 +246,7 @@ const _bsdStatusCmd = [
'uname -or', 'uname -or',
'top -l 1 | grep "CPU usage"', 'top -l 1 | grep "CPU usage"',
'uptime', 'uptime',
'df -h', 'df -k',
'top -l 1 | grep PhysMem', 'top -l 1 | grep PhysMem',
//'sysctl -a | grep temperature', //'sysctl -a | grep temperature',
'hostname', 'hostname',

View File

@@ -34,7 +34,7 @@ final class PodmanImg implements ContainerImg {
}); });
@override @override
String? get sizeMB => size?.convertBytes; String? get sizeMB => size?.bytes2Str;
@override @override
int? get containersCount => containers; int? get containersCount => containers;

View File

@@ -7,9 +7,9 @@ class Disk {
final String dev; final String dev;
final String mount; final String mount;
final int usedPercent; final int usedPercent;
final String used; final BigInt used;
final String size; final BigInt size;
final String avail; final BigInt avail;
const Disk({ const Disk({
required this.dev, required this.dev,
@@ -20,6 +20,12 @@ class Disk {
required this.avail, required this.avail,
}); });
/// raw:
/// ```
/// Filesystem 1K-blocks Used Available Use% Mounted on
/// overlay 959122528 154470540 755857572 17% /
/// tmpfs 65536 0 65536 0% /dev
/// ```
static List<Disk> parse(String raw) { static List<Disk> parse(String raw) {
final list = <Disk>[]; final list = <Disk>[];
final items = raw.split('\n'); final items = raw.split('\n');
@@ -43,9 +49,9 @@ class Disk {
dev: vals[0], dev: vals[0],
mount: vals[5], mount: vals[5],
usedPercent: int.parse(vals[4].replaceFirst('%', '')), usedPercent: int.parse(vals[4].replaceFirst('%', '')),
used: vals[2], used: BigInt.tryParse(vals[2]) ?? BigInt.zero,
size: vals[1], size: BigInt.tryParse(vals[1]) ?? BigInt.one,
avail: vals[3], avail: BigInt.tryParse(vals[3]) ?? BigInt.one,
)); ));
} catch (e) { } catch (e) {
continue; continue;
@@ -79,8 +85,8 @@ class DiskIO extends TimeSeq<DiskIOPiece> {
(String?, String?) getSpeed(String dev) { (String?, String?) getSpeed(String dev) {
final (read_, write_) = _getSpeed(dev); final (read_, write_) = _getSpeed(dev);
if (read_ == null || write_ == null) return (null, null); if (read_ == null || write_ == null) return (null, null);
final read = '${read_.convertBytes}/s'; final read = '${read_.bytes2Str}/s';
final write = '${write_.convertBytes}/s'; final write = '${write_.bytes2Str}/s';
return (read, write); return (read, write);
} }
@@ -92,8 +98,8 @@ class DiskIO extends TimeSeq<DiskIOPiece> {
read += read_ ?? 0; read += read_ ?? 0;
write += write_ ?? 0; write += write_ ?? 0;
} }
final readStr = '${read.convertBytes}/s'; final readStr = '${read.bytes2Str}/s';
final writeStr = '${write.convertBytes}/s'; final writeStr = '${write.bytes2Str}/s';
return (readStr, writeStr); return (readStr, writeStr);
} }

View File

@@ -47,10 +47,10 @@ class NetSpeed extends TimeSeq<NetSpeedPart> {
for (var i = 0; i < now.length; i++) { for (var i = 0; i < now.length; i++) {
size += sizeInBytes(i); size += sizeInBytes(i);
} }
return size.convertBytes; return size.bytes2Str;
} }
final idx = deviceIdx(device); final idx = deviceIdx(device);
return sizeInBytes(idx).convertBytes; return sizeInBytes(idx).bytes2Str;
} }
String speedOut({String? device, bool all = false}) { String speedOut({String? device, bool all = false}) {
@@ -73,10 +73,10 @@ class NetSpeed extends TimeSeq<NetSpeedPart> {
for (var i = 0; i < now.length; i++) { for (var i = 0; i < now.length; i++) {
size += sizeOutBytes(i); size += sizeOutBytes(i);
} }
return size.convertBytes; return size.bytes2Str;
} }
final idx = deviceIdx(device); final idx = deviceIdx(device);
return sizeOutBytes(idx).convertBytes; return sizeOutBytes(idx).bytes2Str;
} }
int deviceIdx(String? device) { int deviceIdx(String? device) {
@@ -90,7 +90,7 @@ class NetSpeed extends TimeSeq<NetSpeedPart> {
return 0; return 0;
} }
String buildStandardOutput(double speed) => '${speed.convertBytes}/s'; String buildStandardOutput(double speed) => '${speed.bytes2Str}/s';
/// [raw] example: /// [raw] example:
/// Inter-| Receive | Transmit /// Inter-| Receive | Transmit

View File

@@ -41,13 +41,13 @@ abstract final class InitStatus {
avail: 1, avail: 1,
), ),
disk: [ disk: [
const Disk( Disk(
dev: '/', dev: '/',
mount: '/', mount: '/',
usedPercent: 0, usedPercent: 0,
used: '0', used: BigInt.zero,
size: '0', size: BigInt.one,
avail: '0', avail: BigInt.zero,
) )
], ],
tcp: const Conn(maxConn: 0, active: 0, passive: 0, fail: 0), tcp: const Conn(maxConn: 0, active: 0, passive: 0, fail: 0),

View File

@@ -193,8 +193,8 @@ class _PrivateKeyEditPageState extends State<PrivateKeyEditPage> {
context.showSnackBar( context.showSnackBar(
l10n.fileTooLarge( l10n.fileTooLarge(
path, path,
size.convertBytes, size.bytes2Str,
Miscs.privateKeyMaxSize.convertBytes, Miscs.privateKeyMaxSize.bytes2Str,
), ),
); );
return; return;

View File

@@ -256,7 +256,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
), ),
UIs.width7, UIs.width7,
Text( Text(
'of ${(ss.mem.total * 1024).convertBytes}', 'of ${(ss.mem.total * 1024).bytes2Str}',
style: UIs.text13Grey, style: UIs.text13Grey,
) )
], ],
@@ -297,7 +297,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
Text('${used.toStringAsFixed(0)}%', style: UIs.text27), Text('${used.toStringAsFixed(0)}%', style: UIs.text27),
UIs.width7, UIs.width7,
Text( Text(
'of ${(ss.swap.total * 1024).convertBytes} ', 'of ${(ss.swap.total * 1024).bytes2Str} ',
style: UIs.text13Grey, style: UIs.text13Grey,
) )
], ],
@@ -452,7 +452,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
Widget _buildDiskItem(Disk disk, ServerStatus ss) { Widget _buildDiskItem(Disk disk, ServerStatus ss) {
final (read, write) = ss.diskIO.getSpeed(disk.dev); final (read, write) = ss.diskIO.getSpeed(disk.dev);
final text = () { final text = () {
final use = '${disk.used} / ${disk.size}'; final use = '${disk.used.kb2Str} / ${disk.size.kb2Str}';
if (read == null || write == null) return use; if (read == null || write == null) return use;
return '$use\n${l10n.read} $read | ${l10n.write} $write'; return '$use\n${l10n.read} $read | ${l10n.write} $write';
}(); }();

View File

@@ -182,7 +182,7 @@ class _LocalStoragePageState extends State<LocalStoragePage> {
title: Text(fileName), title: Text(fileName),
subtitle: isDir subtitle: isDir
? null ? null
: Text(stat.size.convertBytes, style: UIs.textGrey), : Text(stat.size.bytes2Str, style: UIs.textGrey),
trailing: Text( trailing: Text(
stat.modified stat.modified
.toString() .toString()

View File

@@ -304,7 +304,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
subtitle: isDir subtitle: isDir
? null ? null
: Text( : Text(
(file.attr.size ?? 0).convertBytes, (file.attr.size ?? 0).bytes2Str,
style: UIs.textGrey, style: UIs.textGrey,
), ),
onTap: () { onTap: () {

View File

@@ -94,7 +94,7 @@ class _SftpMissionPageState extends State<SftpMissionPage> {
); );
case SftpWorkerStatus.loading: case SftpWorkerStatus.loading:
final percentStr = (status.progress ?? 0.0).toStringAsFixed(2); final percentStr = (status.progress ?? 0.0).toStringAsFixed(2);
final size = (status.size ?? 0).convertBytes; final size = (status.size ?? 0).bytes2Str;
return _wrapInCard( return _wrapInCard(
status: status, status: status,
subtitle: l10n.percentOfSize(percentStr, size), subtitle: l10n.percentOfSize(percentStr, size),