Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/holger

mannequin/src/event_log.rs

use chrono::{DateTime, Utc};
use std::sync::{Arc, Mutex};

#[derive(Clone, Debug)]
pub struct LogEntry {
    pub timestamp: DateTime<Utc>,
    pub system: SystemKind,
    pub direction: Direction,
    pub summary: String,
    pub detail: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SystemKind {
    Pki,
    Oidc,
    Nexus,
}

impl SystemKind {
    pub fn label(&self) -> &'static str {
        match self {
            SystemKind::Pki => "Certificates Utgivare",
            SystemKind::Oidc => "OIDC / AD",
            SystemKind::Nexus => "Mock Nexus Registry",
        }
    }
}

#[derive(Clone, Debug)]
pub enum Direction {
    Inbound,
    Outbound,
}

/// Shared event log accessible from both server handlers and GUI.
#[derive(Clone)]
pub struct EventLog {
    entries: Arc<Mutex<Vec<LogEntry>>>,
}

impl Default for EventLog {
    fn default() -> Self {
        Self::new()
    }
}

impl EventLog {
    pub fn new() -> Self {
        Self {
            entries: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub fn log(
        &self,
        system: SystemKind,
        direction: Direction,
        summary: impl Into<String>,
        detail: Option<String>,
    ) {
        self.entries.lock().unwrap().push(LogEntry {
            timestamp: Utc::now(),
            system,
            direction,
            summary: summary.into(),
            detail,
        });
    }

    pub fn entries_for(&self, system: &SystemKind) -> Vec<LogEntry> {
        self.entries
            .lock()
            .unwrap()
            .iter()
            .filter(|e| &e.system == system)
            .cloned()
            .collect()
    }

    pub fn all_entries(&self) -> Vec<LogEntry> {
        self.entries.lock().unwrap().clone()
    }

    pub fn count_for(&self, system: &SystemKind) -> usize {
        self.entries
            .lock()
            .unwrap()
            .iter()
            .filter(|e| &e.system == system)
            .count()
    }
}