91 lines
3.6 KiB
Rust
91 lines
3.6 KiB
Rust
//! Additive JSON observation mapping inside the existing snapshot payload. This
|
|
//! does not introduce a protobuf tag, a fabricated receive time or a new scope.
|
|
use anyhow::Result;
|
|
use serde_json::{Value, json};
|
|
|
|
pub fn observations(inventory: &crate::inventory::LinuxInventory) -> Result<Vec<Value>> {
|
|
let value = serde_json::to_value(inventory)?;
|
|
let mut result = Vec::new();
|
|
for (name, observation) in value
|
|
.as_object()
|
|
.ok_or_else(|| anyhow::anyhow!("inventory object"))?
|
|
{
|
|
let mut observation = observation.clone();
|
|
observation["scope"] = json!(format!("linux.{name}"));
|
|
// Bound each scope independently. Never drop unsupported/error metadata
|
|
// or imply that absent/truncated rows were deleted from a device.
|
|
while serde_json::to_vec(&observation)?.len() > 32768 {
|
|
observation["truncated"] = json!(true);
|
|
observation["completeness"] = json!("partial");
|
|
observation["error_code"] = json!("WIRE_SCOPE_LIMIT");
|
|
if let Some(rows) = observation["data"].as_array_mut().filter(|v| !v.is_empty()) {
|
|
rows.truncate(rows.len() / 2);
|
|
} else {
|
|
observation["data"] = Value::Null;
|
|
observation["quality"] = json!("unknown");
|
|
break;
|
|
}
|
|
}
|
|
result.push(observation);
|
|
}
|
|
Ok(result)
|
|
}
|
|
pub fn snapshot(extended: bool) -> Result<Value> {
|
|
let started = chrono::Utc::now();
|
|
let mut base = serde_json::to_value(crate::collect()?)?;
|
|
if extended {
|
|
base["observations"] =
|
|
serde_json::to_value(observations(&crate::inventory::collect_native())?)?;
|
|
base["collection_started_at"] = json!(started);
|
|
base["collection_completed_at"] = json!(chrono::Utc::now());
|
|
}
|
|
Ok(base)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn native_wire_scopes_preserve_quality_time_and_unsupported() {
|
|
let native = crate::inventory::collect_native();
|
|
let mapped = observations(&native).unwrap();
|
|
assert_eq!(mapped.len(), 11);
|
|
let smart = mapped.iter().find(|v| v["scope"] == "linux.smart").unwrap();
|
|
assert_eq!(smart["quality"], "unsupported");
|
|
assert!(smart["data"].is_null());
|
|
assert_eq!(smart["completeness"], "unknown");
|
|
for observation in &mapped {
|
|
assert!(
|
|
chrono::DateTime::parse_from_rfc3339(observation["observed_at"].as_str().unwrap())
|
|
.is_ok()
|
|
);
|
|
assert!(!observation["source_id"].as_str().unwrap().is_empty());
|
|
assert!(observation.get("received_at").is_none());
|
|
assert!(serde_json::to_vec(observation).unwrap().len() <= 32768);
|
|
}
|
|
let base = snapshot(false).unwrap();
|
|
assert!(serde_json::from_value::<rmm_domain::Inventory>(base).is_ok());
|
|
}
|
|
#[test]
|
|
fn scope_limit_retains_error_metadata_and_marks_partial() {
|
|
let mut native = crate::inventory::collect_native();
|
|
native.software.data = Some(
|
|
(0..2000)
|
|
.map(|_| crate::inventory::SoftwarePackage {
|
|
name: "x".repeat(128),
|
|
version: "1.0".into(),
|
|
architecture: "amd64".into(),
|
|
status: "installed".into(),
|
|
})
|
|
.collect(),
|
|
);
|
|
let mapped = observations(&native).unwrap();
|
|
let software = mapped
|
|
.iter()
|
|
.find(|v| v["scope"] == "linux.software")
|
|
.unwrap();
|
|
assert_eq!(software["truncated"], true);
|
|
assert_eq!(software["completeness"], "partial");
|
|
}
|
|
}
|