pub struct Monitor {
pub config: MonitorConfig,
pub pauses: Pauses,
pub workdays: Workdays,
pub last_activity: Arc<Mutex<Instant>>,
pub activity_start: Arc<Mutex<Option<Instant>>>,
/* private fields */
}Expand description
The monitor’s moving parts: config, database handles, and the shared timestamps the input thread writes.
Fields§
§config: MonitorConfigThresholds and intervals; changes require a restart to apply.
pauses: PausesPause table handle.
workdays: WorkdaysWorkday table handle.
last_activity: Arc<Mutex<Instant>>When input was last seen; written by the listener thread.
activity_start: Arc<Mutex<Option<Instant>>>Start of the current sustained-activity streak, or None.
Set on the first input after quiet, cleared when a pause begins or a
workday is created. Requiring the streak to outlast
activity_threshold keeps a stray mouse nudge from starting a
workday.
Implementations§
Source§impl Monitor
impl Monitor
Sourcepub fn new(config: MonitorConfig) -> Result<Self>
pub fn new(config: MonitorConfig) -> Result<Self>
Opens the database handles and spawns the input listener thread.
The listener updates last_activity on every keyboard/mouse event
and sets activity_start when a streak begins. Listener errors are
logged, not fatal - the loop keeps running without input data rather
than dying silently in the background.
use kasl::libs::config::MonitorConfig;
use kasl::libs::monitor::Monitor;
let config = MonitorConfig::default();
let mut monitor = Monitor::new(config)?;
monitor.run().await?;Sourcepub async fn run(&mut self) -> Result<()>
pub async fn run(&mut self) -> Result<()>
Runs the polling loop until the process is stopped.
Each tick checks for recent input and dispatches on (state, activity): Active+quiet may open a pause, InPause+input closes it, Active+input keeps the workday going. Database errors inside a tick are logged and the loop continues - a transient lock must not kill the daemon.
pause_threshold == 0 disables the loop entirely (returns at once).
use kasl::libs::config::MonitorConfig;
use kasl::libs::monitor::Monitor;
let config = MonitorConfig {
poll_interval: 1000, // Check every second
pause_threshold: 120, // Pause after 2 minutes
activity_threshold: 30, // Workday starts after 30s
..Default::default()
};
let mut monitor = Monitor::new(config)?;
monitor.run().await?; // Runs indefinitelySourcepub fn detect_activity(&self) -> bool
pub fn detect_activity(&self) -> bool
True when input was seen within the last poll interval.
use kasl::libs::monitor::Monitor;
use kasl::libs::config::MonitorConfig;
let monitor = Monitor::new(MonitorConfig::default())?;
if monitor.detect_activity() {
println!("User is active");
} else {
println!("User appears inactive");
}Sourcepub fn ensure_workday_started(&mut self, today: NaiveDate) -> Result<()>
pub fn ensure_workday_started(&mut self, today: NaiveDate) -> Result<()>
Creates today’s workday once sustained activity outlasts
activity_threshold; the streak tracker is cleared afterwards so
only one workday per date is created.
// Called automatically during the monitoring loop. With
// activity_threshold = 30: first input sets the streak start,
// and 30s of continued input creates the workday.