feat(agent): push usage events to the registry (scan.completed, target.added) #247
No files matched your search
@@ -66,10 +66,36 @@ impl ComplianceAgent {
|
||||
repo_id: &str,
|
||||
trigger: compliance_core::models::ScanTrigger,
|
||||
) -> Result<(), crate::error::AgentError> {
|
||||
let db = self.db_pool.for_tenant_id(tenant_id).await?;
|
||||
let orchestrator =
|
||||
PipelineOrchestrator::new(self.config.clone(), db, self.llm.clone(), self.http.clone());
|
||||
orchestrator.run_target(repo_id, trigger).await
|
||||
self.run_target_scan(tenant_id, repo_id, trigger).await
|
||||
}
|
||||
|
||||
/// Push one usage event to the tenant-registry (PRODUCT_INTEGRATION_SPEC
|
||||
/// 2.0 §9.2) in the background. `key` makes retries idempotent; the
|
||||
/// registry forwards the event to ERPNext. No-op without a registry.
|
||||
pub fn emit_usage(
|
||||
&self,
|
||||
tenant_id: &str,
|
||||
metric: &str,
|
||||
key: String,
|
||||
properties: serde_json::Value,
|
||||
) {
|
||||
let Some(registry) = self.registry.clone() else {
|
||||
return;
|
||||
};
|
||||
let event = crate::registry::UsageEvent {
|
||||
idempotency_key: key,
|
||||
tenant_id: tenant_id.to_string(),
|
||||
product: registry.product().to_string(),
|
||||
metric: metric.to_string(),
|
||||
quantity: 1.0,
|
||||
occurred_at: chrono::Utc::now().to_rfc3339(),
|
||||
properties: Some(properties),
|
||||
};
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = registry.push_usage_events(&[event]).await {
|
||||
tracing::warn!("usage event push failed: {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Alias for [`Self::run_scan`] — every scan runs the unified onboarded-target
|
||||
@@ -84,7 +110,15 @@ impl ComplianceAgent {
|
||||
let db = self.db_pool.for_tenant_id(tenant_id).await?;
|
||||
let orchestrator =
|
||||
PipelineOrchestrator::new(self.config.clone(), db, self.llm.clone(), self.http.clone());
|
||||
orchestrator.run_target(target_id, trigger).await
|
||||
let started = chrono::Utc::now();
|
||||
orchestrator.run_target(target_id, trigger.clone()).await?;
|
||||
self.emit_usage(
|
||||
tenant_id,
|
||||
"scan.completed",
|
||||
format!("scan:{tenant_id}:{target_id}:{}", started.timestamp()),
|
||||
serde_json::json!({ "target_id": target_id, "trigger": format!("{trigger:?}") }),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run a PR review: scan the diff and post review comments.
|
||||
|
||||
@@ -95,10 +95,22 @@ pub async fn add_target(
|
||||
target.allow_destructive = req.allow_destructive;
|
||||
|
||||
let db = tenant_db(&agent, &tenant).await?;
|
||||
db.dast_targets()
|
||||
let res = db
|
||||
.dast_targets()
|
||||
.insert_one(&target)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let target_id = res
|
||||
.inserted_id
|
||||
.as_object_id()
|
||||
.map(|id| id.to_hex())
|
||||
.unwrap_or_default();
|
||||
agent.emit_usage(
|
||||
&tenant.0.tenant_id,
|
||||
"target.added",
|
||||
format!("dast-target:{}:{target_id}", tenant.0.tenant_id),
|
||||
serde_json::json!({ "target_id": target_id, "kind": "dast" }),
|
||||
);
|
||||
|
||||
Ok(Json(ApiResponse {
|
||||
data: target,
|
||||
@@ -124,6 +136,8 @@ pub async fn trigger_scan(
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::NOT_FOUND)?;
|
||||
|
||||
let tenant_id = tenant.0.tenant_id.clone();
|
||||
let usage_agent = agent.clone();
|
||||
tokio::spawn(async move {
|
||||
let orchestrator = compliance_dast::DastOrchestrator::new(100);
|
||||
match orchestrator.run_scan(&target, Vec::new()).await {
|
||||
@@ -131,6 +145,12 @@ pub async fn trigger_scan(
|
||||
if let Err(e) = db.dast_scan_runs().insert_one(&scan_run).await {
|
||||
tracing::error!("Failed to store DAST scan run: {e}");
|
||||
}
|
||||
usage_agent.emit_usage(
|
||||
&tenant_id,
|
||||
"scan.completed",
|
||||
format!("dast:{tenant_id}:{id}:{}", chrono::Utc::now().timestamp()),
|
||||
serde_json::json!({ "target_id": id, "kind": "dast", "findings": findings.len() }),
|
||||
);
|
||||
for finding in &findings {
|
||||
if let Err(e) = db.dast_findings().insert_one(finding).await {
|
||||
tracing::error!("Failed to store DAST finding: {e}");
|
||||
|
||||
@@ -152,6 +152,13 @@ pub async fn create_target(
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
target.id = res.inserted_id.as_object_id();
|
||||
let target_id = target.id.map(|id| id.to_hex()).unwrap_or_default();
|
||||
agent.emit_usage(
|
||||
&tenant.0.tenant_id,
|
||||
"target.added",
|
||||
format!("target:{}:{target_id}", tenant.0.tenant_id),
|
||||
serde_json::json!({ "target_id": target_id, "target_type": format!("{:?}", target.target_type) }),
|
||||
);
|
||||
Ok(Json(ApiResponse {
|
||||
data: target,
|
||||
total: None,
|
||||
|
||||
Reference in new issue
Block a user