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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::prometheus::Prometheus;
use anyhow::format_err;
use std::time::Duration;

pub struct PrometheusRangeView<'a> {
    prometheus: &'a Prometheus,
    start: Duration,
    end: Duration,
}

impl<'a> PrometheusRangeView<'a> {
    pub fn new(prometheus: &'a Prometheus, start: Duration, end: Duration) -> Self {
        Self {
            prometheus,
            start,
            end,
        }
    }

    pub fn avg_txns_per_block(&self) -> Option<f64> {
        self.query_avg(
            "txn_per_block",
            "irate(diem_consensus_num_txns_per_block_sum[1m])/irate(diem_consensus_num_txns_per_block_count[1m])".to_string(),
        )
    }

    pub fn avg_backup_bytes_per_second(&self) -> Option<f64> {
        self.query_avg(
            "backup_bytes_per_second",
            "sum(irate(diem_backup_service_sent_bytes[1m])) by(peer_id)".to_string(),
        )
    }
}

impl<'a> PrometheusRangeView<'a> {
    const STEP: u64 = 10;

    fn query_avg(&self, name: &str, query: String) -> Option<f64> {
        self.prometheus
            .query_range_avg(query, &self.start, &self.end, Self::STEP)
            .map_err(|e| format_err!("No {} data: {}", name, e))
            .ok()
    }
}