feat(metering): new content starts a change-driven scan (t72) #930
No files matched your search
@@ -999,6 +999,30 @@ pub async fn detect_target(
|
||||
}))
|
||||
}
|
||||
|
||||
/// Why a scan is asked for (`POST /api/v1/targets/{id}/scan?reason=…`).
|
||||
#[derive(Debug, Default, serde::Deserialize)]
|
||||
pub struct ScanReason {
|
||||
/// `new_content` after the customer created the target or uploaded
|
||||
/// files (t72): it runs only when something changed, as a change-driven
|
||||
/// Prüfung. Absent: the customer asks for a Prüfung, which always runs
|
||||
/// and counts.
|
||||
#[serde(default)]
|
||||
pub reason: Option<String>,
|
||||
}
|
||||
|
||||
/// The trigger of a scan the customer (`staff: false`) or support asked
|
||||
/// for, for `reason`. Support's scans are never the customer's usage.
|
||||
fn requested_trigger(staff: bool, reason: Option<&str>) -> compliance_core::models::ScanTrigger {
|
||||
use compliance_core::models::ScanTrigger;
|
||||
if staff {
|
||||
ScanTrigger::Support
|
||||
} else if reason == Some("new_content") {
|
||||
ScanTrigger::NewContent
|
||||
} else {
|
||||
ScanTrigger::Manual
|
||||
}
|
||||
}
|
||||
|
||||
/// POST /api/v1/targets/{id}/scan — trigger a scan for the target.
|
||||
///
|
||||
/// Dispatches to the unified pipeline when `UNIFIED_PIPELINE` is set (else the
|
||||
@@ -1008,6 +1032,7 @@ pub async fn trigger_target_scan(
|
||||
Extension(agent): AgentExt,
|
||||
tenant: TenantCtx,
|
||||
Path(id): Path<String>,
|
||||
Query(reason): Query<ScanReason>,
|
||||
) -> Result<Json<serde_json::Value>, StatusCode> {
|
||||
let oid = parse_oid(&id)?;
|
||||
let db = tenant_db(&agent, &tenant).await?;
|
||||
@@ -1024,12 +1049,7 @@ pub async fn trigger_target_scan(
|
||||
|
||||
let agent_clone = (*agent).clone();
|
||||
let tenant_id = tenant.0.tenant_id.clone();
|
||||
// A scan support starts is not the customer's usage.
|
||||
let trigger = if tenant.0.staff.is_some() {
|
||||
compliance_core::models::ScanTrigger::Support
|
||||
} else {
|
||||
compliance_core::models::ScanTrigger::Manual
|
||||
};
|
||||
let trigger = requested_trigger(tenant.0.staff.is_some(), reason.reason.as_deref());
|
||||
tokio::spawn(async move {
|
||||
// Always the unified target pipeline — this endpoint is about an
|
||||
// onboarded target by construction, independent of the global
|
||||
@@ -1045,6 +1065,22 @@ pub async fn trigger_target_scan(
|
||||
mod filter_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_scan_asked_for_new_content_is_change_driven() {
|
||||
use compliance_core::models::ScanTrigger;
|
||||
assert_eq!(requested_trigger(false, None), ScanTrigger::Manual);
|
||||
assert_eq!(
|
||||
requested_trigger(false, Some("new_content")),
|
||||
ScanTrigger::NewContent
|
||||
);
|
||||
assert_eq!(requested_trigger(false, Some("other")), ScanTrigger::Manual);
|
||||
assert_eq!(
|
||||
requested_trigger(true, Some("new_content")),
|
||||
ScanTrigger::Support,
|
||||
"support's scans are never the customer's usage"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_project_filter_matches_its_targets() {
|
||||
let filter = TargetFilter {
|
||||
|
||||
@@ -38,7 +38,10 @@ pub const SCHEDULED_SCAN_JITTER: Duration = Duration::from_secs(30);
|
||||
pub fn start_delay(trigger: &ScanTrigger) -> Duration {
|
||||
match trigger {
|
||||
ScanTrigger::Scheduled => rand::rng().random_range(Duration::ZERO..=SCHEDULED_SCAN_JITTER),
|
||||
ScanTrigger::Webhook | ScanTrigger::Manual | ScanTrigger::Support => Duration::ZERO,
|
||||
ScanTrigger::Webhook
|
||||
| ScanTrigger::Manual
|
||||
| ScanTrigger::Support
|
||||
| ScanTrigger::NewContent => Duration::ZERO,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,11 +190,12 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn webhook_manual_and_support_scans_start_at_once() {
|
||||
fn webhook_manual_support_and_new_content_scans_start_at_once() {
|
||||
for trigger in [
|
||||
ScanTrigger::Webhook,
|
||||
ScanTrigger::Manual,
|
||||
ScanTrigger::Support,
|
||||
ScanTrigger::NewContent,
|
||||
] {
|
||||
assert_eq!(start_delay(&trigger), Duration::ZERO);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ impl ScanAttribution {
|
||||
match trigger {
|
||||
ScanTrigger::Manual => Some(Self::Request),
|
||||
ScanTrigger::Support => Some(Self::Support),
|
||||
ScanTrigger::Scheduled | ScanTrigger::Webhook => None,
|
||||
ScanTrigger::Scheduled | ScanTrigger::Webhook | ScanTrigger::NewContent => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,6 +325,11 @@ mod tests {
|
||||
ScanAttribution::detected(&ScanTrigger::Webhook, false),
|
||||
ScanAttribution::Rescan
|
||||
);
|
||||
assert_eq!(
|
||||
ScanAttribution::detected(&ScanTrigger::NewContent, true),
|
||||
ScanAttribution::Change,
|
||||
"an upload is the customer's change"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -10,6 +10,10 @@ pub enum ScanTrigger {
|
||||
/// Started by BreakPilot support in the customer's tenant; not counted
|
||||
/// as the customer's usage.
|
||||
Support,
|
||||
/// Started after the customer gave the target new content: created it,
|
||||
/// or uploaded files (t72). Like a scheduled scan, it runs only when
|
||||
/// something changed, and then it is a change-driven Prüfung.
|
||||
NewContent,
|
||||
}
|
||||
|
||||
impl ScanTrigger {
|
||||
@@ -18,7 +22,7 @@ impl ScanTrigger {
|
||||
pub fn started_by_person(&self) -> bool {
|
||||
match self {
|
||||
Self::Manual | Self::Support => true,
|
||||
Self::Scheduled | Self::Webhook => false,
|
||||
Self::Scheduled | Self::Webhook | Self::NewContent => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,8 @@ use dioxus::prelude::*;
|
||||
use crate::i18n::texts::{common, kinds, onboarding as otx, targets as tx};
|
||||
use crate::i18n::use_lang;
|
||||
use crate::infrastructure::onboarding::{
|
||||
is_file_kind, plc_format_for, set_archive_password, upload_target_artifact, ArchivePassword,
|
||||
PasswordOutcome, UNSUPPORTED_ARCHIVE,
|
||||
is_file_kind, plc_format_for, set_archive_password, trigger_new_content_scan,
|
||||
upload_target_artifact, ArchivePassword, PasswordOutcome, UNSUPPORTED_ARCHIVE,
|
||||
};
|
||||
|
||||
/// The upload row for target `target_id`; `on_uploaded` runs once the file
|
||||
@@ -82,6 +82,7 @@ pub fn ArtifactUpload(target_id: String, on_uploaded: EventHandler<()>) -> Eleme
|
||||
onclick: move |_| {
|
||||
let Some(picked) = file() else { return; };
|
||||
let (id, kind, pw) = (target_id.clone(), kind(), password());
|
||||
let scan_id = id.clone();
|
||||
let format = (kind == "plc_project").then(|| plc_format.read().clone());
|
||||
busy.set(true);
|
||||
spawn(async move {
|
||||
@@ -115,10 +116,21 @@ pub fn ArtifactUpload(target_id: String, on_uploaded: EventHandler<()>) -> Eleme
|
||||
}
|
||||
}
|
||||
};
|
||||
// New content starts its change-driven scan (t72); a
|
||||
// failed start leaves the file for the next scan.
|
||||
let scanning = match &result {
|
||||
Ok(()) => trigger_new_content_scan(scan_id).await.is_ok(),
|
||||
Err(_) => false,
|
||||
};
|
||||
busy.set(false);
|
||||
match result {
|
||||
Ok(()) => {
|
||||
note.set(Some((true, tx::UPLOADED.get(lang).to_string())));
|
||||
let uploaded = if scanning {
|
||||
tx::UPLOADED
|
||||
} else {
|
||||
tx::UPLOADED_NOT_SCANNED
|
||||
};
|
||||
note.set(Some((true, uploaded.get(lang).to_string())));
|
||||
file.set(None);
|
||||
password.set(String::new());
|
||||
on_uploaded.call(());
|
||||
|
||||
@@ -1118,9 +1118,9 @@ pub mod targets {
|
||||
};
|
||||
pub const UPLOAD_HINT: Text = Text {
|
||||
de: "Ein Archiv (zip, tar, tar.gz), ein SPS-Projekt oder ein Firmware-Image. Die Datei \
|
||||
wird sofort hochgeladen und bei der nächsten Prüfung geprüft.",
|
||||
wird sofort hochgeladen und gleich danach geprüft.",
|
||||
en: "An archive (zip, tar, tar.gz), a PLC project or a firmware image. It is uploaded \
|
||||
at once and checked by the next scan.",
|
||||
at once and scanned right after.",
|
||||
};
|
||||
pub const UPLOAD: Text = Text {
|
||||
de: "Hochladen",
|
||||
@@ -1173,8 +1173,14 @@ pub mod targets {
|
||||
}
|
||||
}
|
||||
pub const UPLOADED: Text = Text {
|
||||
de: "Hochgeladen. Starten Sie die Prüfung, um die Datei zu prüfen.",
|
||||
en: "Uploaded. Start a scan to check the file.",
|
||||
de: "Hochgeladen. Die Prüfung der neuen Datei startet.",
|
||||
en: "Uploaded. The scan of the new file is starting.",
|
||||
};
|
||||
pub const UPLOADED_NOT_SCANNED: Text = Text {
|
||||
de: "Hochgeladen. Die Prüfung konnte nicht gestartet werden; die nächste \
|
||||
geplante Prüfung liest die Datei.",
|
||||
en: "Uploaded. The scan could not be started; the next scheduled scan reads \
|
||||
the file.",
|
||||
};
|
||||
pub const EVIDENCE_BUILDING: Text = Text {
|
||||
de: "Wird erstellt …",
|
||||
|
||||
@@ -606,7 +606,24 @@ pub async fn delete_target(id: String) -> Result<serde_json::Value, ServerFnErro
|
||||
super::agent_client::json_or_error(resp).await
|
||||
}
|
||||
|
||||
/// Trigger a scan for a target.
|
||||
/// Start the scan of a target that just got new content: it was created,
|
||||
/// or files were uploaded to it (t72). It runs only when something changed,
|
||||
/// as a change-driven Prüfung that does not count on an included target.
|
||||
#[server]
|
||||
pub async fn trigger_new_content_scan(id: String) -> Result<serde_json::Value, ServerFnError> {
|
||||
let resp = super::agent_client::agent_request(
|
||||
reqwest::Method::POST,
|
||||
&format!("/api/v1/targets/{id}/scan?reason=new_content"),
|
||||
)
|
||||
.await?
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| ServerFnError::new(e.to_string()))?;
|
||||
super::agent_client::json_or_error(resp).await
|
||||
}
|
||||
|
||||
/// Trigger a scan for a target: the customer asks for a Prüfung, which
|
||||
/// always runs and counts (t72).
|
||||
#[server]
|
||||
pub async fn trigger_target_scan(id: String) -> Result<serde_json::Value, ServerFnError> {
|
||||
let resp = super::agent_client::agent_request(
|
||||
|
||||
@@ -17,7 +17,7 @@ use crate::i18n::texts::{common, kinds, onboarding as tx};
|
||||
use crate::i18n::{format_bytes, use_lang};
|
||||
use crate::infrastructure::onboarding::{
|
||||
create_target, detect_target, enable_target_scans, fetch_applicable_scans, is_file_kind,
|
||||
plc_format_for, set_archive_password, trigger_target_scan, upload_target_artifact,
|
||||
plc_format_for, set_archive_password, trigger_new_content_scan, upload_target_artifact,
|
||||
validate_artifact_ref, validate_target_name, ArchivePassword, ArtifactInputDto,
|
||||
PasswordOutcome, UNSUPPORTED_ARCHIVE,
|
||||
};
|
||||
@@ -591,7 +591,7 @@ pub fn OnboardingPage() -> Element {
|
||||
return;
|
||||
}
|
||||
}
|
||||
first_scan.set(match trigger_target_scan(id).await {
|
||||
first_scan.set(match trigger_new_content_scan(id).await {
|
||||
Ok(_) => FirstScan::Started,
|
||||
Err(e) => FirstScan::Failed(e.to_string()),
|
||||
});
|
||||
@@ -750,7 +750,7 @@ pub fn OnboardingPage() -> Element {
|
||||
let offers_optin = !source_only && !optin_scans(&applicable).is_empty();
|
||||
scans.set(applicable);
|
||||
if upload_errors.read().is_empty() && !offers_optin {
|
||||
first_scan.set(match trigger_target_scan(id).await {
|
||||
first_scan.set(match trigger_new_content_scan(id).await {
|
||||
Ok(_) => FirstScan::Started,
|
||||
Err(e) => FirstScan::Failed(e.to_string()),
|
||||
});
|
||||
|
||||
@@ -76,11 +76,18 @@ New findings appear on the target's page while the scan is still running.
|
||||
Click **Prüfung starten** (Run scan), either in the row of the target in the
|
||||
**Prüfobjekte** list or at the top of the target's page. The scan starts in the
|
||||
background and the message "Prüfung gestartet. Den Fortschritt sehen Sie unter
|
||||
Prüfläufe." appears. A new target is scanned once as soon as you create it in
|
||||
the **Neues Prüfobjekt** wizard.
|
||||
Prüfläufe." appears.
|
||||
|
||||
A manual scan always scans in full, even if nothing changed.
|
||||
|
||||
### Scan after new content
|
||||
|
||||
Creating a target in the wizard and uploading a file to a target start a scan
|
||||
of the new content right away. Like a scheduled scan, it runs only when
|
||||
something changed. Through the API, ask for it with
|
||||
`POST /api/v1/targets/{id}/scan?reason=new_content`; without `reason`, the
|
||||
scan is a Prüfung you request, which always runs in full.
|
||||
|
||||
### Scheduled Scans
|
||||
|
||||
Targets are scanned automatically on a recurring schedule: by default every 6
|
||||
|
||||
Reference in new issue
Block a user