explorerd/service/
statistics.rs1use tinyjson::JsonValue;
20
21use darkfi::{Error, Result};
22use darkfi_sdk::blockchain::block_epoch;
23
24use crate::{service::ExplorerService, store::metrics::GasMetrics};
25
26#[derive(Debug, Clone)]
27pub struct BaseStatistics {
29 pub height: u32,
31 pub epoch: u8,
33 pub last_block: String,
35 pub total_blocks: usize,
37 pub total_txs: usize,
39}
40
41impl BaseStatistics {
42 pub fn to_json_array(&self) -> JsonValue {
44 JsonValue::Array(vec![
45 JsonValue::Number(self.height as f64),
46 JsonValue::Number(self.epoch as f64),
47 JsonValue::String(self.last_block.clone()),
48 JsonValue::Number(self.total_blocks as f64),
49 JsonValue::Number(self.total_txs as f64),
50 ])
51 }
52}
53
54#[derive(Default)]
56pub struct MetricStatistics {
57 pub metrics: GasMetrics,
59}
60
61impl MetricStatistics {
62 pub fn new(metrics: GasMetrics) -> Self {
63 Self { metrics }
64 }
65
66 pub fn to_json_array(&self) -> JsonValue {
68 JsonValue::Array(vec![
69 JsonValue::Number(self.metrics.avg_total_gas_used() as f64),
70 JsonValue::Number(self.metrics.total_gas.min as f64),
71 JsonValue::Number(self.metrics.total_gas.max as f64),
72 JsonValue::Number(self.metrics.avg_wasm_gas_used() as f64),
73 JsonValue::Number(self.metrics.wasm_gas.min as f64),
74 JsonValue::Number(self.metrics.wasm_gas.max as f64),
75 JsonValue::Number(self.metrics.avg_zk_circuits_gas_used() as f64),
76 JsonValue::Number(self.metrics.zk_circuits_gas.min as f64),
77 JsonValue::Number(self.metrics.zk_circuits_gas.max as f64),
78 JsonValue::Number(self.metrics.avg_signatures_gas_used() as f64),
79 JsonValue::Number(self.metrics.signatures_gas.min as f64),
80 JsonValue::Number(self.metrics.signatures_gas.max as f64),
81 JsonValue::Number(self.metrics.avg_deployments_gas_used() as f64),
82 JsonValue::Number(self.metrics.deployments_gas.min as f64),
83 JsonValue::Number(self.metrics.deployments_gas.max as f64),
84 JsonValue::Number(self.metrics.timestamp.inner() as f64),
85 ])
86 }
87}
88impl ExplorerService {
89 pub fn get_base_statistics(&self) -> Result<Option<BaseStatistics>> {
91 let last_block = self.last_block();
92 Ok(last_block
93 .map_err(|e| {
95 Error::DatabaseError(format!(
96 "[get_base_statistics] Retrieving last block failed: {e:?}"
97 ))
98 })?
99 .map(|(height, header_hash)| {
101 let epoch = block_epoch(height);
102 let total_blocks = self.get_block_count();
103 let total_txs = self.get_transaction_count();
104 BaseStatistics { height, epoch, last_block: header_hash, total_blocks, total_txs }
105 }))
106 }
107
108 pub async fn get_metrics_statistics(&self) -> Result<Vec<MetricStatistics>> {
111 let metrics = self.db.metrics_store.get_all_metrics().map_err(|e| {
113 Error::DatabaseError(format!(
114 "[get_metrics_statistics] Retrieving metrics failed: {e:?}"
115 ))
116 })?;
117
118 let metric_statistics =
120 metrics.iter().map(|metrics| MetricStatistics::new(metrics.clone())).collect();
121
122 Ok(metric_statistics)
123 }
124
125 pub async fn get_latest_metrics_statistics(&self) -> Result<MetricStatistics> {
128 match self.db.metrics_store.get_last().map_err(|e| {
130 Error::DatabaseError(format!(
131 "[get_metrics_statistics] Retrieving latest metrics failed: {e:?}"
132 ))
133 })? {
134 Some((_, metrics)) => Ok(MetricStatistics::new(metrics)),
136 None => Ok(MetricStatistics::default()),
138 }
139 }
140}