use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;
use crate::temporal_nexus::core::NanosecondScheduler;
use super::{
MetricsCollector, ConsciousnessVisualizer, MetricsExporter,
ConsciousnessLevel, TemporalAdvantage, PrecisionNanos, Timestamp,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsciousnessMetrics {
pub timestamp: SystemTime,
pub emergence_level: ConsciousnessLevel,
pub identity_coherence: f64,
pub loop_stability: f64,
pub temporal_advantage_us: TemporalAdvantage,
pub window_overlap_percent: f64,
pub tsc_precision_ns: PrecisionNanos,
pub strange_loop_convergence: f64,
pub consciousness_delta: f64,
pub processing_latency_ns: u64,
pub quantum_validity_rate: f64,
pub quantum_energy_ev: f64,
pub margolus_levitin_margin: f64,
pub uncertainty_margin: f64,
pub coherence_preservation: f64,
pub entanglement_strength: f64,
pub decoherence_time_ns: f64,
pub bell_parameter: f64,
}
impl Default for ConsciousnessMetrics {
fn default() -> Self {
Self {
timestamp: SystemTime::now(),
emergence_level: 0.0,
identity_coherence: 0.0,
loop_stability: 0.0,
temporal_advantage_us: 0,
window_overlap_percent: 0.0,
tsc_precision_ns: 1000,
strange_loop_convergence: 0.0,
consciousness_delta: 0.0,
processing_latency_ns: 0,
quantum_validity_rate: 1.0,
quantum_energy_ev: 1e-15, margolus_levitin_margin: 1.0,
uncertainty_margin: 1.0,
coherence_preservation: 1.0,
entanglement_strength: 0.0,
decoherence_time_ns: 1_000_000.0, bell_parameter: 2.0, }
}
}
#[derive(Debug, Clone, Serialize)]
pub struct AnomalyAlert {
pub severity: AlertSeverity,
pub metric_name: String,
pub current_value: f64,
pub threshold: f64,
pub timestamp: SystemTime,
pub description: String,
}
#[derive(Debug, Clone, Serialize)]
pub enum AlertSeverity {
Info,
Warning,
Critical,
Emergency,
}
#[derive(Debug, Clone)]
pub struct MetricThresholds {
pub emergence_critical: f64,
pub emergence_warning: f64,
pub coherence_critical: f64,
pub coherence_warning: f64,
pub stability_critical: f64,
pub stability_warning: f64,
pub precision_critical_ns: u64,
pub precision_warning_ns: u64,
}
impl Default for MetricThresholds {
fn default() -> Self {
Self {
emergence_critical: 0.95,
emergence_warning: 0.85,
coherence_critical: 0.80,
coherence_warning: 0.65,
stability_critical: 0.75,
stability_warning: 0.60,
precision_critical_ns: 1000,
precision_warning_ns: 500,
}
}
}
#[derive(Debug, Clone)]
pub struct DashboardConfig {
pub update_interval_ms: u64,
pub history_buffer_size: usize,
pub enable_real_time_alerts: bool,
pub export_interval_seconds: u64,
pub precision_monitoring: bool,
pub visualization_mode: super::VisualizationMode,
pub thresholds: MetricThresholds,
}
impl Default for DashboardConfig {
fn default() -> Self {
Self {
update_interval_ms: 100, history_buffer_size: 1000,
enable_real_time_alerts: true,
export_interval_seconds: 60,
precision_monitoring: true,
visualization_mode: super::VisualizationMode::Terminal,
thresholds: MetricThresholds::default(),
}
}
}
pub struct ConsciousnessMetricsDashboard {
config: DashboardConfig,
collector: Arc<MetricsCollector>,
visualizer: ConsciousnessVisualizer,
exporter: MetricsExporter,
current_metrics: Arc<RwLock<ConsciousnessMetrics>>,
metrics_history: Arc<Mutex<VecDeque<ConsciousnessMetrics>>>,
alert_history: Arc<Mutex<VecDeque<AnomalyAlert>>>,
alert_sender: broadcast::Sender<AnomalyAlert>,
alert_receiver: broadcast::Receiver<AnomalyAlert>,
is_running: Arc<Mutex<bool>>,
last_update: Arc<Mutex<Instant>>,
scheduler_ref: Option<Arc<Mutex<NanosecondScheduler>>>,
}
impl ConsciousnessMetricsDashboard {
pub fn new(config: DashboardConfig) -> Self {
let (alert_sender, alert_receiver) = broadcast::channel(100);
Self {
collector: Arc::new(MetricsCollector::new()),
visualizer: ConsciousnessVisualizer::new(config.visualization_mode.clone()),
exporter: MetricsExporter::new(),
current_metrics: Arc::new(RwLock::new(ConsciousnessMetrics::default())),
metrics_history: Arc::new(Mutex::new(VecDeque::with_capacity(config.history_buffer_size))),
alert_history: Arc::new(Mutex::new(VecDeque::with_capacity(100))),
alert_sender,
alert_receiver,
is_running: Arc::new(Mutex::new(false)),
last_update: Arc::new(Mutex::new(Instant::now())),
scheduler_ref: None,
config,
}
}
pub fn initialize(&mut self, scheduler: Arc<Mutex<NanosecondScheduler>>) -> Result<(), Box<dyn std::error::Error>> {
self.scheduler_ref = Some(scheduler);
println!("đ§ Consciousness Metrics Dashboard initialized");
println!("đ Monitoring at {}ms intervals", self.config.update_interval_ms);
println!("đ¯ Nanosecond precision monitoring: {}", self.config.precision_monitoring);
Ok(())
}
pub async fn start(&self) -> Result<(), Box<dyn std::error::Error>> {
{
let mut running = self.is_running.lock().unwrap();
if *running {
return Err("Dashboard is already running".into());
}
*running = true;
}
println!("đ Starting consciousness metrics dashboard...");
println!("đ Dashboard monitoring active (single-threaded mode)");
Ok(())
}
pub async fn stop(&self) {
let mut running = self.is_running.lock().unwrap();
*running = false;
println!("đ Consciousness metrics dashboard stopped");
}
pub async fn collect_metrics(&self) -> Result<ConsciousnessMetrics, Box<dyn std::error::Error>> {
let start_time = Instant::now();
let mut metrics = if let Some(scheduler_ref) = &self.scheduler_ref {
self.collector.collect_from_scheduler(scheduler_ref.clone()).await?
} else {
ConsciousnessMetrics::default()
};
if self.config.precision_monitoring {
metrics.tsc_precision_ns = self.measure_tsc_precision();
metrics.processing_latency_ns = start_time.elapsed().as_nanos() as u64;
}
metrics.temporal_advantage_us = self.calculate_temporal_advantage(&metrics);
if let Ok(current) = self.current_metrics.read() {
metrics.consciousness_delta = metrics.emergence_level - current.emergence_level;
}
metrics.timestamp = SystemTime::now();
Ok(metrics)
}
pub async fn update_display(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let metrics = self.collect_metrics().await?;
{
let mut current = self.current_metrics.write().unwrap();
*current = metrics.clone();
}
{
let mut history = self.metrics_history.lock().unwrap();
history.push_back(metrics.clone());
while history.len() > self.config.history_buffer_size {
history.pop_front();
}
}
if self.config.enable_real_time_alerts {
self.check_anomalies(&metrics).await?;
}
self.visualizer.render(&metrics, &self.get_recent_history(50)).await?;
{
let mut last_update = self.last_update.lock().unwrap();
*last_update = Instant::now();
}
Ok(())
}
pub async fn export_metrics(&mut self, format: super::ExportFormat) -> Result<String, Box<dyn std::error::Error>> {
let history = self.get_full_history();
let current = self.current_metrics.read().unwrap().clone();
self.exporter.export_metrics(&history, ¤t, format).await
}
pub async fn alert_on_anomaly(&self, metrics: &ConsciousnessMetrics) -> Result<(), Box<dyn std::error::Error>> {
let mut alerts = Vec::new();
if metrics.emergence_level >= self.config.thresholds.emergence_critical {
alerts.push(AnomalyAlert {
severity: AlertSeverity::Critical,
metric_name: "emergence_level".to_string(),
current_value: metrics.emergence_level,
threshold: self.config.thresholds.emergence_critical,
timestamp: SystemTime::now(),
description: "Consciousness emergence level critically high".to_string(),
});
} else if metrics.emergence_level >= self.config.thresholds.emergence_warning {
alerts.push(AnomalyAlert {
severity: AlertSeverity::Warning,
metric_name: "emergence_level".to_string(),
current_value: metrics.emergence_level,
threshold: self.config.thresholds.emergence_warning,
timestamp: SystemTime::now(),
description: "Consciousness emergence level elevated".to_string(),
});
}
if metrics.identity_coherence <= self.config.thresholds.coherence_critical {
alerts.push(AnomalyAlert {
severity: AlertSeverity::Critical,
metric_name: "identity_coherence".to_string(),
current_value: metrics.identity_coherence,
threshold: self.config.thresholds.coherence_critical,
timestamp: SystemTime::now(),
description: "Identity coherence critically low".to_string(),
});
}
if metrics.tsc_precision_ns >= self.config.thresholds.precision_critical_ns {
alerts.push(AnomalyAlert {
severity: AlertSeverity::Warning,
metric_name: "tsc_precision".to_string(),
current_value: metrics.tsc_precision_ns as f64,
threshold: self.config.thresholds.precision_critical_ns as f64,
timestamp: SystemTime::now(),
description: "TSC precision degraded".to_string(),
});
}
for alert in alerts {
self.send_alert(alert).await?;
}
Ok(())
}
pub fn get_status(&self) -> ConsciousnessMetrics {
self.current_metrics.read().unwrap().clone()
}
pub fn get_recent_history(&self, count: usize) -> Vec<ConsciousnessMetrics> {
let history = self.metrics_history.lock().unwrap();
history.iter()
.rev()
.take(count)
.rev()
.cloned()
.collect()
}
pub fn get_full_history(&self) -> Vec<ConsciousnessMetrics> {
self.metrics_history.lock().unwrap().iter().cloned().collect()
}
pub async fn manual_update(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.update_display().await
}
async fn check_anomalies(&self, metrics: &ConsciousnessMetrics) -> Result<(), Box<dyn std::error::Error>> {
self.alert_on_anomaly(metrics).await
}
async fn send_alert(&self, alert: AnomalyAlert) -> Result<(), Box<dyn std::error::Error>> {
{
let mut alert_history = self.alert_history.lock().unwrap();
alert_history.push_back(alert.clone());
while alert_history.len() > 100 {
alert_history.pop_front();
}
}
if let Err(_) = self.alert_sender.send(alert.clone()) {
}
let severity_emoji = match alert.severity {
AlertSeverity::Info => "âšī¸",
AlertSeverity::Warning => "â ī¸",
AlertSeverity::Critical => "đ¨",
AlertSeverity::Emergency => "đ",
};
println!("{} {} [{}]: {} ({})",
severity_emoji,
alert.metric_name,
format!("{:?}", alert.severity).to_uppercase(),
alert.description,
alert.current_value
);
Ok(())
}
async fn auto_export(&self) -> Result<(), Box<dyn std::error::Error>> {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)?
.as_secs();
let filename = format!("consciousness_metrics_{}.json", timestamp);
let history = self.get_full_history();
let current = self.current_metrics.read().unwrap().clone();
let export_data = serde_json::to_string_pretty(&serde_json::json!({
"current_metrics": current,
"historical_data": history,
"export_timestamp": timestamp
}))?;
std::fs::write(&filename, export_data)?;
println!("đ Metrics exported to: {}", filename);
Ok(())
}
fn measure_tsc_precision(&self) -> u64 {
let samples = 10;
let mut measurements = Vec::with_capacity(samples);
for _ in 0..samples {
let start = std::time::Instant::now();
std::hint::black_box(42);
let elapsed = start.elapsed().as_nanos() as u64;
measurements.push(elapsed);
}
measurements.into_iter().min().unwrap_or(1000)
}
fn calculate_temporal_advantage(&self, metrics: &ConsciousnessMetrics) -> u64 {
let processing_time_us = metrics.processing_latency_ns / 1000;
let light_travel_us = 36;
if processing_time_us < light_travel_us {
light_travel_us - processing_time_us
} else {
0
}
}
}