Skip to main content

Monitor

Struct Monitor 

Source
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: MonitorConfig

Thresholds and intervals; changes require a restart to apply.

§pauses: Pauses

Pause table handle.

§workdays: Workdays

Workday 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

Source

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?;
Source

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 indefinitely
Source

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");
}
Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more