sprites 0.1.0

Official Rust SDK for Sprites - stateful sandbox environments from Fly.io
Documentation
//! Session management for sprites
//!
//! Sessions represent running or detachable command executions that can be
//! listed and reattached to.

use chrono::{DateTime, Utc};
use serde::Deserialize;
use std::time::Duration;

/// Information about an active session
#[derive(Debug, Clone, Deserialize)]
pub struct SessionInfo {
    /// Session ID
    pub id: String,

    /// Command being executed
    #[serde(alias = "cmd")]
    pub command: String,

    /// Working directory
    #[serde(default, alias = "cwd")]
    pub workdir: Option<String>,

    /// Creation timestamp
    #[serde(alias = "created_at")]
    pub created: DateTime<Utc>,

    /// Whether the session is active
    #[serde(default)]
    pub is_active: bool,

    /// Last activity timestamp
    #[serde(default)]
    pub last_activity: Option<DateTime<Utc>>,

    /// Whether this is a TTY session
    #[serde(default)]
    pub tty: bool,

    /// Data throughput (bytes per second)
    #[serde(default)]
    pub bytes_per_second: Option<f64>,
}

impl SessionInfo {
    /// Check if the session is currently active
    pub fn is_session_active(&self) -> bool {
        self.is_active
    }

    /// Get how long ago the session was last active
    ///
    /// Returns `None` if there's no last activity timestamp.
    pub fn get_activity_age(&self) -> Option<Duration> {
        self.last_activity.map(|last| {
            let now = Utc::now();
            let diff = now.signed_duration_since(last);
            Duration::from_secs(diff.num_seconds().max(0) as u64)
        })
    }

    /// Get the session ID
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get the command being executed
    pub fn command(&self) -> &str {
        &self.command
    }
}