alert history work

- dedup error handling
- alert now clears with observation_retenstion_seconds
- evaluate recent alerts once per server per 5s
- some iter stuff, some type stuff, and some store api stuff.
This commit is contained in:
lda committed 2026-09-11 04:23:39 +07:00 Verified
1 parent 50f21a5be2
commit 823bc81d07
29 files changed
+750 -294

No files matched your search

+21 -42
View File
@@ -74,9 +74,9 @@ pub async fn enroll(
match state.store.enroll(&req.enroll_token).await {
Ok(issued) => {
info!(agent_id = %issued.agent_id, "agent enrollment accepted");
if let Err(err) = state
state
.store
.append_audit_event(AuditEventInput {
.audit(AuditEventInput {
actor_type: "agent".into(),
actor_id: Some(issued.agent_id.clone()),
agent_id: Some(issued.agent_id.clone()),
@@ -87,10 +87,7 @@ pub async fn enroll(
message: "agent enrollment accepted".into(),
metadata: serde_json::json!({}),
})
.await
{
warn!(error = %err, "failed to append audit event for enroll success");
}
.await;
Ok((
StatusCode::OK,
Json(EnrollResponse {
@@ -102,9 +99,9 @@ pub async fn enroll(
}
Err(err) => {
warn!(error = %err, "agent enrollment rejected");
if let Err(audit_err) = state
state
.store
.append_audit_event(AuditEventInput {
.audit(AuditEventInput {
actor_type: "agent".into(),
actor_id: None,
agent_id: None,
@@ -115,10 +112,7 @@ pub async fn enroll(
message: err.to_string(),
metadata: serde_json::json!({}),
})
.await
{
warn!(error = %audit_err, "failed to append audit event for enroll rejection");
}
.await;
Err(ApiError::new(
StatusCode::UNAUTHORIZED,
"enrollment_rejected",
@@ -145,9 +139,9 @@ pub async fn issue_enroll_token(
expires_at_unix = issued.expires_at_unix,
"issued enroll token"
);
if let Err(err) = state
state
.store
.append_audit_event(AuditEventInput {
.audit(AuditEventInput {
actor_type: "admin_api".into(),
actor_id: None,
agent_id: None,
@@ -161,10 +155,7 @@ pub async fn issue_enroll_token(
"expires_at_unix": issued.expires_at_unix,
}),
})
.await
{
warn!(error = %err, "failed to append audit event for token issuance");
}
.await;
Ok((
StatusCode::OK,
Json(IssueEnrollTokenResponse {
@@ -189,9 +180,9 @@ pub async fn list_enroll_tokens(
) -> Result<impl IntoResponse, ApiError> {
match state.store.list_enroll_tokens().await {
Ok(tokens) => {
if let Err(err) = state
state
.store
.append_audit_event(AuditEventInput {
.audit(AuditEventInput {
actor_type: "admin_api".into(),
actor_id: None,
agent_id: None,
@@ -204,10 +195,7 @@ pub async fn list_enroll_tokens(
"count": tokens.len(),
}),
})
.await
{
warn!(error = %err, "failed to append audit event for token listing");
}
.await;
let body = tokens
.into_iter()
.map(|t| EnrollTokenStatus {
@@ -235,9 +223,9 @@ pub async fn revoke_enroll_token(
) -> Result<impl IntoResponse, ApiError> {
match state.store.revoke_enroll_token(&token).await {
Ok(revoked) => {
if let Err(err) = state
state
.store
.append_audit_event(AuditEventInput {
.audit(AuditEventInput {
actor_type: "admin_api".into(),
actor_id: None,
agent_id: None,
@@ -256,10 +244,7 @@ pub async fn revoke_enroll_token(
},
metadata: serde_json::json!({ "token": token }),
})
.await
{
warn!(error = %err, "failed to append audit event for token revoke");
}
.await;
Ok((
StatusCode::OK,
Json(RevokeEnrollTokenResponse { token, revoked }),
@@ -289,9 +274,9 @@ pub async fn revoke_agent(
}
}
if let Err(err) = state
state
.store
.append_audit_event(AuditEventInput {
.audit(AuditEventInput {
actor_type: "admin_api".into(),
actor_id: None,
agent_id: Some(agent_id.clone()),
@@ -310,10 +295,7 @@ pub async fn revoke_agent(
},
metadata: serde_json::json!({ "agent_id": agent_id }),
})
.await
{
warn!(error = %err, "failed to append audit event for agent revoke");
}
.await;
Ok((
StatusCode::OK,
@@ -349,9 +331,9 @@ pub async fn set_agent_nickname(
.await
{
Ok(updated) => {
if let Err(err) = state
state
.store
.append_audit_event(AuditEventInput {
.audit(AuditEventInput {
actor_type: "admin_api".into(),
actor_id: None,
agent_id: Some(agent_id.clone()),
@@ -373,10 +355,7 @@ pub async fn set_agent_nickname(
"nickname": normalized,
}),
})
.await
{
warn!(error = %err, "failed to append audit event for nickname set");
}
.await;
Ok((
StatusCode::OK,
+105 -82
View File
@@ -4,11 +4,12 @@ use axum::Json;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use futures_util::StreamExt;
use tracing::warn;
use wakey_agent::protocol::{AgentCommand, InventoryRequest, WakeRequest};
use crate::api::ApiError;
use crate::api::commands::relay_agent_command;
use crate::api::commands::{RelayCommandResponse, relay_agent_command};
use crate::runtime::AppState;
mod build;
@@ -26,6 +27,9 @@ use types::{
RefreshFleetDevicesResponse, WakeFleetDeviceRequest, WakeFleetDeviceResponse,
};
const MAX_REFRESH_AGENTS: usize = 128;
const REFRESH_CONCURRENCY: usize = 8;
pub async fn list_fleet_devices(
State(state): State<AppState>,
Query(query): Query<ListFleetDevicesQuery>,
@@ -55,88 +59,43 @@ pub async fn refresh_fleet_devices(
};
agent_ids.sort();
agent_ids.dedup();
let mut results = Vec::with_capacity(agent_ids.len());
let mut total_accepted = 0usize;
for agent_id in agent_ids {
let response = relay_agent_command(
&state,
&agent_id,
AgentCommand::Inventory(InventoryRequest {
query: None,
name: None,
ips: Vec::new(),
devs: Vec::new(),
nuds: Vec::new(),
macs: Vec::new(),
}),
req.timeout_ms,
)
.await;
match response {
Ok(response) if response.status == "ok" => {
let Some(result) = response.result else {
results.push(RefreshFleetAgentResult {
agent_id,
status: "error".into(),
accepted: 0,
error: Some("inventory command returned no result".into()),
});
continue;
};
match serde_json::from_value::<wakey_core::DeviceInventory>(result)
.map(|i| i.devices)
{
Ok(devices) => {
match state
.store
.replace_agent_device_snapshot(&agent_id, &devices)
.await
{
Ok(accepted) => {
total_accepted = total_accepted.saturating_add(accepted);
results.push(RefreshFleetAgentResult {
agent_id,
status: "ok".into(),
accepted,
error: None,
});
}
Err(err) => results.push(RefreshFleetAgentResult {
agent_id,
status: "error".into(),
accepted: 0,
error: Some(err.to_string()),
}),
}
}
Err(err) => results.push(RefreshFleetAgentResult {
agent_id,
status: "error".into(),
accepted: 0,
error: Some(err.to_string()),
}),
}
}
Ok(response) => results.push(RefreshFleetAgentResult {
agent_id,
status: "error".into(),
accepted: 0,
error: response
.error
.map(|error| error.message)
.or_else(|| Some("inventory command failed".into())),
}),
Err(err) => results.push(RefreshFleetAgentResult {
agent_id,
status: "error".into(),
accepted: 0,
error: Some(format!("{}: {}", err.status, err.message)),
}),
}
if agent_ids.len() > MAX_REFRESH_AGENTS {
warn!(
requested = agent_ids.len(),
capped = MAX_REFRESH_AGENTS,
"capping fleet refresh agent count"
);
agent_ids.truncate(MAX_REFRESH_AGENTS);
}
let results = futures_util::stream::iter(agent_ids.into_iter().map(|agent_id| {
let state = state.clone();
async move {
let response = relay_agent_command(
&state,
&agent_id,
AgentCommand::Inventory(InventoryRequest {
query: None,
name: None,
ips: Vec::new(),
devs: Vec::new(),
nuds: Vec::new(),
macs: Vec::new(),
}),
req.timeout_ms,
)
.await;
refresh_fleet_agent_result(&state, &agent_id, response).await
}
}))
.buffer_unordered(REFRESH_CONCURRENCY)
.collect::<Vec<_>>()
.await;
let mut results = results;
results.sort_by(|a, b| a.agent_id.cmp(&b.agent_id));
let total_accepted = results.iter().map(|result| result.accepted).sum::<usize>();
Ok((
StatusCode::OK,
Json(RefreshFleetDevicesResponse {
@@ -146,6 +105,70 @@ pub async fn refresh_fleet_devices(
))
}
async fn refresh_fleet_agent_result(
state: &AppState,
agent_id: &str,
response: Result<RelayCommandResponse, ApiError>,
) -> RefreshFleetAgentResult {
match response {
Ok(response) if response.status == "ok" => {
let Some(result) = response.result else {
return RefreshFleetAgentResult {
agent_id: agent_id.to_string(),
status: "error".into(),
accepted: 0,
error: Some("inventory command returned no result".into()),
};
};
match serde_json::from_value::<wakey_core::DeviceInventory>(result)
.map(|inventory| inventory.devices)
{
Ok(devices) => {
match state
.store
.replace_agent_device_snapshot(agent_id, &devices)
.await
{
Ok(accepted) => RefreshFleetAgentResult {
agent_id: agent_id.to_string(),
status: "ok".into(),
accepted,
error: None,
},
Err(err) => RefreshFleetAgentResult {
agent_id: agent_id.to_string(),
status: "error".into(),
accepted: 0,
error: Some(err.to_string()),
},
}
}
Err(err) => RefreshFleetAgentResult {
agent_id: agent_id.to_string(),
status: "error".into(),
accepted: 0,
error: Some(err.to_string()),
},
}
}
Ok(response) => RefreshFleetAgentResult {
agent_id: agent_id.to_string(),
status: "error".into(),
accepted: 0,
error: response
.error
.map(|error| error.message)
.or_else(|| Some("inventory command failed".into())),
},
Err(err) => RefreshFleetAgentResult {
agent_id: agent_id.to_string(),
status: "error".into(),
accepted: 0,
error: Some(format!("{}: {}", err.status, err.message)),
},
}
}
pub async fn wake_fleet_device(
State(state): State<AppState>,
Json(req): Json<WakeFleetDeviceRequest>,
@@ -243,7 +266,7 @@ async fn load_fleet_devices(
let sessions = state.sessions.read().await;
sessions.keys().cloned().collect::<BTreeSet<_>>()
};
let agents = state.store.list_agents_with_nicknames().await;
let agents = state.store.list_agents_with_nicknames().await?;
let agent_status = agents
.into_iter()
.map(|(agent_id, nickname)| {