feat(analytics): Add Clickhouse based analytics (#2988)

Co-authored-by: harsh_sharma_juspay <harsh.sharma@juspay.in>
Co-authored-by: Ivor Dsouza <ivor.dsouza@juspay.in>
Co-authored-by: Chethan Rao <70657455+Chethan-rao@users.noreply.github.com>
Co-authored-by: nain-F49FF806 <126972030+nain-F49FF806@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: akshay.s <akshay.s@juspay.in>
Co-authored-by: Gnanasundari24 <118818938+Gnanasundari24@users.noreply.github.com>
This commit is contained in:
Sampras Lopes
2023-11-29 17:04:53 +05:30
committed by GitHub
parent 2e57745352
commit 9df4e0193f
135 changed files with 12145 additions and 901 deletions

View File

@ -0,0 +1,108 @@
use api_models::enums as api_enums;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "flow_type")]
pub enum ApiEventsType {
Payment {
payment_id: String,
},
Refund {
payment_id: String,
refund_id: String,
},
Default,
PaymentMethod {
payment_method_id: String,
payment_method: Option<api_enums::PaymentMethod>,
payment_method_type: Option<api_enums::PaymentMethodType>,
},
Customer {
customer_id: String,
},
User {
//specified merchant_id will overridden on global defined
merchant_id: String,
user_id: String,
},
Webhooks {
connector: String,
payment_id: Option<String>,
},
OutgoingEvent,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ApiEvents {
pub api_name: String,
pub request_id: Option<String>,
//It is require to solve ambiquity in case of event_type is User
#[serde(skip_serializing_if = "Option::is_none")]
pub merchant_id: Option<String>,
pub request: String,
pub response: String,
pub status_code: u16,
#[serde(with = "time::serde::timestamp")]
pub created_at: OffsetDateTime,
pub latency: u128,
//conflicting fields underlying enums will be used
#[serde(flatten)]
pub event_type: ApiEventsType,
pub user_agent: Option<String>,
pub ip_addr: Option<String>,
pub url_path: Option<String>,
pub api_event_type: Option<ApiCallEventType>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum ApiCallEventType {
IncomingApiEvent,
OutgoingApiEvent,
}
impl super::KafkaMessage for ApiEvents {
fn key(&self) -> String {
match &self.event_type {
ApiEventsType::Payment { payment_id } => format!(
"{}_{}",
self.merchant_id
.as_ref()
.unwrap_or(&"default_merchant_id".to_string()),
payment_id
),
ApiEventsType::Refund {
payment_id,
refund_id,
} => format!("{payment_id}_{refund_id}"),
ApiEventsType::Default => "key".to_string(),
ApiEventsType::PaymentMethod {
payment_method_id,
payment_method,
payment_method_type,
} => format!(
"{:?}_{:?}_{:?}",
payment_method_id.clone(),
payment_method.clone(),
payment_method_type.clone(),
),
ApiEventsType::Customer { customer_id } => customer_id.to_string(),
ApiEventsType::User {
merchant_id,
user_id,
} => format!("{}_{}", merchant_id, user_id),
ApiEventsType::Webhooks {
connector,
payment_id,
} => format!(
"webhook_{}_{connector}",
payment_id.clone().unwrap_or_default()
),
ApiEventsType::OutgoingEvent => "outgoing_event".to_string(),
}
}
fn creation_timestamp(&self) -> Option<i64> {
Some(self.created_at.unix_timestamp())
}
}