coven_sync/
detection_record.rs1use crate::SyncError;
8use coven_database::{
9 operation_kind::S3_DETECTION, NewOperation, OperationId, OperationRecord, StoreDatabase,
10};
11use coven_storage::StorageConfig;
12use serde::{Deserialize, Serialize};
13
14#[derive(Serialize, Deserialize)]
16struct DetectionWork {
17 run: uuid::Uuid,
19 location: StorageConfig,
21}
22
23pub struct DetectionRecord {
25 id: OperationId,
26 work: DetectionWork,
27}
28
29impl DetectionRecord {
30 pub async fn start(
32 database: &StoreDatabase,
33 location: &StorageConfig,
34 run: uuid::Uuid,
35 ) -> Result<Self, SyncError> {
36 let work = DetectionWork { run, location: location.clone() };
37 let id = database
38 .start_operation(NewOperation {
39 kind: S3_DETECTION.into(),
40 data: serde_json::to_vec(&work)?,
41 started_by: S3_DETECTION.into(),
42 pauses_writes: false,
43 })
44 .await?;
45 Ok(Self { id, work })
46 }
47
48 pub async fn at(
51 database: &StoreDatabase,
52 location: &StorageConfig,
53 ) -> Result<Vec<Self>, SyncError> {
54 let mut records = Vec::new();
55 for record in database.operations().await? {
56 if record.kind != S3_DETECTION {
57 continue;
58 }
59 let work: DetectionWork = serde_json::from_slice(&record.data)?;
60 if work.location.same_s3_bucket(location) {
61 records.push(Self { id: record.id, work });
62 }
63 }
64 Ok(records)
65 }
66
67 pub fn run(&self) -> uuid::Uuid {
69 self.work.run
70 }
71
72 pub async fn end(&self, database: &StoreDatabase) -> Result<(), SyncError> {
75 Ok(database.finish_operation(self.id).await?)
76 }
77
78 pub fn carried(records: &[OperationRecord]) -> Vec<NewOperation> {
81 records
82 .iter()
83 .filter(|record| record.kind == S3_DETECTION)
84 .map(|record| NewOperation {
85 kind: S3_DETECTION.into(),
86 data: record.data.clone(),
87 started_by: S3_DETECTION.into(),
88 pauses_writes: false,
89 })
90 .collect()
91 }
92}
93
94#[cfg(test)]
95#[path = "detection_record_tests.rs"]
96mod tests;