Skip to content

Commit

Permalink
feat: monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
guchengxi1994 committed Apr 19, 2024
1 parent 2f12349 commit 57ee9f6
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 4 deletions.
53 changes: 53 additions & 0 deletions lib/system_monitor/components/process_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:all_in_one/styles/app_style.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';

class ProcessItem extends StatelessWidget {
const ProcessItem(
{super.key, required this.name, required this.rate, required this.value});
final String name;
final double rate;
final String value;

@override
Widget build(BuildContext context) {
return SizedBox(
height: 60,
child: Row(
children: [
SizedBox(
width: 100,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AutoSizeText(
name,
maxLines: 1,
style: const TextStyle(color: Colors.white),
),
Text(
value,
style: const TextStyle(color: AppStyle.appCheckColor),
)
],
),
),
const SizedBox(
width: 10,
),
Container(
width: 270 * rate,
height: 30,
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
AppStyle.appCheckColor,
AppStyle.red.withOpacity(rate)
]),
borderRadius: BorderRadius.circular(4),
),
)
],
),
);
}
}
73 changes: 73 additions & 0 deletions lib/system_monitor/components/processes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:all_in_one/system_monitor/components/process_item.dart';
import 'package:all_in_one/system_monitor/notifiers/process_notifier.dart';
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class Processes extends ConsumerStatefulWidget {
const Processes({super.key});

@override
ConsumerState<Processes> createState() => _ProcessesState();
}

class _ProcessesState extends ConsumerState<Processes>
with TickerProviderStateMixin {
late final TabController controller;
@override
void initState() {
controller = TabController(length: 2, vsync: this);
super.initState();
}

@override
Widget build(BuildContext context) {
final state = ref.watch(processProvider);

return Column(
children: [
SizedBox(
height: 50,
child: TabBar(controller: controller, tabs: const [
Text(
"cpu",
style: TextStyle(color: Colors.white),
),
Text(
"memory",
style: TextStyle(color: Colors.white),
)
]),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: TabBarView(controller: controller, children: [
ListView.builder(
itemBuilder: (c, i) {
final value = state.cpus[i].cpu / state.cpus.first.cpu;

return ProcessItem(
name: state.cpus[i].name,
rate: value,
value: "${state.cpus[i].cpu.ceilToDouble()}%");
},
itemCount: state.cpus.length,
),
ListView.builder(
itemBuilder: (c, i) {
final value =
state.memories[i].memory / state.memories.first.memory;
return ProcessItem(
name: state.memories[i].name,
rate: value,
value: filesize(state.memories[i].memory));
},
itemCount: state.memories.length,
)
]),
))
],
);
}
}
20 changes: 20 additions & 0 deletions lib/system_monitor/notifiers/process_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:all_in_one/src/rust/system_monitor.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'process_state.dart';

class ProcessNotifier extends AutoDisposeNotifier<ProcessState> {
@override
ProcessState build() {
return ProcessState();
}

changeState(List<SoftwareCpu> cpus, List<SoftwareMemory> memories) {
state = ProcessState(cpus: cpus, memories: memories);
}
}

final processProvider =
AutoDisposeNotifierProvider<ProcessNotifier, ProcessState>(
() => ProcessNotifier(),
);
8 changes: 8 additions & 0 deletions lib/system_monitor/notifiers/process_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:all_in_one/src/rust/system_monitor.dart';

class ProcessState {
final List<SoftwareCpu> cpus;
final List<SoftwareMemory> memories;

ProcessState({this.cpus = const [], this.memories = const []});
}
9 changes: 8 additions & 1 deletion lib/system_monitor/system_monitor_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import 'package:all_in_one/system_monitor/components/memory.dart';
import 'package:all_in_one/system_monitor/notifiers/cpu_notifier.dart';
import 'package:all_in_one/system_monitor/notifiers/disks_notifier.dart';
import 'package:all_in_one/system_monitor/notifiers/memory_notifier.dart';
import 'package:all_in_one/system_monitor/notifiers/process_notifier.dart';
import 'package:flutter/material.dart';
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'components/disks.dart';
import 'components/processes.dart';

class SystemMonitorScreen extends ConsumerStatefulWidget {
const SystemMonitorScreen({super.key});
Expand Down Expand Up @@ -45,6 +47,11 @@ class _SystemMonitorScreenState extends ConsumerState<SystemMonitorScreen> {
if (event.memory != null) {
ref.read(memoryProvider.notifier).changeState(event.memory!);
}
if (event.top5Cpu != null && event.top5Memory != null) {
ref
.read(processProvider.notifier)
.changeState(event.top5Cpu!, event.top5Memory!);
}
}
});
}
Expand Down Expand Up @@ -73,7 +80,7 @@ area1 area1 area1
_wrapper(const Disks()).inGridArea("area1"),
_wrapper(const Cpu()).inGridArea("area2"),
_wrapper(const Memory()).inGridArea("area3"),
_wrapper(null).inGridArea("area4"),
_wrapper(const Processes()).inGridArea("area4"),
_wrapper(null).inGridArea("area5"),
_wrapper(null).inGridArea("area6")
],
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: all_in_one
description: "A new Flutter project."
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.0.3+6
version: 0.0.4

environment:
sdk: '>=3.3.0 <4.0.0'
Expand Down
12 changes: 10 additions & 2 deletions rust/src/system_monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,22 @@ pub fn start_monitor() {
}

v1.sort_by(|a,b|b.cpu.total_cmp(&a.cpu));
v1.reverse();
// v1.reverse();

v2.sort_by(|a,b|b.memory.cmp(&a.memory));
v2.reverse();
// v2.reverse();

monitor_info.top_5_cpu = Some(get_first_five_or_all(&v1));
monitor_info.top_5_memory = Some(get_first_five_or_all(&v2));

// for i in &monitor_info.top_5_cpu{
// println!("v1 {:?}",i);
// }

// for i in &monitor_info.top_5_memory{
// println!("v2 {:?}",i);
// }

match SYS_MONITOR_MESSAGE_SINK.try_read() {
Ok(s) => match s.as_ref() {
Some(s0) => {
Expand Down

0 comments on commit 57ee9f6

Please sign in to comment.