Files
pipelines/app/lib/features/settings/health/health_sections.dart
T

220 lines
7.2 KiB
Dart

import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../settings_theme.dart';
import 'health_models.dart';
import 'health_widgets.dart';
import 'health_storage.dart';
class HealthSections extends StatelessWidget {
const HealthSections({super.key, required this.stats});
final JsonMap stats;
Widget _errorOrRows(JsonMap data, List<(String, String)> rows) {
final error = data['error'];
return error is String && error.isNotEmpty ? Text(error) : HealthRows(rows);
}
Widget _cpu(JsonMap data) {
if (data['error'] case final String error when error.isNotEmpty) {
return HealthCard(title: 'CPU', child: Text(error));
}
final cores =
(data['per_core_percent'] as List?)?.whereType<num>().toList() ?? [];
return HealthCard(
title: 'CPU',
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
HealthMeter(
label: 'Overall',
value: healthRatio(data['usage_percent']),
),
const SizedBox(height: 10),
_errorOrRows(data, [
('Model', healthText(data['model'])),
('Logical cores', healthNumber(data['logical_cores'])),
('Physical cores', healthNumber(data['physical_cores'])),
(
'Load averages',
[
data['load1'],
data['load5'],
data['load15'],
].every((x) => x is num)
? '${healthNumber(data['load1'])} · ${healthNumber(data['load5'])} · ${healthNumber(data['load15'])}'
: '—',
),
]),
if (cores.isNotEmpty) ...[
const SizedBox(height: 8),
const Text('Per-core usage'),
const SizedBox(height: 4),
Wrap(
spacing: 3,
runSpacing: 3,
children: [
for (final core in cores)
SizedBox(
width: 18,
child: LinearProgressIndicator(value: healthRatio(core)),
),
],
),
],
],
),
);
}
Widget _memory(JsonMap data) => HealthCard(
title: 'Memory',
child: data['error'] is String
? Text(data['error'] as String)
: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
HealthMeter(
label: 'Used',
value: healthRatio(data['used_percent']),
),
const SizedBox(height: 8),
_errorOrRows(data, [
(
'Used',
'${healthBytes(data['used_bytes'])} of ${healthBytes(data['total_bytes'])}',
),
('Available', healthBytes(data['available_bytes'])),
(
'Swap',
'${healthBytes(data['swap_used_bytes'])} of ${healthBytes(data['swap_total_bytes'])}',
),
]),
const SizedBox(height: 8),
HealthMeter(
label: 'Swap',
value:
data['swap_total_bytes'] is num &&
(data['swap_total_bytes'] as num) > 0
? healthUsageRatio(
data['swap_used_bytes'],
data['swap_total_bytes'],
)
: null,
),
],
),
);
Widget _gpu(BuildContext context, JsonMap data) {
if (data['error'] case final String error when error.isNotEmpty) {
return HealthCard(title: 'GPU', child: Text(error));
}
final devices =
(data['devices'] as List?)?.whereType<Map>().map(healthMap).toList() ??
[];
if (data['available'] != true || devices.isEmpty) {
return HealthCard(title: 'GPU', child: Text(healthText(data['reason'])));
}
return HealthCard(
title: 'GPU',
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (final (index, device) in devices.indexed) ...[
if (index > 0) const Divider(),
Text(
healthText(device['name']),
style: settingsTextStyle(context, title: true),
),
HealthRows([('Vendor', healthText(device['vendor']))]),
HealthMeter(
label: 'Utilization',
value: healthRatio(device['utilization_percent']),
),
const SizedBox(height: 6),
HealthMeter(
label: 'Memory',
value:
(device['memory_total_bytes'] is num &&
(device['memory_total_bytes'] as num) > 0)
? healthUsageRatio(
device['memory_used_bytes'],
device['memory_total_bytes'],
)
: null,
),
Text(
'${healthBytes(device['memory_used_bytes'])} of ${healthBytes(device['memory_total_bytes'])} · ${healthNumber(device['temperature_c'])} °C',
),
],
],
),
);
}
Widget _data(JsonMap data) => HealthCard(
title: 'Pipelines data',
child: _errorOrRows(data, [
('Directory', healthText(data['dir'])),
('Database', healthBytes(data['db_bytes'])),
('Artifacts', healthBytes(data['artifacts_bytes'])),
('Total', healthBytes(data['total_bytes'])),
]),
);
Widget _process(JsonMap data) => HealthCard(
title: 'Process',
child: _errorOrRows(data, [
('PID', healthNumber(data['pid'])),
('RSS', healthBytes(data['rss_bytes'])),
('CPU', healthPercent(data['cpu_percent'])),
('Goroutines', healthNumber(data['goroutines'])),
('Heap', healthBytes(data['heap_bytes'])),
('Threads', healthNumber(data['threads'])),
('Go version', healthText(data['go_version'])),
('Build', healthText(data['build'])),
('Started', healthText(data['started_at'])),
]),
);
@override
Widget build(BuildContext context) {
final cpu = {
...healthMap(stats['cpu']),
...{
for (final key in ['load1', 'load5', 'load15'])
key: healthMap(stats['host'])[key],
},
};
final sections = [
_cpu(cpu),
_memory(healthMap(stats['memory'])),
_gpu(context, healthMap(stats['gpu'])),
HealthStorage(value: stats['storage']),
_data(healthMap(stats['data'])),
_process(healthMap(stats['process'])),
];
return LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth < 720) return Column(children: sections);
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (var column = 0; column < 2; column++) ...[
if (column > 0) const SizedBox(width: 12),
Expanded(
child: Column(
children: [
for (var i = column; i < sections.length; i += 2)
sections[i],
],
),
),
],
],
);
},
);
}
}