use serde::Serialize;
use crate::catalog::Platform;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum EventLogLevel {
Info,
Warn,
Error,
Debug,
Mark,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum AmagiEventType {
LogInfo,
LogWarn,
LogError,
LogDebug,
LogMark,
HttpRequest,
HttpResponse,
HttpError,
NetworkRetry,
NetworkError,
ApiSuccess,
ApiError,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LogEventData {
pub level: EventLogLevel,
pub message: String,
pub args: Vec<String>,
pub timestamp_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct HttpRequestEventData {
pub method: String,
pub url: String,
pub headers: Vec<(String, String)>,
pub timestamp_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct HttpResponseEventData {
pub method: String,
pub url: String,
pub status_code: u16,
pub response_time_ms: u64,
pub client_ip: Option<String>,
pub request_size: Option<String>,
pub response_size: Option<String>,
pub timestamp_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct NetworkRetryEventData {
pub error_code: String,
pub attempt: u32,
pub max_retries: u32,
pub delay_ms: u64,
pub url: Option<String>,
pub timestamp_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct NetworkErrorEventData {
pub error_code: String,
pub message: String,
pub retries: u32,
pub url: Option<String>,
pub timestamp_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ApiSuccessEventData {
pub platform: Platform,
pub method_key: String,
pub status_code: u16,
pub duration_ms: u64,
pub timestamp_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ApiErrorEventData {
pub platform: Platform,
pub method_key: String,
pub error_code: Option<String>,
pub error_message: String,
pub url: Option<String>,
pub duration_ms: Option<u64>,
pub timestamp_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "event", content = "data", rename_all = "kebab-case")]
pub enum AmagiEvent {
LogInfo(LogEventData),
LogWarn(LogEventData),
LogError(LogEventData),
LogDebug(LogEventData),
LogMark(LogEventData),
HttpRequest(HttpRequestEventData),
HttpResponse(HttpResponseEventData),
HttpError(NetworkErrorEventData),
NetworkRetry(NetworkRetryEventData),
NetworkError(NetworkErrorEventData),
ApiSuccess(ApiSuccessEventData),
ApiError(ApiErrorEventData),
}
impl AmagiEvent {
pub const fn event_type(&self) -> AmagiEventType {
match self {
Self::LogInfo(_) => AmagiEventType::LogInfo,
Self::LogWarn(_) => AmagiEventType::LogWarn,
Self::LogError(_) => AmagiEventType::LogError,
Self::LogDebug(_) => AmagiEventType::LogDebug,
Self::LogMark(_) => AmagiEventType::LogMark,
Self::HttpRequest(_) => AmagiEventType::HttpRequest,
Self::HttpResponse(_) => AmagiEventType::HttpResponse,
Self::HttpError(_) => AmagiEventType::HttpError,
Self::NetworkRetry(_) => AmagiEventType::NetworkRetry,
Self::NetworkError(_) => AmagiEventType::NetworkError,
Self::ApiSuccess(_) => AmagiEventType::ApiSuccess,
Self::ApiError(_) => AmagiEventType::ApiError,
}
}
}