1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use chain_impl_mockchain::fragment::FragmentId;
use jormungandr_automation::jormungandr::{FragmentNode, RemoteJormungandr};
use jormungandr_lib::{
    interfaces::{FragmentLog, FragmentStatus},
    time::SystemTime,
};
use jortestkit::load::{Id, RequestStatusProvider, Status};

pub struct FragmentStatusProvider {
    jormungandr: RemoteJormungandr,
}

impl FragmentStatusProvider {
    pub fn new(jormungandr: RemoteJormungandr) -> Self {
        Self { jormungandr }
    }
}

impl RequestStatusProvider for FragmentStatusProvider {
    fn get_statuses(&self, ids: &[Id]) -> Vec<Status> {
        let fragment_logs = self.jormungandr.fragment_logs().unwrap();
        fragment_logs
            .iter()
            .filter(|(id, _)| ids.contains(&id.to_string()))
            .map(|(id, fragment_log)| into_status(fragment_log, id))
            .collect()
    }
}

fn into_status(fragment_log: &FragmentLog, id: &FragmentId) -> Status {
    match fragment_log.status() {
        FragmentStatus::Pending => {
            let duration = SystemTime::now()
                .duration_since(*fragment_log.received_at())
                .unwrap();
            Status::new_pending(duration.into(), id.to_string())
        }
        FragmentStatus::Rejected { reason } => {
            let duration = fragment_log
                .last_updated_at()
                .duration_since(*fragment_log.received_at())
                .unwrap();
            Status::new_failure(duration.into(), id.to_string(), reason.to_string())
        }
        FragmentStatus::InABlock { .. } => {
            let duration = fragment_log
                .last_updated_at()
                .duration_since(*fragment_log.received_at())
                .unwrap();
            Status::new_success(duration.into(), id.to_string())
        }
    }
}