opt.: cpu line chart

This commit is contained in:
lollipopkit
2024-04-08 21:31:00 +08:00
parent 819c53df7e
commit 7d2fbde2fe
5 changed files with 32 additions and 17 deletions

View File

@@ -7,7 +7,7 @@ import 'package:toolbox/data/res/status.dart';
const _kCap = 30; const _kCap = 30;
class Cpus extends TimeSeq<List<SingleCoreCpu>> { class Cpus extends TimeSeq<List<SingleCpuCore>> {
Cpus(super.init1, super.init2); Cpus(super.init1, super.init2);
@override @override
@@ -72,7 +72,10 @@ class Cpus extends TimeSeq<List<SingleCoreCpu>> {
final _spots = <Fifo<FlSpot>>[]; final _spots = <Fifo<FlSpot>>[];
List<Fifo<FlSpot>> get spots => _spots; List<Fifo<FlSpot>> get spots => _spots;
void _updateSpots() { void _updateSpots() {
for (var i = 1; i < now.length; i++) { /// Only update the entire core when [coresCount] > 4, or the chart will be too crowded
final onlyCalcSingle = coresCount > 4;
final maxIdx = onlyCalcSingle ? 1 : coresCount;
for (var i = onlyCalcSingle ? 0 : 1; i < maxIdx; i++) {
if (i >= _spots.length) { if (i >= _spots.length) {
_spots.add(Fifo(capacity: _kCap)); _spots.add(Fifo(capacity: _kCap));
} else { } else {
@@ -112,7 +115,7 @@ class Cpus extends TimeSeq<List<SingleCoreCpu>> {
} }
} }
class SingleCoreCpu extends TimeSeqIface<SingleCoreCpu> { class SingleCpuCore extends TimeSeqIface<SingleCpuCore> {
final String id; final String id;
final int user; final int user;
final int sys; final int sys;
@@ -122,7 +125,7 @@ class SingleCoreCpu extends TimeSeqIface<SingleCoreCpu> {
final int irq; final int irq;
final int softirq; final int softirq;
SingleCoreCpu( SingleCpuCore(
this.id, this.id,
this.user, this.user,
this.sys, this.sys,
@@ -136,10 +139,10 @@ class SingleCoreCpu extends TimeSeqIface<SingleCoreCpu> {
int get total => user + sys + nice + idle + iowait + irq + softirq; int get total => user + sys + nice + idle + iowait + irq + softirq;
@override @override
bool same(SingleCoreCpu other) => id == other.id; bool same(SingleCpuCore other) => id == other.id;
static List<SingleCoreCpu> parse(String raw) { static List<SingleCpuCore> parse(String raw) {
final List<SingleCoreCpu> cpus = []; final List<SingleCpuCore> cpus = [];
for (var item in raw.split('\n')) { for (var item in raw.split('\n')) {
if (item == '') break; if (item == '') break;
@@ -147,7 +150,7 @@ class SingleCoreCpu extends TimeSeqIface<SingleCoreCpu> {
if (id == null) continue; if (id == null) continue;
final matches = item.replaceFirst(id, '').trim().split(' '); final matches = item.replaceFirst(id, '').trim().split(' ');
cpus.add( cpus.add(
SingleCoreCpu( SingleCpuCore(
id, id,
int.parse(matches[0]), int.parse(matches[0]),
int.parse(matches[1]), int.parse(matches[1]),
@@ -178,7 +181,7 @@ Cpus parseBsdCpu(String raw) {
final init = InitStatus.cpus; final init = InitStatus.cpus;
init.add([ init.add([
SingleCoreCpu('cpu', percents[0].toInt(), 0, 0, SingleCpuCore('cpu', percents[0].toInt(), 0, 0,
percents[2].toInt() + percents[1].toInt(), 0, 0, 0), percents[2].toInt() + percents[1].toInt(), 0, 0, 0),
]); ]);
return init; return init;

View File

@@ -69,7 +69,7 @@ Future<ServerStatus> _getLinuxStatus(ServerStatusUpdateReq req) async {
} }
try { try {
final cpus = SingleCoreCpu.parse(StatusCmdType.cpu.find(segments)); final cpus = SingleCpuCore.parse(StatusCmdType.cpu.find(segments));
req.ss.cpu.update(cpus); req.ss.cpu.update(cpus);
} catch (e, s) { } catch (e, s) {
Loggers.parse.warning(e, s); Loggers.parse.warning(e, s);

View File

@@ -9,7 +9,7 @@ import '../model/server/conn.dart';
import '../model/server/system.dart'; import '../model/server/system.dart';
abstract final class InitStatus { abstract final class InitStatus {
static SingleCoreCpu get _initOneTimeCpuStatus => SingleCoreCpu( static SingleCpuCore get _initOneTimeCpuStatus => SingleCpuCore(
'cpu', 'cpu',
0, 0,
0, 0,

View File

@@ -37,17 +37,24 @@ enum _NetSortType {
} }
} }
Widget _buildLineChart(List<List<FlSpot>> spots, Range<double> x) { Widget _buildLineChart(
List<List<FlSpot>> spots,
Range<double> x, {
String? tooltipPrefix,
bool curve = false,
}) {
return LineChart(LineChartData( return LineChart(LineChartData(
lineTouchData: LineTouchData( lineTouchData: LineTouchData(
touchTooltipData: LineTouchTooltipData( touchTooltipData: LineTouchTooltipData(
tooltipPadding: const EdgeInsets.all(8), tooltipPadding: const EdgeInsets.all(5),
tooltipRoundedRadius: 8, tooltipRoundedRadius: 8,
tooltipMargin: 3,
getTooltipItems: (List<LineBarSpot> touchedSpots) { getTooltipItems: (List<LineBarSpot> touchedSpots) {
return touchedSpots.map((e) { return touchedSpots.map((e) {
return LineTooltipItem( return LineTooltipItem(
'CPU${e.barIndex}: ${e.y.toStringAsFixed(2)}', '$tooltipPrefix${e.barIndex}: ${e.y.toStringAsFixed(2)}',
const TextStyle( const TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
), ),
); );
@@ -101,7 +108,7 @@ Widget _buildLineChart(List<List<FlSpot>> spots, Range<double> x) {
lineBarsData: spots lineBarsData: spots
.map((e) => LineChartBarData( .map((e) => LineChartBarData(
spots: e, spots: e,
isCurved: false, isCurved: curve,
barWidth: 2, barWidth: 2,
isStrokeCapRound: true, isStrokeCapRound: true,
color: primaryColor, color: primaryColor,

View File

@@ -193,11 +193,16 @@ class _ServerDetailPageState extends State<ServerDetailPage>
), ),
children: [ children: [
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 7), padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 13),
child: SizedBox( child: SizedBox(
height: 137, height: 137,
width: _media.size.width - 26 - 34, width: _media.size.width - 26 - 34,
child: _buildLineChart(ss.cpu.spots, ss.cpu.rangeX), child: _buildLineChart(
ss.cpu.spots,
ss.cpu.rangeX,
tooltipPrefix: 'CPU',
curve: true,
),
), ),
), ),
], ],