mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
new: FIFO list impl
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import 'package:toolbox/data/model/server/time_seq.dart';
|
||||||
import 'package:toolbox/data/res/status.dart';
|
import 'package:toolbox/data/res/status.dart';
|
||||||
|
|
||||||
import 'time_seq.dart';
|
class Cpus extends TimeSeq<List<OneTimeCpuStatus>> {
|
||||||
|
Cpus(super.init1, super.init2);
|
||||||
class Cpus extends TimeSeq<OneTimeCpuStatus> {
|
|
||||||
Cpus(super.pre, super.now);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onUpdate() {
|
void onUpdate() {
|
||||||
@@ -124,9 +125,11 @@ Cpus parseBsdCpu(String raw) {
|
|||||||
.map((e) => double.parse(e.group(1) ?? '0') * 100)
|
.map((e) => double.parse(e.group(1) ?? '0') * 100)
|
||||||
.toList();
|
.toList();
|
||||||
if (percents.length != 3) return InitStatus.cpus;
|
if (percents.length != 3) return InitStatus.cpus;
|
||||||
return InitStatus.cpus
|
|
||||||
..now = [
|
final init = InitStatus.cpus;
|
||||||
OneTimeCpuStatus('cpu', percents[0].toInt(), 0, 0,
|
init.add([
|
||||||
percents[2].toInt() + percents[1].toInt(), 0, 0, 0)
|
OneTimeCpuStatus('cpu', percents[0].toInt(), 0, 0,
|
||||||
];
|
percents[2].toInt() + percents[1].toInt(), 0, 0, 0),
|
||||||
|
]);
|
||||||
|
return init;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ class Disk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DiskIO extends TimeSeq<DiskIOPiece> {
|
class DiskIO extends TimeSeq<List<DiskIOPiece>> {
|
||||||
DiskIO(super.pre, super.now);
|
DiskIO(super.init1, super.init2);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onUpdate() {
|
void onUpdate() {
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ class NetSpeedPart extends TimeSeqIface<NetSpeedPart> {
|
|||||||
bool same(NetSpeedPart other) => device == other.device;
|
bool same(NetSpeedPart other) => device == other.device;
|
||||||
}
|
}
|
||||||
|
|
||||||
class NetSpeed extends TimeSeq<NetSpeedPart> {
|
class NetSpeed extends TimeSeq<List<NetSpeedPart>> {
|
||||||
NetSpeed(super.pre, super.now);
|
NetSpeed(super.init1, super.init2);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onUpdate() {
|
void onUpdate() {
|
||||||
@@ -54,12 +54,8 @@ class NetSpeed extends TimeSeq<NetSpeedPart> {
|
|||||||
String sizeOut,
|
String sizeOut,
|
||||||
String speedIn,
|
String speedIn,
|
||||||
String speedOut,
|
String speedOut,
|
||||||
}) cachedRealVals = (
|
}) cachedRealVals =
|
||||||
sizeIn: '0kb',
|
(sizeIn: '0kb', sizeOut: '0kb', speedIn: '0kb/s', speedOut: '0kb/s');
|
||||||
sizeOut: '0kb',
|
|
||||||
speedIn: '0kb/s',
|
|
||||||
speedOut: '0kb/s',
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Time diff between [pre] and [now]
|
/// Time diff between [pre] and [now]
|
||||||
BigInt get _timeDiff => BigInt.from(now[0].time - pre[0].time);
|
BigInt get _timeDiff => BigInt.from(now[0].time - pre[0].time);
|
||||||
|
|||||||
@@ -1,11 +1,56 @@
|
|||||||
abstract class TimeSeq<T extends TimeSeqIface> {
|
import 'dart:collection';
|
||||||
List<T> pre;
|
|
||||||
List<T> now;
|
/// A FIFO queue with fixed capacity.
|
||||||
|
abstract class TimeSeq<T extends List<TimeSeqIface>> extends ListBase<T> {
|
||||||
|
final int capacity;
|
||||||
|
late final List<T> _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 onUpdate();
|
||||||
|
|
||||||
void update(List<T> new_) {
|
void update(T new_) {
|
||||||
pre = now;
|
add(new_);
|
||||||
now = new_;
|
|
||||||
|
|
||||||
if (pre.length != now.length) {
|
if (pre.length != now.length) {
|
||||||
pre.removeWhere((e) => now.any((el) => e.same(el)));
|
pre.removeWhere((e) => now.any((el) => e.same(el)));
|
||||||
@@ -14,8 +59,6 @@ abstract class TimeSeq<T extends TimeSeqIface> {
|
|||||||
|
|
||||||
onUpdate();
|
onUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeSeq(this.pre, this.now);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class TimeSeqIface<T> {
|
abstract class TimeSeqIface<T> {
|
||||||
|
|||||||
@@ -409,7 +409,6 @@ class _ServerEditPageState extends State<ServerEditPage> {
|
|||||||
|
|
||||||
Widget _buildFAB() {
|
Widget _buildFAB() {
|
||||||
return FloatingActionButton(
|
return FloatingActionButton(
|
||||||
heroTag: 'server',
|
|
||||||
onPressed: _onSave,
|
onPressed: _onSave,
|
||||||
child: const Icon(Icons.save),
|
child: const Icon(Icons.save),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -69,7 +69,6 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () => AppRoute.serverEdit().go(context),
|
onPressed: () => AppRoute.serverEdit().go(context),
|
||||||
tooltip: l10n.addAServer,
|
tooltip: l10n.addAServer,
|
||||||
heroTag: 'server',
|
|
||||||
child: const Icon(Icons.add),
|
child: const Icon(Icons.add),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ class _SSHTabPageState extends State<SSHTabPage>
|
|||||||
return FloatingActionButton(
|
return FloatingActionButton(
|
||||||
onPressed: () => AppRoute.serverEdit().go(context),
|
onPressed: () => AppRoute.serverEdit().go(context),
|
||||||
tooltip: l10n.addAServer,
|
tooltip: l10n.addAServer,
|
||||||
heroTag: 'server',
|
|
||||||
child: const Icon(Icons.add),
|
child: const Icon(Icons.add),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user