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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use jormungandr_automation::jormungandr::JormungandrRest;
use jormungandr_lib::interfaces::FragmentLog;
use serde_json;
use time::OffsetDateTime;

pub struct Harvester {
    rest: JormungandrRest,
    endpoint: String,
    timeout: std::time::Duration,
}

impl Harvester {
    pub fn new<S: Into<String>>(endpoint: S, timeout: std::time::Duration) -> Self {
        let endpoint = endpoint.into();

        Self {
            rest: JormungandrRest::new(endpoint.clone()),
            timeout,
            endpoint,
        }
    }

    pub fn harvest(&self) -> Result<Snapshot, super::Error> {
        let mut votes_count: usize = 0;

        for vote_status in self.rest.vote_plan_statuses()? {
            votes_count += vote_status
                .proposals
                .iter()
                .map(|x| x.votes_cast)
                .sum::<usize>();
        }

        let fragment_logs = self.fragment_logs()?;

        Ok(Snapshot {
            timestamp: OffsetDateTime::now_utc(),
            pending: fragment_logs.iter().filter(|x| x.is_pending()).count(),
            total_tx: fragment_logs.len(),
            votes_count,
        })
    }

    pub fn fragment_logs(&self) -> Result<Vec<FragmentLog>, super::Error> {
        let client = reqwest::blocking::Client::builder()
            .timeout(self.timeout)
            .build()?;

        let res = client
            .get(format!("{}/v0/fragment/logs", self.endpoint))
            .send()?;
        serde_json::from_str(&res.text()?).map_err(Into::into)
    }
}

pub struct Snapshot {
    pub timestamp: OffsetDateTime,
    pub votes_count: usize,
    pub pending: usize,
    pub total_tx: usize,
}

impl Snapshot {
    pub fn header(&self) -> String {
        "date,\tvotes-count,\tpending,\ttotal-tx".to_string()
    }

    pub fn entry(&self) -> String {
        format!(
            "{},\t{},\t{},\t{}",
            self.timestamp, self.votes_count, self.pending, self.total_tx
        )
    }

    pub fn to_console_output(&self) -> String {
        format!(
            "date: {},\tvotes-count: {},\tpending: {},\ttotal-tx: {}",
            self.timestamp, self.votes_count, self.pending, self.total_tx
        )
    }
}