ih_muse_core/buffer/
metric_buffer.rsuse std::collections::HashMap;
use tokio::sync::Mutex;
use ih_muse_proto::{LocalElementId, MetricValue};
pub struct MetricBuffer {
buffer: Mutex<HashMap<LocalElementId, HashMap<String, MetricValue>>>,
}
impl Default for MetricBuffer {
fn default() -> Self {
Self::new()
}
}
impl MetricBuffer {
pub fn new() -> Self {
Self {
buffer: Mutex::new(HashMap::new()),
}
}
pub async fn add_metric(
&self,
local_elem_id: LocalElementId,
metric_code: String,
value: MetricValue,
) {
let mut buffer = self.buffer.lock().await;
buffer
.entry(local_elem_id)
.or_insert_with(HashMap::new)
.insert(metric_code, value);
}
pub async fn get_and_clear(&self) -> HashMap<LocalElementId, HashMap<String, MetricValue>> {
let mut buffer = self.buffer.lock().await;
let data = buffer.clone();
buffer.clear();
data
}
}