mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
#148 fix: alpine process
This commit is contained in:
@@ -2,14 +2,16 @@ import '../../res/build_data.dart';
|
|||||||
import '../../res/server_cmd.dart';
|
import '../../res/server_cmd.dart';
|
||||||
import '../server/system.dart';
|
import '../server/system.dart';
|
||||||
|
|
||||||
const _cmdDivider = '\necho $seperator\n';
|
const _cmdDivider = '\necho $seperator\n\t';
|
||||||
|
|
||||||
const _serverBoxDir = r'$HOME/.config/server_box';
|
const _serverBoxDir = r'$HOME/.config/server_box';
|
||||||
const _shellPath = '$_serverBoxDir/mobile_app.sh';
|
const _shellPath = '$_serverBoxDir/mobile_app.sh';
|
||||||
|
|
||||||
enum AppShellFuncType {
|
enum AppShellFuncType {
|
||||||
status,
|
status,
|
||||||
docker;
|
docker,
|
||||||
|
process,
|
||||||
|
;
|
||||||
|
|
||||||
String get flag {
|
String get flag {
|
||||||
switch (this) {
|
switch (this) {
|
||||||
@@ -17,6 +19,8 @@ enum AppShellFuncType {
|
|||||||
return 's';
|
return 's';
|
||||||
case AppShellFuncType.docker:
|
case AppShellFuncType.docker:
|
||||||
return 'd';
|
return 'd';
|
||||||
|
case AppShellFuncType.process:
|
||||||
|
return 'p';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +34,8 @@ enum AppShellFuncType {
|
|||||||
// `dockeR` -> avoid conflict with `docker` command
|
// `dockeR` -> avoid conflict with `docker` command
|
||||||
// 以防止循环递归
|
// 以防止循环递归
|
||||||
return 'dockeR';
|
return 'dockeR';
|
||||||
|
case AppShellFuncType.process:
|
||||||
|
return 'process';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,17 +45,36 @@ enum AppShellFuncType {
|
|||||||
return '''
|
return '''
|
||||||
result=\$(uname 2>&1 | grep "Linux")
|
result=\$(uname 2>&1 | grep "Linux")
|
||||||
if [ "\$result" != "" ]; then
|
if [ "\$result" != "" ]; then
|
||||||
${_statusCmds.join(_cmdDivider)}
|
\t${_statusCmds.join(_cmdDivider)}
|
||||||
else
|
else
|
||||||
${_bsdStatusCmd.join(_cmdDivider)}
|
\t${_bsdStatusCmd.join(_cmdDivider)}
|
||||||
fi''';
|
fi''';
|
||||||
case AppShellFuncType.docker:
|
case AppShellFuncType.docker:
|
||||||
return '''
|
return '''
|
||||||
result=\$(docker version 2>&1 | grep "permission denied")
|
result=\$(docker version 2>&1 | grep "permission denied")
|
||||||
if [ "\$result" != "" ]; then
|
if [ "\$result" != "" ]; then
|
||||||
${_dockerCmds.join(_cmdDivider)}
|
\t${_dockerCmds.join(_cmdDivider)}
|
||||||
else
|
else
|
||||||
${_dockerCmds.map((e) => "sudo -S $e").join(_cmdDivider)}
|
\t${_dockerCmds.map((e) => "sudo -S $e").join(_cmdDivider)}
|
||||||
|
fi''';
|
||||||
|
case AppShellFuncType.process:
|
||||||
|
return '''
|
||||||
|
# Try sequencially
|
||||||
|
# main Linux: `ps -aux`
|
||||||
|
# BSD: `ps -ax`
|
||||||
|
# alpine: `ps -o pid,user,time,args`
|
||||||
|
#
|
||||||
|
# If there is any error, try another one
|
||||||
|
result=\$(ps -aux 2>&1 | grep "ps: ")
|
||||||
|
if [ "\$result" = "" ]; then
|
||||||
|
ps -aux
|
||||||
|
else
|
||||||
|
result=\$(ps -ax 2>&1 | grep "ps: ")
|
||||||
|
if [ "\$result" = "" ]; then
|
||||||
|
ps -ax
|
||||||
|
else
|
||||||
|
ps -o pid,user,time,args
|
||||||
|
fi
|
||||||
fi''';
|
fi''';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
// Models for `ps -aux`
|
import '../../../data/res/misc.dart';
|
||||||
|
|
||||||
// Each line
|
|
||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
|
/// Some field can be null due to incompatible format on `BSD` and `Alpine`
|
||||||
class Proc {
|
class Proc {
|
||||||
final String user;
|
final String user;
|
||||||
final int pid;
|
final int pid;
|
||||||
final double cpu;
|
final double? cpu;
|
||||||
final double mem;
|
final double? mem;
|
||||||
final String vsz;
|
final String? vsz;
|
||||||
final String rss;
|
final String? rss;
|
||||||
final String tty;
|
final String? tty;
|
||||||
final String stat;
|
final String? stat;
|
||||||
final String start;
|
final String? start;
|
||||||
final String time;
|
final String time;
|
||||||
final String command;
|
final String command;
|
||||||
|
|
||||||
@@ -65,7 +63,7 @@ class Proc {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return const JsonEncoder.withIndent(' ').convert(toJson());
|
return jsonEncoder.convert(toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
String get binary {
|
String get binary {
|
||||||
@@ -99,10 +97,10 @@ class PsResult {
|
|||||||
}
|
}
|
||||||
switch (sort) {
|
switch (sort) {
|
||||||
case ProcSortMode.cpu:
|
case ProcSortMode.cpu:
|
||||||
procs.sort((a, b) => b.cpu.compareTo(a.cpu));
|
procs.sort((a, b) => b.cpu?.compareTo(a.cpu ?? 0) ?? 0);
|
||||||
break;
|
break;
|
||||||
case ProcSortMode.mem:
|
case ProcSortMode.mem:
|
||||||
procs.sort((a, b) => b.mem.compareTo(a.mem));
|
procs.sort((a, b) => b.mem?.compareTo(a.mem ?? 0) ?? 0);
|
||||||
break;
|
break;
|
||||||
case ProcSortMode.pid:
|
case ProcSortMode.pid:
|
||||||
procs.sort((a, b) => a.pid.compareTo(b.pid));
|
procs.sort((a, b) => a.pid.compareTo(b.pid));
|
||||||
@@ -110,6 +108,9 @@ class PsResult {
|
|||||||
case ProcSortMode.user:
|
case ProcSortMode.user:
|
||||||
procs.sort((a, b) => a.user.compareTo(b.user));
|
procs.sort((a, b) => a.user.compareTo(b.user));
|
||||||
break;
|
break;
|
||||||
|
case ProcSortMode.name:
|
||||||
|
procs.sort((a, b) => a.binary.compareTo(b.binary));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return PsResult(procs: procs, error: err.isEmpty ? null : err);
|
return PsResult(procs: procs, error: err.isEmpty ? null : err);
|
||||||
}
|
}
|
||||||
@@ -119,5 +120,7 @@ enum ProcSortMode {
|
|||||||
cpu,
|
cpu,
|
||||||
mem,
|
mem,
|
||||||
pid,
|
pid,
|
||||||
user;
|
user,
|
||||||
|
name,
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import '../model/app/github_id.dart';
|
import '../model/app/github_id.dart';
|
||||||
@@ -52,3 +54,5 @@ const participants = <GhId>{
|
|||||||
'xgzxmytx',
|
'xgzxmytx',
|
||||||
'wind057',
|
'wind057',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const jsonEncoder = JsonEncoder.withIndent(' ');
|
||||||
|
|||||||
@@ -254,7 +254,10 @@ class _HomePageState extends State<HomePage>
|
|||||||
|
|
||||||
/// Encode [map] to String with indent `\t`
|
/// Encode [map] to String with indent `\t`
|
||||||
final text = const JsonEncoder.withIndent('\t').convert(map);
|
final text = const JsonEncoder.withIndent('\t').convert(map);
|
||||||
final result = await AppRoute.editor(text: text, langCode: 'json',).go(context);
|
final result = await AppRoute.editor(
|
||||||
|
text: text,
|
||||||
|
langCode: 'json',
|
||||||
|
).go(context);
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,18 +4,18 @@ import 'package:dartssh2/dartssh2.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
import 'package:toolbox/core/extension/context.dart';
|
import 'package:toolbox/core/extension/context.dart';
|
||||||
import 'package:toolbox/core/extension/stringx.dart';
|
|
||||||
import 'package:toolbox/core/extension/uint8list.dart';
|
import 'package:toolbox/core/extension/uint8list.dart';
|
||||||
import 'package:toolbox/core/utils/ui.dart';
|
|
||||||
import 'package:toolbox/data/model/server/proc.dart';
|
|
||||||
import 'package:toolbox/data/model/server/server_private_info.dart';
|
|
||||||
import 'package:toolbox/data/res/ui.dart';
|
|
||||||
import 'package:toolbox/view/widget/round_rect_card.dart';
|
|
||||||
import 'package:toolbox/view/widget/two_line_text.dart';
|
|
||||||
|
|
||||||
|
import '../../core/utils/ui.dart';
|
||||||
|
import '../../data/model/app/shell_func.dart';
|
||||||
|
import '../../data/model/server/proc.dart';
|
||||||
|
import '../../data/model/server/server_private_info.dart';
|
||||||
import '../../data/provider/server.dart';
|
import '../../data/provider/server.dart';
|
||||||
|
import '../../data/res/ui.dart';
|
||||||
import '../../locator.dart';
|
import '../../locator.dart';
|
||||||
import '../widget/custom_appbar.dart';
|
import '../widget/custom_appbar.dart';
|
||||||
|
import '../widget/round_rect_card.dart';
|
||||||
|
import '../widget/two_line_text.dart';
|
||||||
|
|
||||||
class ProcessPage extends StatefulWidget {
|
class ProcessPage extends StatefulWidget {
|
||||||
final ServerPrivateInfo spi;
|
final ServerPrivateInfo spi;
|
||||||
@@ -34,7 +34,12 @@ class _ProcessPageState extends State<ProcessPage> {
|
|||||||
|
|
||||||
PsResult _result = PsResult(procs: []);
|
PsResult _result = PsResult(procs: []);
|
||||||
int? _lastFocusId;
|
int? _lastFocusId;
|
||||||
ProcSortMode _procSortMode = ProcSortMode.mem;
|
|
||||||
|
// Issue #64
|
||||||
|
// In cpu mode, the process list will change in a high frequency.
|
||||||
|
// So user will easily know that the list is refreshed.
|
||||||
|
ProcSortMode _procSortMode = ProcSortMode.cpu;
|
||||||
|
List<ProcSortMode> _sortModes = ProcSortMode.values;
|
||||||
|
|
||||||
final _serverProvider = locator<ServerProvider>();
|
final _serverProvider = locator<ServerProvider>();
|
||||||
|
|
||||||
@@ -54,12 +59,23 @@ class _ProcessPageState extends State<ProcessPage> {
|
|||||||
|
|
||||||
Future<void> _refresh() async {
|
Future<void> _refresh() async {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
final result = await _client?.run('ps -aux'.withLangExport).string;
|
final result = await _client?.run(AppShellFuncType.process.exec).string;
|
||||||
if (result == null || result.isEmpty) {
|
if (result == null || result.isEmpty) {
|
||||||
showSnackBar(context, Text(_s.noResult));
|
showSnackBar(context, Text(_s.noResult));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_result = PsResult.parse(result, sort: _procSortMode);
|
_result = PsResult.parse(result, sort: _procSortMode);
|
||||||
|
|
||||||
|
// If there are any [Proc]'s data is not complete,
|
||||||
|
// the option to sort by cpu/mem will not be available.
|
||||||
|
final isAnyProcDataNotComplete =
|
||||||
|
_result.procs.any((e) => e.cpu == null || e.mem == null);
|
||||||
|
if (isAnyProcDataNotComplete) {
|
||||||
|
_sortModes.removeWhere((e) => e == ProcSortMode.cpu);
|
||||||
|
_sortModes.removeWhere((e) => e == ProcSortMode.mem);
|
||||||
|
} else {
|
||||||
|
_sortModes = ProcSortMode.values;
|
||||||
|
}
|
||||||
setState(() {});
|
setState(() {});
|
||||||
} else {
|
} else {
|
||||||
_timer.cancel();
|
_timer.cancel();
|
||||||
@@ -83,10 +99,8 @@ class _ProcessPageState extends State<ProcessPage> {
|
|||||||
},
|
},
|
||||||
icon: const Icon(Icons.sort),
|
icon: const Icon(Icons.sort),
|
||||||
initialValue: _procSortMode,
|
initialValue: _procSortMode,
|
||||||
itemBuilder: (_) => ProcSortMode.values
|
itemBuilder: (_) => _sortModes
|
||||||
.map(
|
.map((e) => PopupMenuItem(value: e, child: Text(e.name)))
|
||||||
(e) => PopupMenuItem(value: e, child: Text(e.name)),
|
|
||||||
)
|
|
||||||
.toList(),
|
.toList(),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
@@ -107,21 +121,7 @@ class _ProcessPageState extends State<ProcessPage> {
|
|||||||
child = ListView.builder(
|
child = ListView.builder(
|
||||||
itemCount: _result.procs.length,
|
itemCount: _result.procs.length,
|
||||||
padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 7),
|
padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 7),
|
||||||
itemBuilder: (ctx, idx) {
|
itemBuilder: (_, idx) => _buildListItem(_result.procs[idx]),
|
||||||
final proc = _result.procs[idx];
|
|
||||||
return AnimatedSwitcher(
|
|
||||||
duration: const Duration(milliseconds: 277),
|
|
||||||
switchInCurve: Curves.easeIn,
|
|
||||||
switchOutCurve: Curves.easeOut,
|
|
||||||
transitionBuilder: (child, animation) {
|
|
||||||
return FadeTransition(
|
|
||||||
opacity: animation,
|
|
||||||
child: child,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
child: _buildListItem(proc),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -148,14 +148,7 @@ class _ProcessPageState extends State<ProcessPage> {
|
|||||||
maxLines: 3,
|
maxLines: 3,
|
||||||
overflow: TextOverflow.fade,
|
overflow: TextOverflow.fade,
|
||||||
),
|
),
|
||||||
trailing: Row(
|
trailing: _buildItemTrail(proc),
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
children: [
|
|
||||||
TwoLineText(up: proc.cpu.toStringAsFixed(1), down: 'cpu'),
|
|
||||||
width13,
|
|
||||||
TwoLineText(up: proc.mem.toStringAsFixed(1), down: 'mem'),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
onTap: () => _lastFocusId = proc.pid,
|
onTap: () => _lastFocusId = proc.pid,
|
||||||
onLongPress: () {
|
onLongPress: () {
|
||||||
showRoundDialog(
|
showRoundDialog(
|
||||||
@@ -180,4 +173,18 @@ class _ProcessPageState extends State<ProcessPage> {
|
|||||||
key: ValueKey(proc.pid),
|
key: ValueKey(proc.pid),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget? _buildItemTrail(Proc proc) {
|
||||||
|
if (proc.cpu == null || proc.mem == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
TwoLineText(up: proc.cpu!.toStringAsFixed(1), down: 'cpu'),
|
||||||
|
width13,
|
||||||
|
TwoLineText(up: proc.mem!.toStringAsFixed(1), down: 'mem'),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user