use crate::common::U256;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter, Result as FmtResult};
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct QuotingMetrics {
pub data_type: u32,
pub data_size: usize,
pub close_records_stored: usize,
pub records_per_type: Vec<(u32, u32)>,
pub max_records: usize,
pub received_payment_count: usize,
pub live_time: u64,
pub network_density: Option<[u8; 32]>,
pub network_size: Option<u64>,
}
impl Debug for QuotingMetrics {
fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
let density_u256 = self.network_density.map(U256::from_be_bytes);
write!(
formatter,
"QuotingMetrics {{ data_type: {}, data_size: {}, close_records_stored: {}, records_per_type {:?}, max_records: {}, received_payment_count: {}, live_time: {}, network_density: {density_u256:?}, network_size: {:?} }}",
self.data_type,
self.data_size,
self.close_records_stored,
self.records_per_type,
self.max_records,
self.received_payment_count,
self.live_time,
self.network_size
)
}
}
impl QuotingMetrics {
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend_from_slice(&self.data_type.to_le_bytes());
bytes.extend_from_slice(&(self.data_size as u64).to_le_bytes());
bytes.extend_from_slice(&(self.close_records_stored as u64).to_le_bytes());
bytes.extend_from_slice(&(self.records_per_type.len() as u32).to_le_bytes());
for (dtype, count) in &self.records_per_type {
bytes.extend_from_slice(&dtype.to_le_bytes());
bytes.extend_from_slice(&count.to_le_bytes());
}
bytes.extend_from_slice(&(self.max_records as u64).to_le_bytes());
bytes.extend_from_slice(&(self.received_payment_count as u64).to_le_bytes());
bytes.extend_from_slice(&self.live_time.to_le_bytes());
if let Some(density) = &self.network_density {
bytes.push(1); bytes.extend_from_slice(density);
} else {
bytes.push(0); }
if let Some(size) = self.network_size {
bytes.push(1); bytes.extend_from_slice(&size.to_le_bytes());
} else {
bytes.push(0); }
bytes
}
}