use std::collections::HashMap;
use std::time::Instant;
use crate::control::security::identity::AuthenticatedIdentity;
use crate::types::TenantId;
use super::super::dlq::DeviceMetadata;
use super::super::rate_limit::{RateLimitConfig, SyncRateLimiter};
pub struct SyncSession {
pub session_id: String,
pub tenant_id: Option<TenantId>,
pub username: Option<String>,
pub identity: Option<AuthenticatedIdentity>,
pub authenticated: bool,
pub client_clock: HashMap<String, HashMap<String, u64>>,
pub server_clock: HashMap<String, u64>,
pub subscribed_shapes: Vec<String>,
pub mutations_processed: u64,
pub mutations_rejected: u64,
pub mutations_silent_dropped: u64,
pub last_activity: Instant,
pub created_at: Instant,
pub rate_limiter: SyncRateLimiter,
pub device_metadata: DeviceMetadata,
pub last_seen_mutation: HashMap<u64, u64>,
pub tracked_collections: std::collections::HashSet<(u64, String)>,
pub last_seen_lsn: u64,
}
impl SyncSession {
pub fn new(session_id: String) -> Self {
Self::with_rate_limit(session_id, &RateLimitConfig::default())
}
pub fn with_rate_limit(session_id: String, rate_config: &RateLimitConfig) -> Self {
let now = Instant::now();
Self {
session_id,
tenant_id: None,
username: None,
identity: None,
authenticated: false,
client_clock: HashMap::new(),
server_clock: HashMap::new(),
subscribed_shapes: Vec::new(),
mutations_processed: 0,
mutations_rejected: 0,
mutations_silent_dropped: 0,
last_activity: now,
created_at: now,
rate_limiter: SyncRateLimiter::new(rate_config),
device_metadata: DeviceMetadata::default(),
last_seen_mutation: HashMap::new(),
tracked_collections: std::collections::HashSet::new(),
last_seen_lsn: 0,
}
}
pub fn uptime_secs(&self) -> u64 {
self.created_at.elapsed().as_secs()
}
pub fn idle_secs(&self) -> u64 {
self.last_activity.elapsed().as_secs()
}
pub fn track_collection(&mut self, tenant_id: u64, collection: &str) {
self.tracked_collections
.insert((tenant_id, collection.to_string()));
}
}