add reminder rollup RPC and broker query
Surface reminder activity statistics (scheduled, delivered, pending counts) for each agent over configurable time windows. Needed by the per-agent stats page to display reminder metrics. Adds: - ReminderStats struct and ReminderRollup request/response variants - Broker::reminder_rollup_for(agent, since_secs) method - Agent and manager socket handlers for the new RPC - SocketReply mapping for response conversion
This commit is contained in:
parent
4715e88fff
commit
91bfa269fd
5 changed files with 89 additions and 0 deletions
|
|
@ -207,6 +207,14 @@ async fn dispatch(req: &AgentRequest, agent: &str, coord: &Arc<Coordinator>) ->
|
|||
},
|
||||
}
|
||||
}
|
||||
AgentRequest::ReminderRollup { since_secs } => {
|
||||
match coord.broker.reminder_rollup_for(agent, *since_secs) {
|
||||
Ok(stats) => AgentResponse::ReminderRollup(stats),
|
||||
Err(e) => AgentResponse::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
AgentRequest::Whoami => AgentResponse::Whoami {
|
||||
name: agent.to_owned(),
|
||||
role: "agent".to_owned(),
|
||||
|
|
|
|||
|
|
@ -583,6 +583,43 @@ impl Broker {
|
|||
Ok(u64::try_from(n).unwrap_or(0))
|
||||
}
|
||||
|
||||
/// Reminder rollup stats for an agent over a time window. Returns
|
||||
/// counts of scheduled, delivered, and pending reminders created
|
||||
/// in the last `since_secs` seconds (0 = all reminders).
|
||||
pub fn reminder_rollup_for(&self, agent: &str, since_secs: u64) -> Result<hive_sh4re::ReminderStats> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let cutoff_time = if since_secs > 0 {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()
|
||||
.map(|d| d.as_secs() as i64)
|
||||
.unwrap_or(0);
|
||||
now - since_secs as i64
|
||||
} else {
|
||||
i64::MIN
|
||||
};
|
||||
let scheduled: i64 = conn.query_row(
|
||||
"SELECT COUNT(*) FROM reminders WHERE agent = ?1 AND created_at >= ?2",
|
||||
params![agent, cutoff_time],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
let delivered: i64 = conn.query_row(
|
||||
"SELECT COUNT(*) FROM reminders WHERE agent = ?1 AND created_at >= ?2 AND sent_at IS NOT NULL",
|
||||
params![agent, cutoff_time],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
let pending: i64 = conn.query_row(
|
||||
"SELECT COUNT(*) FROM reminders WHERE agent = ?1 AND created_at >= ?2 AND sent_at IS NULL",
|
||||
params![agent, cutoff_time],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
Ok(hive_sh4re::ReminderStats {
|
||||
scheduled: u64::try_from(scheduled).unwrap_or(0),
|
||||
delivered: u64::try_from(delivered).unwrap_or(0),
|
||||
pending: u64::try_from(pending).unwrap_or(0),
|
||||
})
|
||||
}
|
||||
|
||||
/// Delete a reminder by id. Returns the number of rows removed (0
|
||||
/// when the id never existed or was already delivered). Hard
|
||||
/// delete rather than soft so the row doesn't linger and confuse a
|
||||
|
|
|
|||
|
|
@ -369,6 +369,14 @@ async fn dispatch(req: &ManagerRequest, coord: &Arc<Coordinator>) -> ManagerResp
|
|||
},
|
||||
}
|
||||
}
|
||||
ManagerRequest::ReminderRollup { since_secs } => {
|
||||
match coord.broker.reminder_rollup_for(MANAGER_AGENT, *since_secs) {
|
||||
Ok(stats) => ManagerResponse::ReminderRollup(stats),
|
||||
Err(e) => ManagerResponse::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
ManagerRequest::Whoami => ManagerResponse::Whoami {
|
||||
name: MANAGER_AGENT.to_owned(),
|
||||
role: "manager".to_owned(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue