hyprstream_metrics/checkpoint/
state.rs1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct CheckpointMetadata {
10 pub id: String,
12 pub name: Option<String>,
14 pub created_at: DateTime<Utc>,
16 pub tables: Vec<TableCheckpoint>,
18 pub total_size: u64,
20 pub metadata: HashMap<String, String>,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct TableCheckpoint {
27 pub name: String,
29 pub row_count: u64,
31 pub file_size: u64,
33 pub file_path: String,
35}
36
37#[derive(Debug, Clone)]
39pub struct Checkpoint {
40 pub commit_id: String,
42 pub metadata: CheckpointMetadata,
44}
45
46impl Checkpoint {
47 pub fn new(commit_id: String, metadata: CheckpointMetadata) -> Self {
49 Self {
50 commit_id,
51 metadata,
52 }
53 }
54
55 pub fn id(&self) -> &str {
57 &self.commit_id
58 }
59
60 pub fn name(&self) -> Option<&str> {
62 self.metadata.name.as_deref()
63 }
64
65 pub fn created_at(&self) -> DateTime<Utc> {
67 self.metadata.created_at
68 }
69
70 pub fn tables(&self) -> &[TableCheckpoint] {
72 &self.metadata.tables
73 }
74
75 pub fn total_size(&self) -> u64 {
77 self.metadata.total_size
78 }
79}
80
81impl CheckpointMetadata {
82 pub fn new(tables: Vec<TableCheckpoint>) -> Self {
84 let total_size = tables.iter().map(|t| t.file_size).sum();
85 Self {
86 id: String::new(), name: None,
88 created_at: Utc::now(),
89 tables,
90 total_size,
91 metadata: HashMap::new(),
92 }
93 }
94
95 pub fn with_name(mut self, name: impl Into<String>) -> Self {
97 self.name = Some(name.into());
98 self
99 }
100
101 pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
103 self.metadata.insert(key.into(), value.into());
104 self
105 }
106}
107
108impl TableCheckpoint {
109 pub fn new(name: String, row_count: u64, file_size: u64, file_path: String) -> Self {
111 Self {
112 name,
113 row_count,
114 file_size,
115 file_path,
116 }
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn test_checkpoint_metadata_creation() {
126 let tables = vec![
127 TableCheckpoint::new(
128 "metrics".to_owned(),
129 1000,
130 50000,
131 "tables/metrics.parquet".to_owned(),
132 ),
133 TableCheckpoint::new(
134 "events".to_owned(),
135 500,
136 25000,
137 "tables/events.parquet".to_owned(),
138 ),
139 ];
140
141 let metadata = CheckpointMetadata::new(tables)
142 .with_name("checkpoint-1")
143 .with_metadata("version", "1.0");
144
145 assert_eq!(metadata.name, Some("checkpoint-1".to_owned()));
146 assert_eq!(metadata.total_size, 75000);
147 assert_eq!(metadata.tables.len(), 2);
148 assert_eq!(metadata.metadata.get("version"), Some(&"1.0".to_owned()));
149 }
150
151 #[test]
152 fn test_checkpoint_creation() {
153 let tables = vec![TableCheckpoint::new(
154 "test".to_owned(),
155 100,
156 5000,
157 "tables/test.parquet".to_owned(),
158 )];
159
160 let metadata = CheckpointMetadata::new(tables);
161 let checkpoint = Checkpoint::new("abc123".to_owned(), metadata);
162
163 assert_eq!(checkpoint.id(), "abc123");
164 assert_eq!(checkpoint.tables().len(), 1);
165 assert_eq!(checkpoint.total_size(), 5000);
166 }
167}