Skip to content

Commit f820090

Browse files
author
Devdutt Shenoi
committed
refactor: separate constructors
1 parent 0a07fbc commit f820090

File tree

1 file changed

+66
-52
lines changed

1 file changed

+66
-52
lines changed

src/stats.rs

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,69 @@ pub struct Stats {
4545
}
4646

4747
impl Stats {
48+
fn get_current(event_labels: &[&str], storage_size_labels: &[&str]) -> Option<Self> {
49+
let events = EVENTS_INGESTED
50+
.get_metric_with_label_values(event_labels)
51+
.ok()?
52+
.get() as u64;
53+
let ingestion = EVENTS_INGESTED_SIZE
54+
.get_metric_with_label_values(event_labels)
55+
.ok()?
56+
.get() as u64;
57+
let storage = STORAGE_SIZE
58+
.get_metric_with_label_values(storage_size_labels)
59+
.ok()?
60+
.get() as u64;
61+
62+
Some(Self {
63+
events,
64+
ingestion,
65+
storage,
66+
})
67+
}
68+
69+
fn get_lifetime(event_labels: &[&str], storage_size_labels: &[&str]) -> Option<Self> {
70+
let events = LIFETIME_EVENTS_INGESTED
71+
.get_metric_with_label_values(event_labels)
72+
.ok()?
73+
.get() as u64;
74+
let ingestion = LIFETIME_EVENTS_INGESTED_SIZE
75+
.get_metric_with_label_values(event_labels)
76+
.ok()?
77+
.get() as u64;
78+
let storage = LIFETIME_EVENTS_STORAGE_SIZE
79+
.get_metric_with_label_values(storage_size_labels)
80+
.ok()?
81+
.get() as u64;
82+
83+
Some(Self {
84+
events,
85+
ingestion,
86+
storage,
87+
})
88+
}
89+
90+
fn get_deleted(event_labels: &[&str], storage_size_labels: &[&str]) -> Option<Self> {
91+
let events = EVENTS_DELETED
92+
.get_metric_with_label_values(event_labels)
93+
.ok()?
94+
.get() as u64;
95+
let ingestion = EVENTS_DELETED_SIZE
96+
.get_metric_with_label_values(event_labels)
97+
.ok()?
98+
.get() as u64;
99+
let storage = DELETED_EVENTS_STORAGE_SIZE
100+
.get_metric_with_label_values(storage_size_labels)
101+
.ok()?
102+
.get() as u64;
103+
104+
Some(Self {
105+
events,
106+
ingestion,
107+
storage,
108+
})
109+
}
110+
48111
pub fn for_stream_on_date(date: NaiveDate, stream_name: &str) -> Stats {
49112
let date = date.to_string();
50113
let event_labels = event_labels_date(stream_name, "json", &date);
@@ -141,59 +204,10 @@ impl FullStats {
141204
let event_labels = event_labels(stream_name, format);
142205
let storage_size_labels = storage_size_labels(stream_name);
143206

144-
let events_ingested = EVENTS_INGESTED
145-
.get_metric_with_label_values(&event_labels)
146-
.ok()?
147-
.get() as u64;
148-
let ingestion_size = EVENTS_INGESTED_SIZE
149-
.get_metric_with_label_values(&event_labels)
150-
.ok()?
151-
.get() as u64;
152-
let storage_size = STORAGE_SIZE
153-
.get_metric_with_label_values(&storage_size_labels)
154-
.ok()?
155-
.get() as u64;
156-
let events_deleted = EVENTS_DELETED
157-
.get_metric_with_label_values(&event_labels)
158-
.ok()?
159-
.get() as u64;
160-
let events_deleted_size = EVENTS_DELETED_SIZE
161-
.get_metric_with_label_values(&event_labels)
162-
.ok()?
163-
.get() as u64;
164-
let deleted_events_storage_size = DELETED_EVENTS_STORAGE_SIZE
165-
.get_metric_with_label_values(&storage_size_labels)
166-
.ok()?
167-
.get() as u64;
168-
let lifetime_events_ingested = LIFETIME_EVENTS_INGESTED
169-
.get_metric_with_label_values(&event_labels)
170-
.ok()?
171-
.get() as u64;
172-
let lifetime_ingestion_size = LIFETIME_EVENTS_INGESTED_SIZE
173-
.get_metric_with_label_values(&event_labels)
174-
.ok()?
175-
.get() as u64;
176-
let lifetime_events_storage_size = LIFETIME_EVENTS_STORAGE_SIZE
177-
.get_metric_with_label_values(&storage_size_labels)
178-
.ok()?
179-
.get() as u64;
180-
181207
Some(FullStats {
182-
lifetime_stats: Stats {
183-
events: lifetime_events_ingested,
184-
ingestion: lifetime_ingestion_size,
185-
storage: lifetime_events_storage_size,
186-
},
187-
current_stats: Stats {
188-
events: events_ingested,
189-
ingestion: ingestion_size,
190-
storage: storage_size,
191-
},
192-
deleted_stats: Stats {
193-
events: events_deleted,
194-
ingestion: events_deleted_size,
195-
storage: deleted_events_storage_size,
196-
},
208+
lifetime_stats: Stats::get_lifetime(&event_labels, &storage_size_labels)?,
209+
current_stats: Stats::get_current(&event_labels, &storage_size_labels)?,
210+
deleted_stats: Stats::get_deleted(&event_labels, &storage_size_labels)?,
197211
})
198212
}
199213
}

0 commit comments

Comments
 (0)