use serde::{Deserialize, Serialize};
use spacedb_meter::Resource;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HomeObs {
pub id: String,
pub region: String,
pub online: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ShardObs {
pub id: String,
pub collection: String,
pub reachable_replicas: u32,
pub target_replicas: u32,
pub durable_floor: u32,
pub size_bytes: u64,
}
impl ShardObs {
pub fn lost(&self) -> bool {
self.reachable_replicas < self.durable_floor
}
pub fn at_risk(&self) -> bool {
self.reachable_replicas == self.durable_floor
}
pub fn under_replicated(&self) -> bool {
self.reachable_replicas < self.target_replicas
}
pub fn over_replicated(&self) -> bool {
self.reachable_replicas > self.target_replicas
}
pub fn excess(&self) -> u32 {
self.reachable_replicas.saturating_sub(self.target_replicas)
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StrongObs {
pub collection: String,
pub members_online: u32,
pub members_total: u32,
}
impl StrongObs {
pub fn has_quorum(&self) -> bool {
self.members_online > self.members_total / 2
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LagObs {
pub collection: String,
pub lag_ops: u64,
pub region: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CapabilityObs {
pub bearer: String,
pub scope: String,
pub ops: String,
pub expiry: Option<u64>,
pub budget_micro_mata: Option<u64>,
pub revoked: bool,
}
impl CapabilityObs {
pub fn is_agent(&self) -> bool {
self.bearer.starts_with("did:agent:")
}
pub fn expires_within(&self, now: u64, window: u64) -> bool {
matches!(self.expiry, Some(e) if e >= now && e - now <= window)
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuditObs {
pub actor: String,
pub action: String,
pub at: u64,
pub allowed: bool,
}
impl AuditObs {
pub fn is_agent(&self) -> bool {
self.actor.starts_with("did:agent:")
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SettledObs {
pub host_did: String,
pub settles_to_did: String,
pub resource: Resource,
pub micro_mata: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AgentBudgetObs {
pub agent: String,
pub remaining: u64,
pub limit: u64,
}