Trying to save some events

This commit is contained in:
lda
2026-04-30 13:59:18 +07:00 Verified
parent 2b98662bcc
commit b20f53c6ba
11 changed files with 377 additions and 192 deletions
+38 -5
View File
@@ -323,6 +323,13 @@ pub async fn wake_fleet_device(
"selected route agent is not connected",
));
}
if !route.wakeable {
return Err(json_error(
StatusCode::BAD_REQUEST,
"wake_route_unavailable",
"selected route is not wakeable",
));
}
let mac = mac.parse().map_err(|err| {
json_error(
@@ -530,6 +537,7 @@ fn add_observation_to_entry(
observation: AgentDeviceObservation,
context: &FleetBuildContext,
) {
let observation_offline = observation_is_offline(&observation);
if let Some(summary) = observation_known_device(&observation, context)
&& entry.known_device.is_none()
{
@@ -540,7 +548,7 @@ fn add_observation_to_entry(
if let Some(mac) = observation.mac.as_deref() {
entry.macs.insert(mac.to_string());
}
if let Some(ip) = observation.ip.as_deref() {
if !observation_offline && let Some(ip) = observation.ip.as_deref() {
entry.ips.insert(ip.to_string());
}
if let Some(hostname) = observation.hostname.as_deref() {
@@ -595,7 +603,7 @@ fn add_observation_to_entry(
observation.ip.as_deref(),
&observation.kind,
);
let wakeable = status.connected && observation.mac.is_some();
let wakeable = status.connected && observation.mac.is_some() && !observation_offline;
entry.routes.insert(
route_id.clone(),
FleetWakeRoute {
@@ -612,7 +620,7 @@ fn add_observation_to_entry(
},
);
if let Some(route) = entry.routes.get_mut(&route_id) {
route.wakeable = route.connected && route.mac.is_some();
route.wakeable = route.connected && route.mac.is_some() && !observation_offline;
}
}
@@ -744,12 +752,17 @@ fn inventory_result_to_observations(
fn observation_presence_rank(observation: &AgentDeviceObservation) -> u8 {
match observation.last_action.as_str() {
"remove" => 0,
"add" | "old" | "update" => 2,
"remove" | "failed" => 0,
"permanent" | "reachable" => 3,
"stale" | "add" | "old" | "update" => 2,
_ => 1,
}
}
fn observation_is_offline(observation: &AgentDeviceObservation) -> bool {
matches!(observation.last_action.as_str(), "remove" | "failed")
}
fn rank_presence(rank: u8) -> &'static str {
match rank {
3 => "online",
@@ -932,6 +945,26 @@ mod tests {
assert!(!devices[0].route_candidates[0].wakeable);
}
#[test]
fn offline_observation_does_not_advertise_current_ip_or_wake_route() {
let mut offline = observation(
"agent-a",
Some("aa:bb:cc:dd:ee:ff"),
Some("192.168.1.2"),
20,
);
offline.kind = "neigh".into();
offline.last_action = "remove".into();
let devices = build_fleet_devices(Vec::new(), vec![offline], &context(&["agent-a"]));
assert_eq!(devices.len(), 1);
assert!(devices[0].ips.is_empty());
assert_eq!(devices[0].presence, "offline");
assert!(devices[0].recommended_route.is_none());
assert!(!devices[0].route_candidates[0].wakeable);
}
#[test]
fn inventory_result_maps_to_stored_observations() {
let observations = inventory_result_to_observations(serde_json::json!({
@@ -259,10 +259,7 @@ pub(in crate::state::store) fn normalize_agent_observation(
let mac = normalize_optional_text(input.mac.as_deref()).map(|value| value.to_ascii_lowercase());
let ip = normalize_optional_text(input.ip.as_deref());
let hostname = normalize_optional_text(input.hostname.as_deref());
let identifier = mac
.as_ref()
.map(|value| format!("mac:{value}"))
.or_else(|| ip.as_ref().map(|value| format!("ip:{value}")))
let identifier = observation_identifier(&kind, mac.as_deref(), ip.as_deref())
.ok_or_else(|| anyhow::anyhow!("observation requires mac or ip"))?;
let observation_key = format!("agent:{agent_id}:{kind}:{identifier}");
Ok(AgentDeviceObservation {
@@ -278,6 +275,15 @@ pub(in crate::state::store) fn normalize_agent_observation(
})
}
fn observation_identifier(kind: &str, mac: Option<&str>, ip: Option<&str>) -> Option<String> {
match (kind, mac, ip) {
("neigh", Some(mac), Some(ip)) => Some(format!("mac:{mac}:ip:{ip}")),
(_, Some(mac), _) => Some(format!("mac:{mac}")),
(_, None, Some(ip)) => Some(format!("ip:{ip}")),
(_, None, None) => None,
}
}
pub(in crate::state::store) async fn insert_device_identifier_tx(
tx: &mut Transaction<'_, Sqlite>,
device_id: &str,
@@ -211,6 +211,8 @@ impl Store {
r#"SELECT events.event_id as "event_id!",
('agent:' || events.agent_id || ':' || events.kind || ':' ||
CASE
WHEN events.kind = 'neigh' AND events.mac IS NOT NULL AND events.ip IS NOT NULL
THEN 'mac:' || events.mac || ':ip:' || events.ip
WHEN events.mac IS NOT NULL THEN 'mac:' || events.mac
WHEN events.ip IS NOT NULL THEN 'ip:' || events.ip
ELSE ''
@@ -239,6 +241,8 @@ impl Store {
AND (?4 IS NULL OR events.ip = ?4)
AND (?5 IS NULL OR ('agent:' || events.agent_id || ':' || events.kind || ':' ||
CASE
WHEN events.kind = 'neigh' AND events.mac IS NOT NULL AND events.ip IS NOT NULL
THEN 'mac:' || events.mac || ':ip:' || events.ip
WHEN events.mac IS NOT NULL THEN 'mac:' || events.mac
WHEN events.ip IS NOT NULL THEN 'ip:' || events.ip
ELSE ''