From 3c7bb77f3e4d71cdd540f1811be14ac47d3b25f5 Mon Sep 17 00:00:00 2001 From: lollipopkit Date: Sun, 7 Apr 2024 22:55:00 +0800 Subject: [PATCH] new: FIFO list impl --- lib/data/model/server/cpu.dart | 21 +++++----- lib/data/model/server/disk.dart | 4 +- lib/data/model/server/net_speed.dart | 12 ++---- lib/data/model/server/time_seq.dart | 59 ++++++++++++++++++++++++---- lib/view/page/server/edit.dart | 1 - lib/view/page/server/tab.dart | 1 - lib/view/page/ssh/tab.dart | 1 - 7 files changed, 69 insertions(+), 30 deletions(-) diff --git a/lib/data/model/server/cpu.dart b/lib/data/model/server/cpu.dart index 8d5cb733..201bdde8 100644 --- a/lib/data/model/server/cpu.dart +++ b/lib/data/model/server/cpu.dart @@ -1,9 +1,10 @@ +import 'dart:collection'; + +import 'package:toolbox/data/model/server/time_seq.dart'; import 'package:toolbox/data/res/status.dart'; -import 'time_seq.dart'; - -class Cpus extends TimeSeq { - Cpus(super.pre, super.now); +class Cpus extends TimeSeq> { + Cpus(super.init1, super.init2); @override void onUpdate() { @@ -124,9 +125,11 @@ Cpus parseBsdCpu(String raw) { .map((e) => double.parse(e.group(1) ?? '0') * 100) .toList(); if (percents.length != 3) return InitStatus.cpus; - return InitStatus.cpus - ..now = [ - OneTimeCpuStatus('cpu', percents[0].toInt(), 0, 0, - percents[2].toInt() + percents[1].toInt(), 0, 0, 0) - ]; + + final init = InitStatus.cpus; + init.add([ + OneTimeCpuStatus('cpu', percents[0].toInt(), 0, 0, + percents[2].toInt() + percents[1].toInt(), 0, 0, 0), + ]); + return init; } diff --git a/lib/data/model/server/disk.dart b/lib/data/model/server/disk.dart index a5ee0e0c..329b6752 100644 --- a/lib/data/model/server/disk.dart +++ b/lib/data/model/server/disk.dart @@ -58,8 +58,8 @@ class Disk { } } -class DiskIO extends TimeSeq { - DiskIO(super.pre, super.now); +class DiskIO extends TimeSeq> { + DiskIO(super.init1, super.init2); @override void onUpdate() { diff --git a/lib/data/model/server/net_speed.dart b/lib/data/model/server/net_speed.dart index 042f774c..133d9aeb 100644 --- a/lib/data/model/server/net_speed.dart +++ b/lib/data/model/server/net_speed.dart @@ -14,8 +14,8 @@ class NetSpeedPart extends TimeSeqIface { bool same(NetSpeedPart other) => device == other.device; } -class NetSpeed extends TimeSeq { - NetSpeed(super.pre, super.now); +class NetSpeed extends TimeSeq> { + NetSpeed(super.init1, super.init2); @override void onUpdate() { @@ -54,12 +54,8 @@ class NetSpeed extends TimeSeq { String sizeOut, String speedIn, String speedOut, - }) cachedRealVals = ( - sizeIn: '0kb', - sizeOut: '0kb', - speedIn: '0kb/s', - speedOut: '0kb/s', - ); + }) cachedRealVals = + (sizeIn: '0kb', sizeOut: '0kb', speedIn: '0kb/s', speedOut: '0kb/s'); /// Time diff between [pre] and [now] BigInt get _timeDiff => BigInt.from(now[0].time - pre[0].time); diff --git a/lib/data/model/server/time_seq.dart b/lib/data/model/server/time_seq.dart index 2b25b103..f1106b98 100644 --- a/lib/data/model/server/time_seq.dart +++ b/lib/data/model/server/time_seq.dart @@ -1,11 +1,56 @@ -abstract class TimeSeq { - List pre; - List now; +import 'dart:collection'; + +/// A FIFO queue with fixed capacity. +abstract class TimeSeq> extends ListBase { + final int capacity; + late final List _list; + + /// Due to the design, at least two elements are required, otherwise [pre] / + /// [now] will throw. + TimeSeq( + T init1, + T init2, { + this.capacity = 30, + }) : _list = [init1, init2]; + + @override + void add(element) { + if (length == capacity) { + _list.removeAt(0); + } + _list.add(element); + } + + @override + int get length => _list.length; + + @override + set length(int newLength) { + throw UnimplementedError(); + } + + @override + T operator [](int index) { + return _list[index]; + } + + @override + void operator []=(int index, value) { + _list[index] = value; + } + + T get pre { + return _list[length - 2]; + } + + T get now { + return _list[length - 1]; + } + void onUpdate(); - void update(List new_) { - pre = now; - now = new_; + void update(T new_) { + add(new_); if (pre.length != now.length) { pre.removeWhere((e) => now.any((el) => e.same(el))); @@ -14,8 +59,6 @@ abstract class TimeSeq { onUpdate(); } - - TimeSeq(this.pre, this.now); } abstract class TimeSeqIface { diff --git a/lib/view/page/server/edit.dart b/lib/view/page/server/edit.dart index e52cc952..5176b934 100644 --- a/lib/view/page/server/edit.dart +++ b/lib/view/page/server/edit.dart @@ -409,7 +409,6 @@ class _ServerEditPageState extends State { Widget _buildFAB() { return FloatingActionButton( - heroTag: 'server', onPressed: _onSave, child: const Icon(Icons.save), ); diff --git a/lib/view/page/server/tab.dart b/lib/view/page/server/tab.dart index 9e4938b2..de8fd960 100644 --- a/lib/view/page/server/tab.dart +++ b/lib/view/page/server/tab.dart @@ -69,7 +69,6 @@ class _ServerPageState extends State floatingActionButton: FloatingActionButton( onPressed: () => AppRoute.serverEdit().go(context), tooltip: l10n.addAServer, - heroTag: 'server', child: const Icon(Icons.add), ), ); diff --git a/lib/view/page/ssh/tab.dart b/lib/view/page/ssh/tab.dart index 84ba2cea..2e9905a9 100644 --- a/lib/view/page/ssh/tab.dart +++ b/lib/view/page/ssh/tab.dart @@ -48,7 +48,6 @@ class _SSHTabPageState extends State return FloatingActionButton( onPressed: () => AppRoute.serverEdit().go(context), tooltip: l10n.addAServer, - heroTag: 'server', child: const Icon(Icons.add), ); },