Skip to main content

gluex_ccdb/
models.rs

1use crate::CCDBResult;
2use chrono::{DateTime, Utc};
3use gluex_core::{parsers::parse_timestamp, Id, RunNumber};
4use std::fmt::Display;
5
6/// Typed representation of a column type.
7#[derive(Debug, Copy, Clone, Default)]
8pub enum ColumnType {
9    /// A column of signed integers (i32).
10    Int,
11    /// A column of unsigned integers (u32).
12    UInt,
13    /// A column of signed integers (i64).
14    Long,
15    /// A column of unsigned integers (u64).
16    ULong,
17    /// A column of floating-point values (f64).
18    #[default]
19    Double,
20    /// A column of UTF-8 encoded strings.
21    String,
22    /// A column of boolean values.
23    Bool,
24}
25impl ColumnType {
26    /// Attempts to build a [`ColumnType`] from the identifier stored in CCDB.
27    #[must_use]
28    pub fn type_from_str(s: &str) -> Option<Self> {
29        match s {
30            "int" => Some(Self::Int),
31            "uint" => Some(Self::UInt),
32            "long" => Some(Self::Long),
33            "ulong" => Some(Self::ULong),
34            "double" => Some(Self::Double),
35            "bool" => Some(Self::Bool),
36            "string" => Some(Self::String),
37            _ => None,
38        }
39    }
40
41    /// Returns the identifier string stored in CCDB for this type.
42    #[must_use]
43    pub fn as_str(&self) -> &'static str {
44        match self {
45            Self::Int => "int",
46            Self::UInt => "uint",
47            Self::Long => "long",
48            Self::ULong => "ulong",
49            Self::Double => "double",
50            Self::Bool => "bool",
51            Self::String => "string",
52        }
53    }
54}
55impl Display for ColumnType {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        write!(f, "{}", self.as_str())
58    }
59}
60
61/// Metadata row describing a column belonging to a CCDB constant type.
62#[derive(Debug, Clone, Default)]
63pub struct ColumnMeta {
64    pub(crate) id: Id,
65    pub(crate) created: String,
66    pub(crate) modified: String,
67    pub(crate) name: String,
68    pub(crate) type_id: Id,
69    pub(crate) column_type: ColumnType,
70    pub(crate) order: i64,
71    pub(crate) comment: String,
72}
73impl ColumnMeta {
74    /// Identifier of the column definition.
75    #[must_use]
76    pub fn id(&self) -> Id {
77        self.id
78    }
79    /// Human readable column name.
80    #[must_use]
81    pub fn name(&self) -> &str {
82        &self.name
83    }
84    /// Identifier of the type table the column belongs to.
85    #[must_use]
86    pub fn type_id(&self) -> Id {
87        self.type_id
88    }
89    /// Typed representation of the stored column data.
90    #[must_use]
91    pub fn column_type(&self) -> ColumnType {
92        self.column_type
93    }
94    /// Ordering index for the column within the table schema.
95    #[must_use]
96    pub fn order(&self) -> i64 {
97        self.order
98    }
99    /// Free-form comment associated with the column.
100    #[must_use]
101    pub fn comment(&self) -> &str {
102        &self.comment
103    }
104    /// Timestamp describing when the column definition was created.
105    ///
106    /// # Errors
107    ///
108    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
109    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
110        Ok(parse_timestamp(&self.created)?)
111    }
112    /// Timestamp describing when the column definition was last updated.
113    ///
114    /// # Errors
115    ///
116    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
117    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
118        Ok(parse_timestamp(&self.modified)?)
119    }
120}
121
122/// Metadata describing a directory entry that groups constant types.
123#[derive(Debug, Clone, Default)]
124pub struct DirectoryMeta {
125    pub(crate) id: Id,
126    pub(crate) created: String,
127    pub(crate) modified: String,
128    pub(crate) name: String,
129    pub(crate) parent_id: Id,
130    pub(crate) author_id: Id,
131    pub(crate) comment: String,
132    pub(crate) is_deprecated: bool,
133    pub(crate) deprecated_by_user_id: Id,
134    pub(crate) is_locked: bool,
135    pub(crate) locked_by_user_id: Id,
136}
137impl DirectoryMeta {
138    /// Identifier of the directory row.
139    #[must_use]
140    pub fn id(&self) -> Id {
141        self.id
142    }
143    /// Human readable directory name.
144    #[must_use]
145    pub fn name(&self) -> &str {
146        &self.name
147    }
148    /// Identifier of the parent directory.
149    #[must_use]
150    pub fn parent_id(&self) -> Id {
151        self.parent_id
152    }
153    /// Identifier of the user who created the directory.
154    #[must_use]
155    pub fn author_id(&self) -> Id {
156        self.author_id
157    }
158    /// Free-form comment describing the directory.
159    #[must_use]
160    pub fn comment(&self) -> &str {
161        &self.comment
162    }
163    /// True when the directory has been marked as deprecated.
164    #[must_use]
165    pub fn is_deprecated(&self) -> bool {
166        self.is_deprecated
167    }
168    /// Identifier of the user who deprecated the directory.
169    #[must_use]
170    pub fn deprecated_by_user_id(&self) -> Id {
171        self.deprecated_by_user_id
172    }
173    /// True when the directory is locked against modification.
174    #[must_use]
175    pub fn is_locked(&self) -> bool {
176        self.is_locked
177    }
178    /// Identifier of the user who locked the directory.
179    #[must_use]
180    pub fn locked_by_user_id(&self) -> Id {
181        self.locked_by_user_id
182    }
183    /// Timestamp describing when the directory was created.
184    ///
185    /// # Errors
186    ///
187    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
188    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
189        Ok(parse_timestamp(&self.created)?)
190    }
191    /// Timestamp describing when the directory was last updated.
192    ///
193    /// # Errors
194    ///
195    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
196    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
197        Ok(parse_timestamp(&self.modified)?)
198    }
199}
200
201/// Metadata describing a CCDB type table containing constants.
202#[derive(Debug, Clone, Default)]
203pub struct TypeTableMeta {
204    pub(crate) id: Id,
205    pub(crate) created: String,
206    pub(crate) modified: String,
207    pub(crate) directory_id: Id,
208    pub(crate) name: String,
209    pub(crate) n_rows: i64,
210    pub(crate) n_columns: i64,
211    pub(crate) n_assignments: i64,
212    pub(crate) author_id: Id,
213    pub(crate) comment: String,
214    pub(crate) is_deprecated: bool,
215    pub(crate) deprecated_by_user_id: Id,
216    pub(crate) is_locked: bool,
217    pub(crate) locked_by_user_id: Id,
218    pub(crate) lock_time: String,
219}
220
221impl TypeTableMeta {
222    /// Identifier of the type table.
223    #[must_use]
224    pub fn id(&self) -> Id {
225        self.id
226    }
227    /// Identifier of the directory containing the type.
228    #[must_use]
229    pub fn directory_id(&self) -> Id {
230        self.directory_id
231    }
232    /// Name of the type table.
233    #[must_use]
234    pub fn name(&self) -> &str {
235        &self.name
236    }
237    /// Number of rows stored in the table.
238    #[must_use]
239    pub fn n_rows(&self) -> i64 {
240        self.n_rows
241    }
242    /// Number of columns defined for the table.
243    #[must_use]
244    pub fn n_columns(&self) -> i64 {
245        self.n_columns
246    }
247    /// Number of assignments referencing this table.
248    #[must_use]
249    pub fn n_assignments(&self) -> i64 {
250        self.n_assignments
251    }
252    /// Identifier of the user who created the type.
253    #[must_use]
254    pub fn author_id(&self) -> Id {
255        self.author_id
256    }
257    /// Free-form comment explaining the type table.
258    #[must_use]
259    pub fn comment(&self) -> &str {
260        &self.comment
261    }
262    /// True when the type has been deprecated.
263    #[must_use]
264    pub fn is_deprecated(&self) -> bool {
265        self.is_deprecated
266    }
267    /// Identifier of the user who deprecated the type.
268    #[must_use]
269    pub fn deprecated_by_user_id(&self) -> Id {
270        self.deprecated_by_user_id
271    }
272    /// True when the type is locked.
273    #[must_use]
274    pub fn is_locked(&self) -> bool {
275        self.is_locked
276    }
277    /// Identifier of the user who locked the type.
278    #[must_use]
279    pub fn locked_by_user_id(&self) -> Id {
280        self.locked_by_user_id
281    }
282    /// Timestamp describing when the type was created.
283    ///
284    /// # Errors
285    ///
286    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
287    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
288        Ok(parse_timestamp(&self.created)?)
289    }
290    /// Timestamp describing when the type metadata was updated.
291    ///
292    /// # Errors
293    ///
294    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
295    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
296        Ok(parse_timestamp(&self.modified)?)
297    }
298    /// Timestamp describing when the type was locked.
299    ///
300    /// # Errors
301    ///
302    /// Returns an error if the stored lock timestamp cannot be parsed as a UTC datetime.
303    pub fn lock_time(&self) -> CCDBResult<DateTime<Utc>> {
304        Ok(parse_timestamp(&self.lock_time)?)
305    }
306}
307
308/// Metadata describing a stored set of constants for a type table.
309#[derive(Debug, Clone, Default)]
310pub struct ConstantSetMeta {
311    pub(crate) id: Id,
312    pub(crate) created: String,
313    pub(crate) modified: String,
314    pub(crate) vault: String,
315    pub(crate) constant_type_id: Id,
316}
317
318impl ConstantSetMeta {
319    /// Identifier of the constant set.
320    #[must_use]
321    pub fn id(&self) -> Id {
322        self.id
323    }
324    /// Vault path or identifier for the backing data blob.
325    #[must_use]
326    pub fn vault(&self) -> &str {
327        &self.vault
328    }
329    /// Identifier of the type table the set belongs to.
330    #[must_use]
331    pub fn constant_type_id(&self) -> Id {
332        self.constant_type_id
333    }
334    /// Timestamp describing when the set was created.
335    ///
336    /// # Errors
337    ///
338    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
339    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
340        Ok(parse_timestamp(&self.created)?)
341    }
342    /// Timestamp describing when the set was last modified.
343    ///
344    /// # Errors
345    ///
346    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
347    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
348        Ok(parse_timestamp(&self.modified)?)
349    }
350}
351
352/// Metadata describing an assignment of a constant set to a run/event range.
353#[derive(Debug, Clone, Default)]
354pub struct AssignmentMeta {
355    pub(crate) id: Id,
356    pub(crate) created: String,
357    pub(crate) modified: String,
358    pub(crate) variation_id: Id,
359    pub(crate) run_range_id: Id,
360    pub(crate) event_range_id: Id,
361    pub(crate) author_id: Id,
362    pub(crate) comment: String,
363    pub(crate) constant_set_id: Id,
364}
365impl AssignmentMeta {
366    /// Identifier of the assignment.
367    #[must_use]
368    pub fn id(&self) -> Id {
369        self.id
370    }
371    /// Identifier of the variation referenced by the assignment.
372    #[must_use]
373    pub fn variation_id(&self) -> Id {
374        self.variation_id
375    }
376    /// Identifier of the associated run range.
377    #[must_use]
378    pub fn run_range_id(&self) -> Id {
379        self.run_range_id
380    }
381    /// Identifier of the associated event range.
382    #[must_use]
383    pub fn event_range_id(&self) -> Id {
384        self.event_range_id
385    }
386    /// Identifier of the user who created the assignment.
387    #[must_use]
388    pub fn author_id(&self) -> Id {
389        self.author_id
390    }
391    /// Free-form comment associated with the assignment.
392    #[must_use]
393    pub fn comment(&self) -> &str {
394        &self.comment
395    }
396    /// Identifier of the constant set referenced by the assignment.
397    #[must_use]
398    pub fn constant_set_id(&self) -> Id {
399        self.constant_set_id
400    }
401    /// Timestamp describing when the assignment was created.
402    ///
403    /// # Errors
404    ///
405    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
406    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
407        Ok(parse_timestamp(&self.created)?)
408    }
409    /// Timestamp describing when the assignment was last updated.
410    ///
411    /// # Errors
412    ///
413    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
414    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
415        Ok(parse_timestamp(&self.modified)?)
416    }
417}
418
419/// Lightweight assignment row containing only identity and creation info.
420#[derive(Debug, Clone, Default)]
421pub struct AssignmentMetaLite {
422    pub(crate) id: Id,
423    pub(crate) created: String,
424    pub(crate) constant_set_id: Id,
425}
426impl AssignmentMetaLite {
427    /// Identifier of the assignment.
428    #[must_use]
429    pub fn id(&self) -> Id {
430        self.id
431    }
432    /// Identifier of the constant set referenced by the assignment.
433    #[must_use]
434    pub fn constant_set_id(&self) -> Id {
435        self.constant_set_id
436    }
437    /// Timestamp describing when the assignment was created.
438    ///
439    /// # Errors
440    ///
441    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
442    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
443        Ok(parse_timestamp(&self.created)?)
444    }
445}
446
447/// Metadata describing a variation that partitions assignments.
448#[derive(Debug, Clone, Default)]
449pub struct VariationMeta {
450    pub(crate) id: Id,
451    pub(crate) created: String,
452    pub(crate) modified: String,
453    pub(crate) name: String,
454    pub(crate) description: String,
455    pub(crate) author_id: Id,
456    pub(crate) comment: String,
457    pub(crate) parent_id: Id,
458    pub(crate) is_locked: bool,
459    pub(crate) lock_time: String,
460    pub(crate) locked_by_user_id: Id,
461    pub(crate) go_back_behavior: i64,
462    pub(crate) go_back_time: String,
463    pub(crate) is_deprecated: bool,
464    pub(crate) deprecated_by_user_id: Id,
465}
466impl VariationMeta {
467    /// Identifier of the variation row.
468    #[must_use]
469    pub fn id(&self) -> Id {
470        self.id
471    }
472    /// Human readable variation name.
473    #[must_use]
474    pub fn name(&self) -> &str {
475        &self.name
476    }
477    /// Optional descriptive text for the variation.
478    #[must_use]
479    pub fn description(&self) -> &str {
480        &self.description
481    }
482    /// Identifier of the user who created the variation.
483    #[must_use]
484    pub fn author_id(&self) -> Id {
485        self.author_id
486    }
487    /// Free-form comment associated with the variation.
488    #[must_use]
489    pub fn comment(&self) -> &str {
490        &self.comment
491    }
492    /// Identifier of the parent variation.
493    #[must_use]
494    pub fn parent_id(&self) -> Id {
495        self.parent_id
496    }
497    /// True when the variation is locked.
498    #[must_use]
499    pub fn is_locked(&self) -> bool {
500        self.is_locked
501    }
502    /// Identifier of the user who locked the variation.
503    #[must_use]
504    pub fn locked_by_user_id(&self) -> Id {
505        self.locked_by_user_id
506    }
507    /// Behavior flag defining how lookups walk parent variations.
508    #[must_use]
509    pub fn go_back_behavior(&self) -> i64 {
510        self.go_back_behavior
511    }
512    /// True when the variation is deprecated.
513    #[must_use]
514    pub fn is_deprecated(&self) -> bool {
515        self.is_deprecated
516    }
517    /// Identifier of the user who deprecated the variation.
518    #[must_use]
519    pub fn deprecated_by_user_id(&self) -> Id {
520        self.deprecated_by_user_id
521    }
522    /// Timestamp describing when the variation was created.
523    ///
524    /// # Errors
525    ///
526    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
527    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
528        Ok(parse_timestamp(&self.created)?)
529    }
530    /// Timestamp describing when the variation metadata was updated.
531    ///
532    /// # Errors
533    ///
534    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
535    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
536        Ok(parse_timestamp(&self.modified)?)
537    }
538    /// Timestamp describing when the variation was locked.
539    ///
540    /// # Errors
541    ///
542    /// Returns an error if the stored lock timestamp cannot be parsed as a UTC datetime.
543    pub fn lock_time(&self) -> CCDBResult<DateTime<Utc>> {
544        Ok(parse_timestamp(&self.lock_time)?)
545    }
546    /// Timestamp describing when the go-back window expires.
547    ///
548    /// # Errors
549    ///
550    /// Returns an error if the stored go-back timestamp cannot be parsed as a UTC datetime.
551    pub fn go_back_time(&self) -> CCDBResult<DateTime<Utc>> {
552        Ok(parse_timestamp(&self.go_back_time)?)
553    }
554}
555
556/// Metadata describing an inclusive range of run numbers.
557#[derive(Debug, Clone, Default)]
558pub struct RunRangeMeta {
559    pub(crate) id: Id,
560    pub(crate) created: String,
561    pub(crate) modified: String,
562    pub(crate) name: String,
563    pub(crate) run_min: RunNumber,
564    pub(crate) run_max: RunNumber,
565    pub(crate) comment: String,
566}
567
568impl RunRangeMeta {
569    /// Identifier of the run range.
570    #[must_use]
571    pub fn id(&self) -> Id {
572        self.id
573    }
574    /// Human readable name of the run range.
575    #[must_use]
576    pub fn name(&self) -> &str {
577        &self.name
578    }
579    /// Minimum run number included in the range.
580    #[must_use]
581    pub fn run_min(&self) -> RunNumber {
582        self.run_min
583    }
584    /// Maximum run number included in the range.
585    #[must_use]
586    pub fn run_max(&self) -> RunNumber {
587        self.run_max
588    }
589    /// Free-form comment describing the run range.
590    #[must_use]
591    pub fn comment(&self) -> &str {
592        &self.comment
593    }
594    /// Timestamp describing when the run range was created.
595    ///
596    /// # Errors
597    ///
598    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
599    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
600        Ok(parse_timestamp(&self.created)?)
601    }
602    /// Timestamp describing when the run range metadata was updated.
603    ///
604    /// # Errors
605    ///
606    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
607    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
608        Ok(parse_timestamp(&self.modified)?)
609    }
610}
611
612/// Metadata describing an inclusive event range bound to a run.
613#[derive(Debug, Clone, Default)]
614pub struct EventRangeMeta {
615    pub(crate) id: Id,
616    pub(crate) created: String,
617    pub(crate) modified: String,
618    pub(crate) run_number: RunNumber,
619    pub(crate) event_min: i64,
620    pub(crate) event_max: i64,
621    pub(crate) comment: String,
622}
623
624impl EventRangeMeta {
625    /// Identifier of the event range.
626    #[must_use]
627    pub fn id(&self) -> Id {
628        self.id
629    }
630    /// Run number this event range belongs to.
631    #[must_use]
632    pub fn run_number(&self) -> RunNumber {
633        self.run_number
634    }
635    /// Minimum event number included in the range.
636    #[must_use]
637    pub fn event_min(&self) -> i64 {
638        self.event_min
639    }
640    /// Maximum event number included in the range.
641    #[must_use]
642    pub fn event_max(&self) -> i64 {
643        self.event_max
644    }
645    /// Free-form comment describing the event range.
646    #[must_use]
647    pub fn comment(&self) -> &str {
648        &self.comment
649    }
650    /// Timestamp describing when the event range was created.
651    ///
652    /// # Errors
653    ///
654    /// Returns an error if the stored creation timestamp cannot be parsed as a UTC datetime.
655    pub fn created(&self) -> CCDBResult<DateTime<Utc>> {
656        Ok(parse_timestamp(&self.created)?)
657    }
658    /// Timestamp describing when the event range metadata was updated.
659    ///
660    /// # Errors
661    ///
662    /// Returns an error if the stored modification timestamp cannot be parsed as a UTC datetime.
663    pub fn modified(&self) -> CCDBResult<DateTime<Utc>> {
664        Ok(parse_timestamp(&self.modified)?)
665    }
666}