use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemContext {
pub timestamp: u64,
pub schema_version: String,
pub system: SystemIdentity,
pub hardware: HardwareContext,
pub software: SoftwareContext,
pub metrics: MetricsContext,
pub alerts: Vec<AlertContext>,
pub meta: ContextMeta,
}
impl SystemContext {
pub fn builder() -> SystemContextBuilder {
SystemContextBuilder::new()
}
pub fn to_json_pretty(&self) -> String {
serde_json::to_string_pretty(self).unwrap_or_default()
}
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap_or_default()
}
pub fn estimated_tokens(&self) -> usize {
self.to_json().len() / 4
}
pub fn minimal(&self) -> MinimalContext {
MinimalContext {
timestamp: self.timestamp,
hostname: self.system.hostname.clone(),
os: self.system.os_name.clone(),
cpu_count: self.hardware.cpu.as_ref().map(|c| c.thread_count),
gpu_count: self.hardware.gpus.len(),
memory_total_gb: self.hardware.memory.as_ref().map(|m| m.total_gb),
alert_count: self.alerts.len(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MinimalContext {
pub timestamp: u64,
pub hostname: Option<String>,
pub os: Option<String>,
pub cpu_count: Option<usize>,
pub gpu_count: usize,
pub memory_total_gb: Option<f64>,
pub alert_count: usize,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SystemIdentity {
pub hostname: Option<String>,
pub os_name: Option<String>,
pub os_version: Option<String>,
pub kernel_version: Option<String>,
pub architecture: Option<String>,
pub machine_id: Option<String>,
pub boot_time: Option<u64>,
pub uptime_seconds: Option<u64>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HardwareContext {
pub cpu: Option<CpuContext>,
pub gpus: Vec<GpuContext>,
pub memory: Option<MemoryContext>,
pub disks: Vec<DiskContext>,
pub network_interfaces: Vec<NetworkInterfaceContext>,
pub motherboard: Option<MotherboardContext>,
pub power_supply: Option<PowerSupplyContext>,
pub fans: Vec<FanContext>,
pub temperature_sensors: Vec<TemperatureSensorContext>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpuContext {
pub model: Option<String>,
pub vendor: Option<String>,
pub core_count: Option<usize>,
pub thread_count: usize,
pub base_frequency_mhz: Option<u32>,
pub max_frequency_mhz: Option<u32>,
pub l1_cache_kb: Option<u32>,
pub l2_cache_kb: Option<u32>,
pub l3_cache_kb: Option<u32>,
pub features: Option<Vec<String>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuContext {
pub index: usize,
pub name: String,
pub vendor: String,
pub vram_mb: Option<u64>,
pub driver_version: Option<String>,
pub compute_version: Option<String>,
pub pcie: Option<PcieContext>,
pub capabilities: Option<Vec<String>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PcieContext {
pub generation: Option<u8>,
pub width: Option<u8>,
pub bus_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryContext {
pub total_gb: f64,
pub memory_type: Option<String>,
pub speed_mhz: Option<u32>,
pub dimm_count: Option<u32>,
pub swap_total_gb: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiskContext {
pub device: String,
pub model: Option<String>,
pub disk_type: String,
pub size_gb: Option<f64>,
pub mount_point: Option<String>,
pub filesystem: Option<String>,
pub filesystem_error: Option<String>,
pub serial: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkInterfaceContext {
pub name: String,
pub interface_type: Option<String>,
pub mac_address: Option<String>,
pub ipv4_addresses: Option<Vec<String>>,
pub ipv6_addresses: Option<Vec<String>>,
pub speed_mbps: Option<u32>,
pub mtu: Option<u32>,
pub is_up: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MotherboardContext {
pub manufacturer: Option<String>,
pub product: Option<String>,
pub bios_vendor: Option<String>,
pub bios_version: Option<String>,
pub bios_date: Option<String>,
pub chassis_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PowerSupplyContext {
pub status: String,
pub on_ac_power: Option<bool>,
pub battery_percent: Option<f32>,
pub battery_status: Option<String>,
pub time_remaining_minutes: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FanContext {
pub name: String,
pub speed_rpm: Option<u32>,
pub speed_percent: Option<u8>,
pub min_rpm: Option<u32>,
pub max_rpm: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemperatureSensorContext {
pub name: String,
pub temperature_c: Option<f32>,
pub high_threshold_c: Option<f32>,
pub critical_threshold_c: Option<f32>,
pub location: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SoftwareContext {
pub process_count: usize,
pub services: Option<Vec<ServiceContext>>,
pub drivers: Vec<DriverContext>,
pub boot_config: Option<BootConfigContext>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceContext {
pub name: String,
pub display_name: Option<String>,
pub status: String,
pub start_type: Option<String>,
pub pid: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriverContext {
pub name: String,
pub version: Option<String>,
pub provider: Option<String>,
pub date: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BootConfigContext {
pub boot_mode: Option<String>,
pub secure_boot: Option<bool>,
pub boot_entries: Option<Vec<String>>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MetricsContext {
pub cpu: Option<CpuMetrics>,
pub gpus: Vec<GpuMetrics>,
pub memory: Option<MemoryMetrics>,
pub disks: Vec<DiskMetrics>,
pub network: Vec<NetworkMetrics>,
pub top_processes: Vec<ProcessMetrics>,
pub system_load: Option<SystemLoadMetrics>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpuMetrics {
pub utilization_percent: f32,
pub per_core_utilization: Vec<Option<f32>>,
pub frequency_mhz: Option<u32>,
pub temperature_c: Option<f32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuMetrics {
pub index: usize,
pub utilization_percent: Option<f32>,
pub memory_used_mb: Option<u64>,
pub memory_total_mb: Option<u64>,
pub temperature_c: Option<f32>,
pub power_watts: Option<f32>,
pub fan_speed_percent: Option<u8>,
pub clocks: Option<GpuClocks>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuClocks {
pub graphics_mhz: Option<u32>,
pub memory_mhz: Option<u32>,
pub sm_mhz: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryMetrics {
pub used_mb: u64,
pub free_mb: Option<u64>,
pub total_mb: u64,
pub cached_mb: Option<u64>,
pub buffers_mb: Option<u64>,
pub swap_used_mb: Option<u64>,
pub swap_total_mb: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiskMetrics {
pub device: String,
pub used_gb: Option<f64>,
pub free_gb: Option<f64>,
pub total_gb: Option<f64>,
pub read_bps: Option<u64>,
pub write_bps: Option<u64>,
pub iops_read: Option<u64>,
pub iops_write: Option<u64>,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkMetrics {
pub interface: String,
pub rx_bps: Option<f64>,
pub tx_bps: Option<f64>,
pub rx_bytes_total: u64,
pub tx_bytes_total: u64,
pub rx_packets: u64,
pub tx_packets: u64,
pub errors: u64,
pub dropped: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessMetrics {
pub pid: u32,
pub name: String,
pub cpu_percent: Option<f32>,
pub memory_mb: u64,
pub gpu_memory_mb: Option<u64>,
pub threads: u32,
pub user: Option<String>,
pub command: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemLoadMetrics {
pub load_1: Option<f64>,
pub load_5: Option<f64>,
pub load_15: Option<f64>,
pub running_processes: Option<u32>,
pub total_processes: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertContext {
pub id: String,
pub severity: String,
pub message: String,
pub source: String,
pub timestamp: u64,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContextMeta {
pub generation_time_ms: u64,
pub size_bytes: usize,
pub included_capabilities: Vec<String>,
pub excluded_capabilities: Vec<String>,
}
pub struct SystemContextBuilder {
context: SystemContext,
start_time: std::time::Instant,
}
impl SystemContextBuilder {
pub fn new() -> Self {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
Self {
context: SystemContext {
timestamp,
schema_version: "1.0.0".to_string(),
system: SystemIdentity::default(),
hardware: HardwareContext::default(),
software: SoftwareContext::default(),
metrics: MetricsContext::default(),
alerts: Vec::new(),
meta: ContextMeta::default(),
},
start_time: std::time::Instant::now(),
}
}
pub fn system(mut self, system: SystemIdentity) -> Self {
self.context.system = system;
self
}
pub fn hardware(mut self, hardware: HardwareContext) -> Self {
self.context.hardware = hardware;
self
}
pub fn software(mut self, software: SoftwareContext) -> Self {
self.context.software = software;
self
}
pub fn metrics(mut self, metrics: MetricsContext) -> Self {
self.context.metrics = metrics;
self
}
pub fn alerts(mut self, alerts: Vec<AlertContext>) -> Self {
self.context.alerts = alerts;
self
}
pub fn build(mut self) -> SystemContext {
let generation_time = self.start_time.elapsed().as_millis() as u64;
let json = serde_json::to_string(&self.context).unwrap_or_default();
self.context.meta.generation_time_ms = generation_time;
self.context.meta.size_bytes = json.len();
self.context
}
}
impl Default for SystemContextBuilder {
fn default() -> Self {
Self::new()
}
}