feat: with monitoring

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-27 00:07:56 +02:00
parent 569f5272e6
commit e0545c726c
8 changed files with 145 additions and 11 deletions

View File

@@ -0,0 +1,78 @@
use std::collections::HashMap;
use std::sync::Arc;
use axum::async_trait;
use churn_domain::ServerEnrollReq;
use serde::{ser::SerializeStruct, Deserialize, Serialize};
use tokio::sync::{Mutex, RwLock};
#[derive(Clone)]
pub struct EventService(Arc<dyn EventServiceTrait + Send + Sync + 'static>);
impl std::ops::Deref for EventService {
type Target = Arc<dyn EventServiceTrait + Send + Sync + 'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Default for EventService {
fn default() -> Self {
Self(Arc::new(DefaultEventService::default()))
}
}
#[derive(Default)]
struct DefaultEventService {
agents: Arc<RwLock<Vec<LogEvent>>>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LogEvent {
pub id: uuid::Uuid,
pub author: String,
pub content: String,
}
impl LogEvent {
pub fn new(author: impl Into<String>, content: impl Into<String>) -> Self {
Self {
id: uuid::Uuid::new_v4(),
author: author.into(),
content: content.into(),
}
}
}
#[async_trait]
pub trait EventServiceTrait {
async fn append(&self, req: LogEvent) -> anyhow::Result<()>;
async fn get_from_cursor(&self, cursor: uuid::Uuid) -> anyhow::Result<Vec<LogEvent>>;
async fn get_from_beginning(&self) -> anyhow::Result<Vec<LogEvent>>;
}
#[async_trait]
impl EventServiceTrait for DefaultEventService {
async fn append(&self, req: LogEvent) -> anyhow::Result<()> {
let mut events = self.agents.write().await;
events.push(req);
Ok(())
}
async fn get_from_cursor(&self, cursor: uuid::Uuid) -> anyhow::Result<Vec<LogEvent>> {
let events = self.agents.read().await;
let items = events
.iter()
.skip_while(|item| item.id != cursor)
.skip(1)
.cloned()
.collect::<Vec<_>>();
Ok(items)
}
async fn get_from_beginning(&self) -> anyhow::Result<Vec<LogEvent>> {
let events = self.agents.read().await;
Ok(events.iter().cloned().collect())
}
}