use smbcloud_gresiq_sdk::{Environment, GresiqClient, GresiqCredentials};
use super::events::{InferenceEvent, ModelLoadedEvent};
const EMBEDDED_API_KEY_DEV: Option<&str> = option_env!("GRESIQ_API_KEY_DEV");
const EMBEDDED_API_SECRET_DEV: Option<&str> = option_env!("GRESIQ_API_SECRET_DEV");
const EMBEDDED_API_KEY_PRODUCTION: Option<&str> = option_env!("GRESIQ_API_KEY_PRODUCTION");
const EMBEDDED_API_SECRET_SECRET_PRODUCTION: Option<&str> =
option_env!("GRESIQ_API_SECRET_PRODUCTION");
#[derive(Debug, Clone)]
pub struct PulseClient {
inner: GresiqClient,
edge_id: String,
}
impl PulseClient {
pub fn new(environment: Environment, edge_id: String) -> Option<Self> {
let (api_key, api_secret) = match environment {
Environment::Dev => (EMBEDDED_API_KEY_DEV?, EMBEDDED_API_SECRET_DEV?),
Environment::Production => (
EMBEDDED_API_KEY_PRODUCTION?,
EMBEDDED_API_SECRET_SECRET_PRODUCTION?,
),
};
let edge_id = if edge_id.is_empty() {
"onde-unknown".to_string()
} else {
edge_id
};
let credentials = GresiqCredentials {
api_key,
api_secret,
};
let inner = GresiqClient::from_credentials(environment, credentials);
Some(PulseClient { inner, edge_id })
}
pub fn record_model_loaded(&self, model_id: String, model_name: String, load_duration_ms: u64) {
let client = self.clone();
tokio::spawn(async move {
let event = ModelLoadedEvent {
edge_id: client.edge_id.clone(),
model_id,
model_name,
load_duration_ms,
};
if let Err(error) = client.inner.insert("pulse/model_loaded", &event).await {
log::warn!("pulse: model_loaded failed: {}", error);
}
});
}
pub fn record_inference(
&self,
model_id: String,
request_id: String,
duration_ms: u64,
status: String,
) {
let client = self.clone();
tokio::spawn(async move {
let event = InferenceEvent {
edge_id: client.edge_id.clone(),
model_id,
request_id,
duration_ms,
status,
};
if let Err(error) = client.inner.insert("pulse/inference_event", &event).await {
log::warn!("pulse: inference_event failed: {}", error);
}
});
}
}