Get relay statistics
This commit is contained in:
12
.gitignore
vendored
12
.gitignore
vendored
@@ -56,6 +56,10 @@ app.*.map.json
|
||||
/android/local.properties
|
||||
/android/*.iml
|
||||
|
||||
# Android signing files (sensitive)
|
||||
/android/app/*.keystore
|
||||
/android/key.properties
|
||||
|
||||
# Fix for Android Studio APK filename bug
|
||||
build/app/outputs/flutter-apk/app--debug.apk
|
||||
|
||||
@@ -68,3 +72,11 @@ build/app/outputs/flutter-apk/app--debug.apk
|
||||
/packages/audio_service/android/.gradle
|
||||
/packages/audio_service/example/android/.gradle
|
||||
/target
|
||||
/nowser-master
|
||||
build_android.sh
|
||||
/Amber-master
|
||||
/amethyst-main
|
||||
/flotilla-master
|
||||
/nostr-signer-capacitor-plugin-main
|
||||
/nostr-rust
|
||||
/zapstore
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:aegis/utils/relay_service.dart';
|
||||
@@ -19,6 +21,10 @@ class _LocalRelayInfoState extends State<LocalRelayInfo> {
|
||||
int _databaseSize = 0;
|
||||
bool _isLoading = true;
|
||||
bool _isClearing = false;
|
||||
Map<String, dynamic>? _stats;
|
||||
DateTime? _sessionStartTime;
|
||||
Duration _currentSessionUptime = Duration.zero;
|
||||
Timer? _uptimeTimer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -31,6 +37,7 @@ class _LocalRelayInfoState extends State<LocalRelayInfo> {
|
||||
@override
|
||||
void dispose() {
|
||||
RelayService.instance.serverNotifier.removeListener(_onRelayStatusChanged);
|
||||
_stopUptimeTimer();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -38,21 +45,75 @@ class _LocalRelayInfoState extends State<LocalRelayInfo> {
|
||||
_loadRelayInfo();
|
||||
}
|
||||
|
||||
void _startUptimeTimer() {
|
||||
_uptimeTimer ??= Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (!mounted || _sessionStartTime == null) return;
|
||||
final duration = DateTime.now().difference(_sessionStartTime!);
|
||||
if (duration.inSeconds != _currentSessionUptime.inSeconds) {
|
||||
setState(() {
|
||||
_currentSessionUptime = duration;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _stopUptimeTimer() {
|
||||
_uptimeTimer?.cancel();
|
||||
_uptimeTimer = null;
|
||||
}
|
||||
|
||||
Future<void> _loadRelayInfo() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
_isRelayRunning = await RelayService.instance.isRunning();
|
||||
_relayUrl = await RelayService.instance.getUrl();
|
||||
_databaseSize = await RelayService.instance.getDatabaseSize();
|
||||
final isRunning = await RelayService.instance.isRunning();
|
||||
final relayUrl = await RelayService.instance.getUrl();
|
||||
final databaseSize = await RelayService.instance.getDatabaseSize();
|
||||
final stats = await RelayService.instance.getStats();
|
||||
|
||||
DateTime? sessionStart;
|
||||
if (isRunning) {
|
||||
RelayService.instance.recordSessionStartIfUnset();
|
||||
sessionStart = RelayService.instance.sessionStartTime;
|
||||
} else {
|
||||
RelayService.instance.clearSessionStart();
|
||||
sessionStart = null;
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isRelayRunning = isRunning;
|
||||
_relayUrl = relayUrl;
|
||||
_databaseSize = databaseSize;
|
||||
_stats = stats;
|
||||
_sessionStartTime = sessionStart;
|
||||
_currentSessionUptime = sessionStart != null
|
||||
? DateTime.now().difference(sessionStart)
|
||||
: Duration.zero;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
if (sessionStart != null) {
|
||||
_startUptimeTimer();
|
||||
} else {
|
||||
_stopUptimeTimer();
|
||||
}
|
||||
} catch (e) {
|
||||
AegisLogger.error("Failed to load relay info", e);
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
_isRelayRunning = false;
|
||||
_sessionStartTime = null;
|
||||
_currentSessionUptime = Duration.zero;
|
||||
_stats = null;
|
||||
});
|
||||
}
|
||||
RelayService.instance.clearSessionStart();
|
||||
_stopUptimeTimer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +129,38 @@ class _LocalRelayInfoState extends State<LocalRelayInfo> {
|
||||
}
|
||||
}
|
||||
|
||||
String _formatNumber(int number) {
|
||||
if (number >= 1000000) {
|
||||
return '${(number / 1000000).toStringAsFixed(1)}M';
|
||||
} else if (number >= 1000) {
|
||||
return '${(number / 1000).toStringAsFixed(1)}K';
|
||||
}
|
||||
return number.toString();
|
||||
}
|
||||
|
||||
String _formatDuration(int seconds) {
|
||||
if (seconds <= 0) {
|
||||
return '0s';
|
||||
}
|
||||
final duration = Duration(seconds: seconds);
|
||||
if (duration.inDays > 0) {
|
||||
final days = duration.inDays;
|
||||
final hours = duration.inHours % 24;
|
||||
return hours > 0 ? '${days}d ${hours}h' : '${days}d';
|
||||
}
|
||||
if (duration.inHours > 0) {
|
||||
final hours = duration.inHours;
|
||||
final minutes = duration.inMinutes % 60;
|
||||
return minutes > 0 ? '${hours}h ${minutes}m' : '${hours}h';
|
||||
}
|
||||
if (duration.inMinutes > 0) {
|
||||
final minutes = duration.inMinutes;
|
||||
final secs = duration.inSeconds % 60;
|
||||
return secs > 0 ? '${minutes}m ${secs}s' : '${minutes}m';
|
||||
}
|
||||
return '${duration.inSeconds}s';
|
||||
}
|
||||
|
||||
Future<void> _handleClearDatabase() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
@@ -131,6 +224,7 @@ class _LocalRelayInfoState extends State<LocalRelayInfo> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Widget _buildInfoItem(String label, Widget value) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
@@ -180,6 +274,13 @@ class _LocalRelayInfoState extends State<LocalRelayInfo> {
|
||||
fontWeight: FontWeight.w400,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: 'Refresh',
|
||||
onPressed: _isLoading ? null : () => _loadRelayInfo(),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
@@ -237,7 +338,28 @@ class _LocalRelayInfoState extends State<LocalRelayInfo> {
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_stats != null)
|
||||
_buildInfoItem(
|
||||
'Total Events',
|
||||
Text(
|
||||
_formatNumber(_stats!['totalEvents'] as int? ?? 0),
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_sessionStartTime != null)
|
||||
_buildInfoItem(
|
||||
'Service Uptime',
|
||||
Text(
|
||||
_formatDuration(_currentSessionUptime.inSeconds),
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 32),
|
||||
// Clear Database Button
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: SizedBox(
|
||||
|
||||
@@ -18,6 +18,7 @@ class RelayService {
|
||||
// Default port: 18081 for desktop, 8081 for mobile
|
||||
int _port = PlatformUtils.isDesktop ? 18081 : 8081;
|
||||
String? _relayUrl;
|
||||
DateTime? _sessionStartTime;
|
||||
|
||||
/// Get the relay URL (for client connections)
|
||||
String get relayUrl {
|
||||
@@ -31,6 +32,19 @@ class RelayService {
|
||||
/// Get the relay port
|
||||
String get port => _port.toString();
|
||||
|
||||
/// Get the current session start time (if known)
|
||||
DateTime? get sessionStartTime => _sessionStartTime;
|
||||
|
||||
/// Ensure we have a session start timestamp when relay is confirmed running
|
||||
void recordSessionStartIfUnset() {
|
||||
_sessionStartTime ??= DateTime.now();
|
||||
}
|
||||
|
||||
/// Clear the cached session start timestamp
|
||||
void clearSessionStart() {
|
||||
_sessionStartTime = null;
|
||||
}
|
||||
|
||||
/// Get default port based on platform
|
||||
static int get _defaultPort => PlatformUtils.isDesktop ? 18081 : 8081;
|
||||
|
||||
@@ -54,6 +68,7 @@ class RelayService {
|
||||
AegisLogger.warning("⚠️ Relay is already running (URL retrieval failed, using default: $_relayUrl)");
|
||||
}
|
||||
serverNotifier.value = true;
|
||||
recordSessionStartIfUnset();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -65,6 +80,7 @@ class RelayService {
|
||||
// Start the relay (using async version)
|
||||
_relayUrl = await rust_relay.startRelay(host: _host, port: _port, dbPath: dbPath);
|
||||
serverNotifier.value = true;
|
||||
_sessionStartTime = DateTime.now();
|
||||
AegisLogger.info("✅ Nostr relay started on $_relayUrl");
|
||||
} catch (e) {
|
||||
AegisLogger.error("🚨 Failed to start relay", e);
|
||||
@@ -84,6 +100,7 @@ class RelayService {
|
||||
await rust_relay.stopRelay();
|
||||
serverNotifier.value = false;
|
||||
_relayUrl = null;
|
||||
_sessionStartTime = null;
|
||||
AegisLogger.info("✅ Relay stopped");
|
||||
} catch (e) {
|
||||
AegisLogger.error("🚨 Failed to stop relay", e);
|
||||
@@ -188,5 +205,22 @@ class RelayService {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get relay statistics (event-focused)
|
||||
/// Note: This requires regenerating flutter_rust_bridge code after adding the Rust API
|
||||
Future<Map<String, dynamic>?> getStats() async {
|
||||
try {
|
||||
final dbPath = await getDatabasePath();
|
||||
final stats = await rust_relay.getRelayStats(dbPath: dbPath);
|
||||
final totalEvents = int.parse(stats.totalEvents.toString());
|
||||
|
||||
return {
|
||||
'totalEvents': totalEvents,
|
||||
};
|
||||
} catch (e) {
|
||||
AegisLogger.error("🚨 Failed to get relay stats", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Submodule packages/nostr-rust-flutter-plugin updated: 4354ded7b0...e1c3f28dbc
Reference in New Issue
Block a user