Skip to main content

printobserver_core/records/
samples.rs

1//! The canonical values of every record this domain declares.
2//!
3//! One value carrying every field, one carrying every optional field absent,
4//! and one per further arm of a union: what the schema set and the round-trip
5//! corpus are walked over. They are here rather than beside each record so
6//! that the records read as the data they are, and so that the fixed
7//! identifiers and instants the samples share are written once.
8
9use std::collections::BTreeMap;
10
11use printobserver_printer_api::{Adjustable, PrinterState};
12use printobserver_types::contract::Sample;
13use printobserver_types::{EventId, FileName, ImageId, PrintId, Range, Timestamp};
14
15use super::action::{
16    AcknowledgementDisposition, ActionKind, ActionRecord, ActionRequest, Actor, ActorClass,
17    ExecutionOutcome, PrintAction,
18};
19use super::charge::{
20    ChargeKey, ChargeMethod, FilePosition, PendingCharge, PendingTaken, PrintOutcome,
21};
22use super::ids::{ActionId, InterventionId, SpoolId};
23use super::image::ImageRecord;
24use super::intervention::{Intervention, InterventionOutcome};
25use super::manifest::JobManifest;
26use super::policy::{EffectiveBounds, PolicyDecision, RejectionReason, SafetyEnvelope};
27use super::preflight::{Preflight, PreflightTool, PreflightVerdict};
28use super::print::{ManifestNarrowing, PrintRecord};
29use super::spool::{
30    ClearedMark, LoadedSpool, SpoolRecord, SpoolTrust, UnverifiedMark, UnverifiedReason,
31};
32use super::spool_command::SpoolCommand;
33
34/// A later fixed instant than the one the type crate's sample carries.
35fn later_instant() -> Timestamp {
36    "2026-03-01T12:30:00Z"
37        .parse()
38        .expect("a fixed RFC 3339 instant")
39}
40
41/// A fixed action identifier.
42fn action_id() -> ActionId {
43    "0191f0a0-0000-7000-8000-000000000004"
44        .parse()
45        .expect("a fixed version 7 identifier")
46}
47
48/// A fixed intervention identifier.
49fn intervention_id() -> InterventionId {
50    "0191f0a0-0000-7000-8000-000000000005"
51        .parse()
52        .expect("a fixed version 7 identifier")
53}
54
55/// A fixed spool identifier.
56fn spool_id() -> SpoolId {
57    "0191f0a0-0000-7000-8000-000000000006"
58        .parse()
59        .expect("a fixed version 7 identifier")
60}
61
62/// A second fixed spool identifier, for the spool a swap replaced.
63fn replaced_spool_id() -> SpoolId {
64    "0191f0a0-0000-7000-8000-000000000007"
65        .parse()
66        .expect("a fixed version 7 identifier")
67}
68
69impl Sample for SpoolId {
70    fn sample_full() -> Self {
71        spool_id()
72    }
73}
74
75impl Sample for ActionId {
76    fn sample_full() -> Self {
77        action_id()
78    }
79}
80
81impl Sample for InterventionId {
82    fn sample_full() -> Self {
83        intervention_id()
84    }
85}
86
87impl Sample for ManifestNarrowing {
88    fn sample_full() -> Self {
89        Self {
90            adjustable: Adjustable::Feedrate,
91            requested: Range::new(0.2, 4.0),
92            applied: Range::new(0.5, 1.5),
93        }
94    }
95}
96
97impl Sample for PrintRecord {
98    fn sample_full() -> Self {
99        Self {
100            id: PrintId::sample_full(),
101            provider_print_id: Some(4211),
102            file_name: Some("benchy.gcode".to_owned()),
103            state: PrinterState::Printing,
104            opened_at: Timestamp::sample_full(),
105            job_started_at: Some(Timestamp::sample_full()),
106            job_print_time_s: Some(3024),
107            ended_at: Some(later_instant()),
108            end_reason: Some("finished".to_owned()),
109            narrowings: vec![ManifestNarrowing::sample_full()],
110        }
111    }
112
113    fn sample_minimal() -> Self {
114        Self {
115            id: PrintId::sample_full(),
116            provider_print_id: None,
117            file_name: None,
118            state: PrinterState::Printing,
119            opened_at: Timestamp::sample_full(),
120            job_started_at: None,
121            job_print_time_s: None,
122            ended_at: None,
123            end_reason: None,
124            narrowings: vec![],
125        }
126    }
127}
128
129impl Sample for ImageRecord {
130    fn sample_full() -> Self {
131        Self {
132            id: ImageId::sample_full(),
133            print_id: PrintId::sample_full(),
134            event_id: EventId::sample_full(),
135            source_url: Some("https://detector.example/snapshots/1.jpg".to_owned()),
136            fetched_at: Timestamp::sample_full(),
137            content_type: "image/jpeg".to_owned(),
138            byte_len: 51_200,
139            sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".to_owned(),
140            relative_path: "images/0191f0a0/1.jpg".to_owned(),
141        }
142    }
143
144    fn sample_minimal() -> Self {
145        Self {
146            source_url: None,
147            ..Self::sample_full()
148        }
149    }
150}
151
152impl Sample for Actor {
153    fn sample_full() -> Self {
154        Self::Agent {
155            session_name: "print-0191f0a0".to_owned(),
156        }
157    }
158}
159
160impl Sample for ActorClass {
161    fn sample_full() -> Self {
162        Self::Agent
163    }
164}
165
166impl Sample for ActionKind {
167    fn sample_full() -> Self {
168        Self::SetFeedrateFactor
169    }
170}
171
172impl Sample for AcknowledgementDisposition {
173    fn sample_full() -> Self {
174        Self::Watch
175    }
176}
177
178impl Sample for ExecutionOutcome {
179    fn sample_full() -> Self {
180        Self::Failed {
181            reason: "the printer refused the command".to_owned(),
182        }
183    }
184}
185
186impl Sample for PrintAction {
187    fn sample_full() -> Self {
188        Self::SetFeedrateFactor {
189            factor: 0.8,
190            duration_s: Some(600),
191            reason: "the first layer is going down fast".to_owned(),
192            actor: Actor::sample_full(),
193        }
194    }
195
196    fn sample_minimal() -> Self {
197        Self::SetFeedrateFactor {
198            factor: 0.8,
199            duration_s: None,
200            reason: "the first layer is going down fast".to_owned(),
201            actor: Actor::sample_full(),
202        }
203    }
204
205    fn sample_alternates() -> Vec<Self> {
206        let reason = || "the print needs it".to_owned();
207        vec![
208            Self::Pause {
209                reason: reason(),
210                actor: Actor::sample_full(),
211            },
212            Self::Resume {
213                reason: reason(),
214                actor: Actor::sample_full(),
215            },
216            Self::Cancel {
217                reason: reason(),
218                actor: Actor::sample_full(),
219            },
220            Self::StartPrint {
221                file_name: FileName::sample_full(),
222                manifest: JobManifest::sample_full(),
223                override_filament_check: true,
224                reason: reason(),
225                actor: Actor::sample_full(),
226            },
227            Self::SetFlowrateFactor {
228                factor: 1.05,
229                duration_s: Some(300),
230                reason: reason(),
231                actor: Actor::sample_full(),
232            },
233            Self::SetToolTargetC {
234                tool: 0,
235                target_c: 215.0,
236                duration_s: Some(300),
237                reason: reason(),
238                actor: Actor::sample_full(),
239            },
240            Self::SetBedTargetC {
241                target_c: 60.0,
242                duration_s: Some(300),
243                reason: reason(),
244                actor: Actor::sample_full(),
245            },
246            Self::SetFanPercent {
247                percent: 80.0,
248                duration_s: Some(300),
249                reason: reason(),
250                actor: Actor::sample_full(),
251            },
252            Self::AcknowledgeFailure {
253                event_id: EventId::sample_full(),
254                disposition: AcknowledgementDisposition::Watch,
255                reason: reason(),
256                actor: Actor::Operator,
257            },
258        ]
259    }
260}
261
262impl Sample for ActionRequest {
263    fn sample_full() -> Self {
264        Self {
265            action: PrintAction::sample_full(),
266            actor: Actor::sample_full(),
267            requested_at: Timestamp::sample_full(),
268        }
269    }
270
271    fn sample_minimal() -> Self {
272        Self {
273            action: PrintAction::sample_minimal(),
274            ..Self::sample_full()
275        }
276    }
277}
278
279impl Sample for ActionRecord {
280    fn sample_full() -> Self {
281        Self {
282            id: action_id(),
283            print_id: PrintId::sample_full(),
284            request: ActionRequest::sample_full(),
285            decision: PolicyDecision::Accepted,
286            executed_at: Some(later_instant()),
287            outcome: Some(ExecutionOutcome::Succeeded),
288        }
289    }
290
291    fn sample_minimal() -> Self {
292        Self {
293            request: ActionRequest::sample_minimal(),
294            executed_at: None,
295            outcome: None,
296            ..Self::sample_full()
297        }
298    }
299}
300
301/// The allowed map both the manifest and the envelope carry.
302fn allowed_ranges() -> BTreeMap<Adjustable, Range> {
303    BTreeMap::from([
304        (Adjustable::Feedrate, Range::new(0.5, 1.5)),
305        (Adjustable::ToolTarget { tool: 0 }, Range::new(190.0, 230.0)),
306    ])
307}
308
309impl Sample for JobManifest {
310    fn sample_full() -> Self {
311        Self {
312            file_name: "benchy.gcode".to_owned(),
313            material: "PLA".to_owned(),
314            nozzle_diameter_mm: 0.4,
315            slicer_profile: "0.2mm standard".to_owned(),
316            allowed: allowed_ranges(),
317            metadata: BTreeMap::from([("layer_height_mm".to_owned(), "0.2".to_owned())]),
318        }
319    }
320}
321
322impl Sample for SafetyEnvelope {
323    fn sample_full() -> Self {
324        Self {
325            allowed: allowed_ranges(),
326            actions: BTreeMap::from([(
327                ActorClass::Agent,
328                vec![ActionKind::Pause, ActionKind::SetFeedrateFactor],
329            )]),
330            agent_min_interval_s: 60,
331        }
332    }
333}
334
335impl Sample for EffectiveBounds {
336    fn sample_full() -> Self {
337        Self {
338            allowed: allowed_ranges(),
339        }
340    }
341}
342
343impl Sample for RejectionReason {
344    fn sample_full() -> Self {
345        Self::OutOfBounds {
346            adjustable: Adjustable::Feedrate,
347            requested: 4.0,
348            allowed: Range::new(0.5, 1.5),
349        }
350    }
351
352    fn sample_alternates() -> Vec<Self> {
353        vec![
354            Self::InsufficientFilament {
355                tool: 0,
356                spool_id: spool_id(),
357                spool_name: "Galaxy black".to_owned(),
358                required_g: 120.0,
359                buffer_g: 12.0,
360                remaining_g: 80.0,
361                unverified: Some(UnverifiedReason::sample_full()),
362            },
363            Self::InsufficientFilament {
364                tool: 0,
365                spool_id: spool_id(),
366                spool_name: "Galaxy black".to_owned(),
367                required_g: 120.0,
368                buffer_g: 12.0,
369                remaining_g: 80.0,
370                unverified: None,
371            },
372            Self::UnverifiedSpool {
373                tool: 0,
374                spool_id: spool_id(),
375                spool_name: "Galaxy black".to_owned(),
376                reason: UnverifiedReason::sample_full(),
377            },
378            Self::AnalysisUnavailable {
379                file_name: "benchy.gcode".to_owned(),
380                waited_s: 300,
381            },
382            Self::SpoolRefused {
383                detail: "spool 0191f0a0 is retired, so it takes no command".to_owned(),
384            },
385        ]
386    }
387}
388
389impl Sample for PolicyDecision {
390    fn sample_full() -> Self {
391        Self::Rejected(RejectionReason::sample_full())
392    }
393}
394
395impl Sample for InterventionOutcome {
396    fn sample_full() -> Self {
397        Self::Superseded {
398            by: intervention_id(),
399        }
400    }
401}
402
403impl Sample for Intervention {
404    fn sample_full() -> Self {
405        Self {
406            id: intervention_id(),
407            print_id: PrintId::sample_full(),
408            action_id: action_id(),
409            adjustable: Adjustable::Feedrate,
410            prior_value: Some(1.0),
411            applied_value: 0.8,
412            applied_at: Timestamp::sample_full(),
413            expires_at: later_instant(),
414            restored_at: Some(later_instant()),
415            outcome: InterventionOutcome::Restored,
416        }
417    }
418
419    fn sample_minimal() -> Self {
420        Self {
421            prior_value: None,
422            restored_at: None,
423            outcome: InterventionOutcome::StillActive,
424            ..Self::sample_full()
425        }
426    }
427}
428
429impl Sample for UnverifiedReason {
430    fn sample_full() -> Self {
431        Self::UnchargedPrint {
432            print_id: PrintId::sample_full(),
433        }
434    }
435
436    fn sample_alternates() -> Vec<Self> {
437        vec![
438            Self::UnaccountedPrint {
439                file_name: "benchy.gcode".to_owned(),
440                printed_at: Timestamp::sample_full(),
441            },
442            Self::FilamentChangeUnconfirmed {
443                print_id: PrintId::sample_full(),
444            },
445            Self::FilamentChangeUnconfirmedPosition {
446                print_id: PrintId::sample_full(),
447            },
448            Self::TallyReset {
449                file_name: "benchy.gcode".to_owned(),
450                origin: "local".to_owned(),
451            },
452        ]
453    }
454}
455
456impl Sample for UnverifiedMark {
457    fn sample_full() -> Self {
458        Self {
459            reason: UnverifiedReason::sample_full(),
460            since: Timestamp::sample_full(),
461        }
462    }
463}
464
465impl Sample for SpoolTrust {
466    fn sample_full() -> Self {
467        Self::Unverified {
468            marks: vec![
469                UnverifiedMark::sample_full(),
470                UnverifiedMark {
471                    reason: UnverifiedReason::FilamentChangeUnconfirmed {
472                        print_id: PrintId::sample_full(),
473                    },
474                    since: later_instant(),
475                },
476            ],
477        }
478    }
479
480    fn sample_alternates() -> Vec<Self> {
481        vec![Self::Verified]
482    }
483}
484
485impl Sample for SpoolRecord {
486    fn sample_full() -> Self {
487        Self {
488            id: spool_id(),
489            name: "Galaxy black".to_owned(),
490            material: "PLA".to_owned(),
491            color: Some("black".to_owned()),
492            diameter_mm: 1.75,
493            density_g_cm3: 1.24,
494            initial_g: 1_000.0,
495            remaining_g: 742.5,
496            tare_g: Some(250.0),
497            loaded_on_tool: Some(0),
498            trust: SpoolTrust::sample_full(),
499            registered_at: Timestamp::sample_full(),
500            updated_at: later_instant(),
501            retired_at: None,
502        }
503    }
504
505    fn sample_minimal() -> Self {
506        Self {
507            color: None,
508            tare_g: None,
509            loaded_on_tool: None,
510            trust: SpoolTrust::Verified,
511            retired_at: None,
512            ..Self::sample_full()
513        }
514    }
515
516    fn sample_alternates() -> Vec<Self> {
517        vec![Self {
518            remaining_g: 12.5,
519            loaded_on_tool: None,
520            retired_at: Some(later_instant()),
521            ..Self::sample_full()
522        }]
523    }
524}
525
526impl Sample for ClearedMark {
527    fn sample_full() -> Self {
528        Self {
529            spool_id: replaced_spool_id(),
530            reason: UnverifiedReason::FilamentChangeUnconfirmed {
531                print_id: PrintId::sample_full(),
532            },
533        }
534    }
535}
536
537impl Sample for LoadedSpool {
538    fn sample_full() -> Self {
539        Self {
540            tool: 0,
541            spool_id: spool_id(),
542        }
543    }
544}
545
546impl Sample for PrintOutcome {
547    fn sample_full() -> Self {
548        Self::Completed
549    }
550}
551
552impl Sample for ChargeMethod {
553    fn sample_full() -> Self {
554        Self::GcodeExtrusion
555    }
556}
557
558impl Sample for ChargeKey {
559    fn sample_full() -> Self {
560        Self::Print {
561            print_id: PrintId::sample_full(),
562            tool: 0,
563            segment: 1,
564        }
565    }
566
567    fn sample_alternates() -> Vec<Self> {
568        vec![Self::Unobserved {
569            origin: "local".to_owned(),
570            file_name: "benchy.gcode".to_owned(),
571            generation: 2,
572            print_number: 7,
573            tool: 0,
574        }]
575    }
576}
577
578impl Sample for FilePosition {
579    fn sample_full() -> Self {
580        Self {
581            bytes: 1_761_607,
582            size_bytes: Some(4_194_304),
583            observed_at: Timestamp::sample_full(),
584        }
585    }
586
587    fn sample_minimal() -> Self {
588        Self {
589            size_bytes: None,
590            ..Self::sample_full()
591        }
592    }
593}
594
595impl Sample for PendingCharge {
596    fn sample_full() -> Self {
597        Self {
598            charge_key: ChargeKey::sample_full(),
599            print_id: PrintId::sample_full(),
600            tool: 0,
601            length_mm: 1_204.5,
602            method: ChargeMethod::GcodeExtrusion,
603            analysed_length_mm: Some(5_120.0),
604            position: Some(FilePosition::sample_full()),
605            note: Some("fed after the filament change at layer 12".to_owned()),
606            pended_at: later_instant(),
607        }
608    }
609
610    fn sample_minimal() -> Self {
611        Self {
612            method: ChargeMethod::FileFraction,
613            analysed_length_mm: None,
614            position: None,
615            note: None,
616            ..Self::sample_full()
617        }
618    }
619}
620
621impl Sample for PendingTaken {
622    fn sample_full() -> Self {
623        Self {
624            charge_key: ChargeKey::sample_full(),
625            print_id: PrintId::sample_full(),
626        }
627    }
628}
629
630impl Sample for PreflightVerdict {
631    fn sample_full() -> Self {
632        Self::Insufficient
633    }
634}
635
636impl Sample for PreflightTool {
637    fn sample_full() -> Self {
638        Self {
639            tool: 0,
640            spool_id: Some(spool_id()),
641            spool_name: Some("Galaxy black".to_owned()),
642            required_g: Some(120.0),
643            buffer_g: Some(12.0),
644            remaining_g: Some(80.0),
645            marks: vec![UnverifiedMark::sample_full()],
646            verdict: PreflightVerdict::Insufficient,
647        }
648    }
649
650    fn sample_minimal() -> Self {
651        Self {
652            tool: 1,
653            spool_id: None,
654            spool_name: None,
655            required_g: None,
656            buffer_g: None,
657            remaining_g: None,
658            marks: Vec::new(),
659            verdict: PreflightVerdict::Unchecked,
660        }
661    }
662}
663
664impl Sample for Preflight {
665    fn sample_full() -> Self {
666        Self {
667            file_name: "benchy.gcode".to_owned(),
668            tools: vec![
669                PreflightTool::sample_full(),
670                PreflightTool::sample_minimal(),
671            ],
672            verdict: PreflightVerdict::Insufficient,
673            checked_at: Timestamp::sample_full(),
674        }
675    }
676}
677
678impl Sample for SpoolCommand {
679    fn sample_full() -> Self {
680        Self::RegisterSpool {
681            name: "Galaxy black".to_owned(),
682            material: "PLA".to_owned(),
683            color: Some("black".to_owned()),
684            diameter_mm: Some(1.75),
685            density_g_cm3: Some(1.24),
686            initial_g: 1_000.0,
687            remaining_g: Some(742.5),
688            tare_g: Some(250.0),
689            reason: "a new spool went on the shelf".to_owned(),
690            actor: Actor::Operator,
691        }
692    }
693
694    fn sample_minimal() -> Self {
695        Self::RegisterSpool {
696            name: "Galaxy black".to_owned(),
697            material: "PLA".to_owned(),
698            color: None,
699            diameter_mm: None,
700            density_g_cm3: None,
701            initial_g: 1_000.0,
702            remaining_g: None,
703            tare_g: None,
704            reason: "a new spool went on the shelf".to_owned(),
705            actor: Actor::Operator,
706        }
707    }
708
709    fn sample_alternates() -> Vec<Self> {
710        let reason = || "the spool on the shelf changed".to_owned();
711        vec![
712            Self::LoadSpool {
713                spool_id: spool_id(),
714                tool: 0,
715                reason: reason(),
716                actor: Actor::Operator,
717            },
718            Self::UnloadSpool {
719                spool_id: spool_id(),
720                reason: reason(),
721                actor: Actor::Operator,
722            },
723            Self::AdjustSpool {
724                spool_id: spool_id(),
725                remaining_g: Some(700.0),
726                gross_g: None,
727                reason: reason(),
728                actor: Actor::Operator,
729            },
730            Self::AdjustSpool {
731                spool_id: spool_id(),
732                remaining_g: None,
733                gross_g: Some(950.0),
734                reason: reason(),
735                actor: Actor::sample_full(),
736            },
737            Self::RetireSpool {
738                spool_id: spool_id(),
739                reason: reason(),
740                actor: Actor::Operator,
741            },
742        ]
743    }
744}