Skip to content

Commit ea22e38

Browse files
committed
redis-db: add implementation for stats
1 parent 01e3672 commit ea22e38

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

Diff for: common/src/database/redis.rs

+61-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,37 @@ impl Database for RedisDatabase {
486486
subscription: &str,
487487
start_time: i64,
488488
) -> Result<SubscriptionStatsCounters> {
489-
todo!()
489+
let fields = HashMap::<RedisDomain, String>::from([
490+
(RedisDomain::Subscription, subscription.to_string()),
491+
]);
492+
let heartbeats = self.get_heartbeats_by_field(fields).await?;
493+
494+
let total_machines_count = i64::try_from(heartbeats.len())?;
495+
let mut alive_machines_count = 0;
496+
let mut active_machines_count = 0;
497+
let mut dead_machines_count = 0;
498+
499+
for hb in heartbeats.iter() {
500+
match hb {
501+
HeartbeatData{last_seen, last_event_seen, ..} if MachineStatusFilter::Alive.is_match(last_seen, last_event_seen, start_time) => {
502+
alive_machines_count += 1;
503+
},
504+
HeartbeatData{last_seen, last_event_seen, ..} if MachineStatusFilter::Active.is_match(last_seen, last_event_seen, start_time) => {
505+
active_machines_count += 1;
506+
},
507+
HeartbeatData{last_seen, last_event_seen, ..} if MachineStatusFilter::Dead.is_match(last_seen, last_event_seen, start_time) => {
508+
dead_machines_count += 1;
509+
},
510+
_ => {},
511+
};
512+
}
513+
514+
Ok(SubscriptionStatsCounters::new(
515+
total_machines_count,
516+
alive_machines_count,
517+
active_machines_count,
518+
dead_machines_count,
519+
))
490520
}
491521

492522
async fn get_machines(
@@ -495,6 +525,35 @@ impl Database for RedisDatabase {
495525
start_time: i64,
496526
stat_type: Option<SubscriptionMachineState>,
497527
) -> Result<Vec<SubscriptionMachine>> {
498-
todo!()
528+
let fields = HashMap::<RedisDomain, String>::from([
529+
(RedisDomain::Subscription, subscription.to_string()),
530+
]);
531+
let heartbeats = self.get_heartbeats_by_field(fields).await?;
532+
let mut result = Vec::<SubscriptionMachine>::new();
533+
534+
for hb in heartbeats.iter() {
535+
536+
match stat_type {
537+
None => {},
538+
Some(SubscriptionMachineState::Active) => {
539+
if !MachineStatusFilter::Active.is_match(&hb.last_seen, &hb.last_event_seen, start_time) {
540+
continue;
541+
}
542+
},
543+
Some(SubscriptionMachineState::Alive) => {
544+
if !MachineStatusFilter::Alive.is_match(&hb.last_seen, &hb.last_event_seen, start_time) {
545+
continue;
546+
}
547+
},
548+
Some(SubscriptionMachineState::Dead) => {
549+
if !MachineStatusFilter::Dead.is_match(&hb.last_seen, &hb.last_event_seen, start_time) {
550+
continue;
551+
}
552+
},
553+
}
554+
result.push(SubscriptionMachine::new(hb.machine().to_string(), hb.ip().to_string()));
555+
}
556+
557+
Ok(result)
499558
}
500559
}

Diff for: common/src/heartbeat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub struct HeartbeatData {
1313
#[serde(serialize_with = "utils::serialize_timestamp")]
1414
first_seen: Timestamp,
1515
#[serde(serialize_with = "utils::serialize_timestamp")]
16-
last_seen: Timestamp,
16+
pub last_seen: Timestamp,
1717
#[serde(serialize_with = "utils::serialize_option_timestamp")]
18-
last_event_seen: Option<Timestamp>,
18+
pub last_event_seen: Option<Timestamp>,
1919
}
2020

2121
fn serialize_subscription_data<S>(

0 commit comments

Comments
 (0)