Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(analytics): Adds tenant ID to Analytics Provider #4594

Closed
wants to merge 13 commits into from
3 changes: 2 additions & 1 deletion crates/analytics/src/clickhouse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use actix_web::http::StatusCode;
use common_utils::errors::ParsingError;
use common_utils::{errors::ParsingError, types::TenantID};
use error_stack::{report, Report, ResultExt};
use router_env::logger;
use time::PrimitiveDateTime;
Expand Down Expand Up @@ -35,6 +35,7 @@ pub type ClickhouseResult<T> = error_stack::Result<T, ClickhouseError>;
#[derive(Clone, Debug)]
pub struct ClickhouseClient {
pub config: Arc<ClickhouseConfig>,
pub tenant_id: TenantID,
}

#[derive(Clone, Debug, serde::Deserialize)]
Expand Down
15 changes: 10 additions & 5 deletions crates/analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod search;
mod sqlx;
mod types;
use api_event::metrics::{ApiEventMetric, ApiEventMetricRow};
use common_utils::errors::CustomResult;
use common_utils::{errors::CustomResult, types::TenantID};
use disputes::metrics::{DisputeMetric, DisputeMetricRow};
use hyperswitch_interfaces::secrets_interface::{
secret_handler::SecretsHandler,
Expand Down Expand Up @@ -601,22 +601,27 @@ impl AnalyticsProvider {
}
}

pub async fn from_conf(config: &AnalyticsConfig) -> Self {
pub async fn from_conf(config: &AnalyticsConfig, tenant_id: TenantID) -> Self {
match config {
AnalyticsConfig::Sqlx { sqlx } => Self::Sqlx(SqlxClient::from_conf(sqlx).await),
AnalyticsConfig::Sqlx { sqlx } => {
Self::Sqlx(SqlxClient::from_conf(sqlx, tenant_id).await)
}
AnalyticsConfig::Clickhouse { clickhouse } => Self::Clickhouse(ClickhouseClient {
config: Arc::new(clickhouse.clone()),
tenant_id,
}),
AnalyticsConfig::CombinedCkh { sqlx, clickhouse } => Self::CombinedCkh(
SqlxClient::from_conf(sqlx).await,
SqlxClient::from_conf(sqlx, tenant_id.clone()).await,
ClickhouseClient {
config: Arc::new(clickhouse.clone()),
tenant_id,
},
),
AnalyticsConfig::CombinedSqlx { sqlx, clickhouse } => Self::CombinedSqlx(
SqlxClient::from_conf(sqlx).await,
SqlxClient::from_conf(sqlx, tenant_id.clone()).await,
ClickhouseClient {
config: Arc::new(clickhouse.clone()),
tenant_id,
},
),
}
Expand Down
12 changes: 9 additions & 3 deletions crates/analytics/src/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use api_models::{
analytics::refunds::RefundType,
enums::{DisputeStage, DisputeStatus},
};
use common_utils::errors::{CustomResult, ParsingError};
use common_utils::{
errors::{CustomResult, ParsingError},
types::TenantID,
};
use diesel_models::enums::{
AttemptStatus, AuthenticationType, Currency, PaymentMethod, RefundStatus,
};
Expand All @@ -31,6 +34,8 @@ use super::{
#[derive(Debug, Clone)]
pub struct SqlxClient {
pool: Pool<Postgres>,
#[allow(unused)]
tenant_id: TenantID,
}

impl Default for SqlxClient {
Expand All @@ -44,12 +49,13 @@ impl Default for SqlxClient {
pool: PgPoolOptions::new()
.connect_lazy(&database_url)
.expect("SQLX Pool Creation failed"),
tenant_id: TenantID::default(),
}
}
}

impl SqlxClient {
pub async fn from_conf(conf: &Database) -> Self {
pub async fn from_conf(conf: &Database, tenant_id: TenantID) -> Self {
let password = &conf.password.peek();
let database_url = format!(
"postgres://{}:{}@{}:{}/{}",
Expand All @@ -61,7 +67,7 @@ impl SqlxClient {
.acquire_timeout(std::time::Duration::from_secs(conf.connection_timeout))
.connect_lazy(&database_url)
.expect("SQLX Pool Creation failed");
Self { pool }
Self { pool, tenant_id }
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/common_utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,13 @@
<serde_json::Value as ToSql<Jsonb, diesel::pg::Pg>>::to_sql(&value, &mut out.reborrow())
}
}

/// This struct represnts the Tenant Id and stores it in a String

Check warning on line 216 in crates/common_utils/src/types.rs

View workflow job for this annotation

GitHub Actions / Spell check

"represnts" should be "represents".
#[derive(Clone, Debug)]
pub struct TenantID(#[allow(unused)] String);

impl Default for TenantID {
fn default() -> Self {
Self(String::from("default"))
}
}
8 changes: 6 additions & 2 deletions crates/router/src/routes/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use actix_web::{web, Scope};
use analytics::TenantID;
#[cfg(all(feature = "business_profile_routing", feature = "olap"))]
use api_models::routing::RoutingRetrieveQuery;
#[cfg(feature = "olap")]
Expand Down Expand Up @@ -218,8 +219,11 @@ impl AppState {
};

#[cfg(feature = "olap")]
let pool =
crate::analytics::AnalyticsProvider::from_conf(conf.analytics.get_inner()).await;
let pool = crate::analytics::AnalyticsProvider::from_conf(
conf.analytics.get_inner(),
TenantID::default(),
)
.await;

#[cfg(feature = "email")]
let email_client = Arc::new(create_email_client(&conf).await);
Expand Down