Skip to main content

printobserver_core/records/
charge.rs

1//! What a print took from a spool, and the durable identity of each debit.
2//!
3//! A charge is keyed rather than counted: every debit carries the
4//! [`ChargeKey`] naming exactly which stretch of which print on which tool it
5//! is for, so a replay of the same observation finds the key already charged
6//! and debits nothing twice. Filament a tool fed after a filament change nobody
7//! confirmed is held as a [`PendingCharge`] until somebody says which spool fed
8//! it, so a spool that really was fed is never left undebited.
9
10use printobserver_types::schemars::JsonSchema;
11use printobserver_types::serde::{Deserialize, Serialize};
12use printobserver_types::{PrintId, Timestamp};
13
14/// How a print ended.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
16#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
17#[schemars(crate = "printobserver_types::schemars")]
18pub enum PrintOutcome {
19    /// It ran to its end.
20    Completed,
21    /// Somebody cancelled it.
22    Cancelled,
23    /// It stopped on an error.
24    Failed,
25}
26
27/// How the length a charge debits was worked out.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
29#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
30#[schemars(crate = "printobserver_types::schemars")]
31pub enum ChargeMethod {
32    /// A completed print: the length the file's analysis gives.
33    SlicerEstimate,
34    /// The extrusion the file's own moves add up to, up to a position in it.
35    GcodeExtrusion,
36    /// The analysed length scaled by how far into the file the print got; the
37    /// fallback used only when the file itself could not be read.
38    FileFraction,
39    /// A completed print this system never saw: the file's analysed length.
40    UnobservedEstimate,
41}
42
43/// The durable identity of one debit, so that a replay can never debit twice.
44#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
45#[serde(
46    crate = "printobserver_types::serde",
47    tag = "key",
48    rename_all = "snake_case",
49    deny_unknown_fields
50)]
51#[schemars(crate = "printobserver_types::schemars")]
52pub enum ChargeKey {
53    /// One segment of a print this system watched, on one tool.
54    Print {
55        /// The print.
56        print_id: PrintId,
57        /// The tool, in the printer's own numbering.
58        tool: i64,
59        /// Which segment: zero from the print's start, and one more each time
60        /// a mid-print swap or a filament-change signal closes the current one.
61        segment: u32,
62    },
63    /// One print this system never saw, on one tool.
64    Unobserved {
65        /// Where the file lives, in the printer's own vocabulary.
66        origin: String,
67        /// The file that was printed.
68        file_name: String,
69        /// The file's tally generation, which advances each time the printer's
70        /// tally for the file is reset.
71        generation: u32,
72        /// The print's ordinal in the file's tally — completed and failed
73        /// prints counted up to and including this one — within that
74        /// generation.
75        print_number: i64,
76        /// The tool, in the printer's own numbering.
77        tool: i64,
78    },
79}
80
81impl ChargeKey {
82    /// The one spelling this key is stored under.
83    ///
84    /// `print:<print_id>:<tool>:<segment>` for a watched print and
85    /// `unobserved:<origin>:<generation>:<print_number>:<tool>:<file_name>`
86    /// for one this system never saw — the file name last, because it is the
87    /// one part that may itself carry a colon.
88    #[must_use]
89    pub fn canonical(&self) -> String {
90        match self {
91            Self::Print {
92                print_id,
93                tool,
94                segment,
95            } => format!("print:{print_id}:{tool}:{segment}"),
96            Self::Unobserved {
97                origin,
98                file_name,
99                generation,
100                print_number,
101                tool,
102            } => format!("unobserved:{origin}:{generation}:{print_number}:{tool}:{file_name}"),
103        }
104    }
105}
106
107/// How far into its file a print had got, and when that was read.
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
109#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
110#[schemars(crate = "printobserver_types::schemars")]
111pub struct FilePosition {
112    /// The byte the printer had read the file up to.
113    pub bytes: i64,
114    /// The file's size in bytes, when it was known.
115    #[serde(default, skip_serializing_if = "Option::is_none")]
116    pub size_bytes: Option<i64>,
117    /// When the position was read.
118    pub observed_at: Timestamp,
119}
120
121/// Filament a tool fed after a filament change nobody has confirmed, held until
122/// somebody says which spool fed it.
123///
124/// A length rather than grams, because the grams follow the spool that takes
125/// it.
126#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
127#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
128#[schemars(crate = "printobserver_types::schemars")]
129pub struct PendingCharge {
130    /// The key of the print segment the filament was fed in.
131    pub charge_key: ChargeKey,
132    /// The print it was fed in.
133    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
134    pub print_id: PrintId,
135    /// The tool that fed it.
136    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
137    pub tool: i64,
138    /// How much was fed, in millimetres.
139    pub length_mm: f64,
140    /// How that length was worked out.
141    pub method: ChargeMethod,
142    /// The file's analysed length for the tool, in millimetres, when it had one.
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    pub analysed_length_mm: Option<f64>,
145    /// How far into the file the segment ran, when that was read.
146    #[serde(default, skip_serializing_if = "Option::is_none")]
147    pub position: Option<FilePosition>,
148    /// Anything a reader needs to make sense of the figure.
149    #[serde(default, skip_serializing_if = "Option::is_none")]
150    pub note: Option<String>,
151    /// When it was held.
152    pub pended_at: Timestamp,
153}
154
155/// One pending charge a spool command took.
156#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
157#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
158#[schemars(crate = "printobserver_types::schemars")]
159pub struct PendingTaken {
160    /// The key it is charged under.
161    pub charge_key: ChargeKey,
162    /// The print it was fed in.
163    pub print_id: PrintId,
164}