Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(analytics): Revert api_event metrics and filters back to merchant_id authentication (#5821)

Sandeep Kumar283154d3f66a
diff --git a/crates/analytics/src/api_event/core.rs b/crates/analytics/src/api_event/core.rs
index f3a7b154b9..305de7e69c 100644
--- a/crates/analytics/src/api_event/core.rs
+++ b/crates/analytics/src/api_event/core.rs
@@ -21,7 +21,6 @@ use super::{
metrics::ApiEventMetricRow,
};
use crate::{
- enums::AuthInfo,
errors::{AnalyticsError, AnalyticsResult},
metrics,
types::FiltersError,
@@ -52,7 +51,7 @@ pub async fn api_events_core(
pub async fn get_filters(
pool: &AnalyticsProvider,
req: GetApiEventFiltersRequest,
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
) -> AnalyticsResult<ApiEventFiltersResponse> {
use api_models::analytics::{api_event::ApiEventDimensions, ApiEventFilterValue};
@@ -69,7 +68,8 @@ pub async fn get_filters(
AnalyticsProvider::Clickhouse(ckh_pool)
| AnalyticsProvider::CombinedSqlx(_, ckh_pool)
| AnalyticsProvider::CombinedCkh(_, ckh_pool) => {
- get_api_event_filter_for_dimension(dim, auth, &req.time_range, ckh_pool).await
+ get_api_event_filter_for_dimension(dim, merchant_id, &req.time_range, ckh_pool)
+ .await
}
}
.switch()?
@@ -92,7 +92,7 @@ pub async fn get_filters(
#[instrument(skip_all)]
pub async fn get_api_event_metrics(
pool: &AnalyticsProvider,
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
req: GetApiEventMetricRequest,
) -> AnalyticsResult<MetricsResponse<ApiMetricsBucketResponse>> {
let mut metrics_accumulator: HashMap<ApiEventMetricsBucketIdentifier, ApiEventMetricRow> =
@@ -109,14 +109,14 @@ pub async fn get_api_event_metrics(
// TODO: lifetime issues with joinset,
// can be optimized away if joinset lifetime requirements are relaxed
- let auth_scoped = auth.to_owned();
+ let merchant_id_scoped = merchant_id.to_owned();
set.spawn(
async move {
let data = pool
.get_api_event_metrics(
&metric_type,
&req.group_by_names.clone(),
- &auth_scoped,
+ &merchant_id_scoped,
&req.filters,
&req.time_series.map(|t| t.granularity),
&req.time_range,
diff --git a/crates/analytics/src/api_event/filters.rs b/crates/analytics/src/api_event/filters.rs
index 5c8136805c..62fd878901 100644
--- a/crates/analytics/src/api_event/filters.rs
+++ b/crates/analytics/src/api_event/filters.rs
@@ -4,7 +4,6 @@ use error_stack::ResultExt;
use time::PrimitiveDateTime;
use crate::{
- enums::AuthInfo,
query::{Aggregate, GroupByClause, QueryBuilder, QueryFilter, ToSql, Window},
types::{AnalyticsCollection, AnalyticsDataSource, FiltersError, FiltersResult, LoadRow},
};
@@ -13,7 +12,7 @@ pub trait ApiEventFilterAnalytics: LoadRow<ApiEventFilter> {}
pub async fn get_api_event_filter_for_dimension<T>(
dimension: ApiEventDimensions,
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
time_range: &TimeRange,
pool: &T,
) -> FiltersResult<Vec<ApiEventFilter>>
@@ -33,7 +32,9 @@ where
.attach_printable("Error filtering time range")
.switch()?;
- auth.set_filter_clause(&mut query_builder).switch()?;
+ query_builder
+ .add_filter_clause("merchant_id", merchant_id)
+ .switch()?;
query_builder.set_distinct();
diff --git a/crates/analytics/src/api_event/metrics.rs b/crates/analytics/src/api_event/metrics.rs
index 48e95b6b5c..ac29ec1516 100644
--- a/crates/analytics/src/api_event/metrics.rs
+++ b/crates/analytics/src/api_event/metrics.rs
@@ -7,7 +7,6 @@ use api_models::analytics::{
use time::PrimitiveDateTime;
use crate::{
- enums::AuthInfo,
query::{Aggregate, GroupByClause, ToSql, Window},
types::{AnalyticsCollection, AnalyticsDataSource, LoadRow, MetricsResult},
};
@@ -44,7 +43,7 @@ where
async fn load_metrics(
&self,
dimensions: &[ApiEventDimensions],
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
filters: &ApiEventFilters,
granularity: &Option<Granularity>,
time_range: &TimeRange,
@@ -65,7 +64,7 @@ where
async fn load_metrics(
&self,
dimensions: &[ApiEventDimensions],
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
filters: &ApiEventFilters,
granularity: &Option<Granularity>,
time_range: &TimeRange,
@@ -74,17 +73,38 @@ where
match self {
Self::Latency => {
MaxLatency
- .load_metrics(dimensions, auth, filters, granularity, time_range, pool)
+ .load_metrics(
+ dimensions,
+ merchant_id,
+ filters,
+ granularity,
+ time_range,
+ pool,
+ )
.await
}
Self::ApiCount => {
ApiCount
- .load_metrics(dimensions, auth, filters, granularity, time_range, pool)
+ .load_metrics(
+ dimensions,
+ merchant_id,
+ filters,
+ granularity,
+ time_range,
+ pool,
+ )
.await
}
Self::StatusCodeCount => {
StatusCodeCount
- .load_metrics(dimensions, auth, filters, granularity, time_range, pool)
+ .load_metrics(
+ dimensions,
+ merchant_id,
+ filters,
+ granularity,
+ time_range,
+ pool,
+ )
.await
}
}
diff --git a/crates/analytics/src/api_event/metrics/api_count.rs b/crates/analytics/src/api_event/metrics/api_count.rs
index 3973870a45..f00c01bbf3 100644
--- a/crates/analytics/src/api_event/metrics/api_count.rs
+++ b/crates/analytics/src/api_event/metrics/api_count.rs
@@ -10,7 +10,6 @@ use time::PrimitiveDateTime;
use super::ApiEventMetricRow;
use crate::{
- enums::AuthInfo,
query::{Aggregate, GroupByClause, QueryBuilder, QueryFilter, SeriesBucket, ToSql, Window},
types::{AnalyticsCollection, AnalyticsDataSource, MetricsError, MetricsResult},
};
@@ -31,7 +30,7 @@ where
async fn load_metrics(
&self,
_dimensions: &[ApiEventDimensions],
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
filters: &ApiEventFilters,
granularity: &Option<Granularity>,
time_range: &TimeRange,
@@ -70,7 +69,9 @@ where
.switch()?;
}
- auth.set_filter_clause(&mut query_builder).switch()?;
+ query_builder
+ .add_filter_clause("merchant_id", merchant_id)
+ .switch()?;
time_range
.set_filter_clause(&mut query_builder)
diff --git a/crates/analytics/src/api_event/metrics/latency.rs b/crates/analytics/src/api_event/metrics/latency.rs
index c9a62abe67..5d71da2a0a 100644
--- a/crates/analytics/src/api_event/metrics/latency.rs
+++ b/crates/analytics/src/api_event/metrics/latency.rs
@@ -10,7 +10,6 @@ use time::PrimitiveDateTime;
use super::ApiEventMetricRow;
use crate::{
- enums::AuthInfo,
query::{
Aggregate, FilterTypes, GroupByClause, QueryBuilder, QueryFilter, SeriesBucket, ToSql,
Window,
@@ -34,7 +33,7 @@ where
async fn load_metrics(
&self,
_dimensions: &[ApiEventDimensions],
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
filters: &ApiEventFilters,
granularity: &Option<Granularity>,
time_range: &TimeRange,
@@ -77,7 +76,9 @@ where
filters.set_filter_clause(&mut query_builder).switch()?;
- auth.set_filter_clause(&mut query_builder).switch()?;
+ query_builder
+ .add_filter_clause("merchant_id", merchant_id)
+ .switch()?;
time_range
.set_filter_clause(&mut query_builder)
diff --git a/crates/analytics/src/api_event/metrics/status_code_count.rs b/crates/analytics/src/api_event/metrics/status_code_count.rs
index 190f38999c..b4fff367b6 100644
--- a/crates/analytics/src/api_event/metrics/status_code_count.rs
+++ b/crates/analytics/src/api_event/metrics/status_code_count.rs
@@ -10,7 +10,6 @@ use time::PrimitiveDateTime;
use super::ApiEventMetricRow;
use crate::{
- enums::AuthInfo,
query::{Aggregate, GroupByClause, QueryBuilder, QueryFilter, SeriesBucket, ToSql, Window},
types::{AnalyticsCollection, AnalyticsDataSource, MetricsError, MetricsResult},
};
@@ -31,7 +30,7 @@ where
async fn load_metrics(
&self,
_dimensions: &[ApiEventDimensions],
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
filters: &ApiEventFilters,
granularity: &Option<Granularity>,
time_range: &TimeRange,
@@ -48,7 +47,9 @@ where
filters.set_filter_clause(&mut query_builder).switch()?;
- auth.set_filter_clause(&mut query_builder).switch()?;
+ query_builder
+ .add_filter_clause("merchant_id", merchant_id)
+ .switch()?;
time_range
.set_filter_clause(&mut query_builder)
diff --git a/crates/analytics/src/lib.rs b/crates/analytics/src/lib.rs
index 66005a61b7..d5cb3c718d 100644
--- a/crates/analytics/src/lib.rs
+++ b/crates/analytics/src/lib.rs
@@ -828,7 +828,7 @@ impl AnalyticsProvider {
&self,
metric: &ApiEventMetrics,
dimensions: &[ApiEventDimensions],
- auth: &AuthInfo,
+ merchant_id: &common_utils::id_type::MerchantId,
filters: &ApiEventFilters,
granularity: &Option<Granularity>,
time_range: &TimeRange,
@@ -840,7 +840,14 @@ impl AnalyticsProvider {
| Self::CombinedSqlx(_, ckh_pool) => {
// Since API events are ckh only use ckh here
metric
- .load_metrics(dimensions, auth, filters, granularity, time_range, ckh_pool)
+ .load_metrics(
+ dimensions,
+ merchant_id,
+ filters,
+ granularity,
+ time_range,
+ ckh_pool,
+ )
.await
}
}
diff --git a/crates/router/src/analytics.rs b/crates/router/src/analytics.rs
index 7c4d1acc6b..879db9a5a6 100644
--- a/crates/router/src/analytics.rs
+++ b/crates/router/src/analytics.rs
@@ -215,14 +215,6 @@ pub mod routes {
web::resource("filters/disputes")
.route(web::post().to(get_org_dispute_filters)),
)
- .service(
- web::resource("filters/api_events")
- .route(web::post().to(get_org_api_event_filters)),
- )
- .service(
- web::resource("metrics/api_events")
- .route(web::post().to(get_org_api_events_metrics)),
- )
.service(
web::resource("report/dispute")
.route(web::post().to(generate_org_dispute_report)),
@@ -257,14 +249,6 @@ pub mod routes {
web::resource("filters/refunds")
.route(web::post().to(get_profile_refund_filters)),
)
- .service(
- web::resource("metrics/api_events")
- .route(web::post().to(get_profile_api_events_metrics)),
- )
- .service(
- web::resource("filters/api_events")
- .route(web::post().to(get_profile_api_event_filters)),
- )
.service(
web::resource("metrics/disputes")
.route(web::post().to(get_profile_dispute_metrics)),
@@ -1757,100 +1741,13 @@ pub mod routes {
&req,
payload,
|state, auth: AuthenticationData, req, _| async move {
- let org_id = auth.merchant_account.get_org_id();
- let merchant_id = auth.merchant_account.get_id();
- let auth: AuthInfo = AuthInfo::MerchantLevel {
- org_id: org_id.clone(),
- merchant_ids: vec![merchant_id.clone()],
- };
- analytics::api_event::get_api_event_metrics(&state.pool, &auth, req)
- .await
- .map(ApplicationResponse::Json)
- },
- &auth::JWTAuth {
- permission: Permission::Analytics,
- minimum_entity_level: EntityType::Merchant,
- },
- api_locking::LockAction::NotApplicable,
- ))
- .await
- }
-
- /// # Panics
- ///
- /// Panics if `json_payload` array does not contain one `GetApiEventMetricRequest` element.
- pub async fn get_org_api_events_metrics(
- state: web::Data<AppState>,
- req: actix_web::HttpRequest,
- json_payload: web::Json<[GetApiEventMetricRequest; 1]>,
- ) -> impl Responder {
- // safety: This shouldn't panic owing to the data type
- #[allow(clippy::expect_used)]
- let payload = json_payload
- .into_inner()
- .to_vec()
- .pop()
- .expect("Couldn't get GetApiEventMetricRequest");
- let flow = AnalyticsFlow::GetApiEventMetrics;
- Box::pin(api::server_wrap(
- flow,
- state.clone(),
- &req,
- payload,
- |state, auth: AuthenticationData, req, _| async move {
- let org_id = auth.merchant_account.get_org_id();
- let auth: AuthInfo = AuthInfo::OrgLevel {
- org_id: org_id.clone(),
- };
- analytics::api_event::get_api_event_metrics(&state.pool, &auth, req)
- .await
- .map(ApplicationResponse::Json)
- },
- &auth::JWTAuth {
- permission: Permission::Analytics,
- minimum_entity_level: EntityType::Merchant,
- },
- api_locking::LockAction::NotApplicable,
- ))
- .await
- }
-
- /// # Panics
- ///
- /// Panics if `json_payload` array does not contain one `GetApiEventMetricRequest` element.
- pub async fn get_profile_api_events_metrics(
- state: web::Data<AppState>,
- req: actix_web::HttpRequest,
- json_payload: web::Json<[GetApiEventMetricRequest; 1]>,
- ) -> impl Responder {
- // safety: This shouldn't panic owing to the data type
- #[allow(clippy::expect_used)]
- let payload = json_payload
- .into_inner()
- .to_vec()
- .pop()
- .expect("Couldn't get GetApiEventMetricRequest");
- let flow = AnalyticsFlow::GetApiEventMetrics;
- Box::pin(api::server_wrap(
- flow,
- state.clone(),
- &req,
- payload,
- |state, auth: AuthenticationData, req, _| async move {
- let org_id = auth.merchant_account.get_org_id();
- let merchant_id = auth.merchant_account.get_id();
- let profile_id = auth
- .profile_id
- .ok_or(report!(UserErrors::JwtProfileIdMissing))
- .change_context(AnalyticsError::AccessForbiddenError)?;
- let auth: AuthInfo = AuthInfo::ProfileLevel {
- org_id: org_id.clone(),
- merchant_id: merchant_id.clone(),
- profile_ids: vec![profile_id.clone()],
- };
- analytics::api_event::get_api_event_metrics(&state.pool, &auth, req)
- .await
- .map(ApplicationResponse::Json)
+ analytics::api_event::get_api_event_metrics(
+ &state.pool,
+ auth.merchant_account.get_id(),
+ req,
+ )
+ .await
+ .map(ApplicationResponse::Json)
},
&auth::JWTAuth {
permission: Permission::Analytics,
@@ -1873,78 +1770,7 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, auth: AuthenticationData, req, _| async move {
- let org_id = auth.merchant_account.get_org_id();
- let merchant_id = auth.merchant_account.get_id();
- let auth: AuthInfo = AuthInfo::MerchantLevel {
- org_id: org_id.clone(),
- merchant_ids: vec![merchant_id.clone()],
- };
- analytics::api_event::get_filters(&state.pool, req, &auth)
- .await
- .map(ApplicationResponse::Json)
- },
- &auth::JWTAuth {
- permission: Permission::Analytics,
- minimum_entity_level: EntityType::Merchant,
- },
- api_locking::LockAction::NotApplicable,
- ))
- .await
- }
-
- pub async fn get_org_api_event_filters(
- state: web::Data<AppState>,
- req: actix_web::HttpRequest,
- json_payload: web::Json<GetApiEventFiltersRequest>,
- ) -> impl Responder {
- let flow = AnalyticsFlow::GetApiEventFilters;
- Box::pin(api::server_wrap(
- flow,
- state.clone(),
- &req,
- json_payload.into_inner(),
- |state, auth: AuthenticationData, req, _| async move {
- let org_id = auth.merchant_account.get_org_id();
- let auth: AuthInfo = AuthInfo::OrgLevel {
- org_id: org_id.clone(),
- };
- analytics::api_event::get_filters(&state.pool, req, &auth)
- .await
- .map(ApplicationResponse::Json)
- },
- &auth::JWTAuth {
- permission: Permission::Analytics,
- minimum_entity_level: EntityType::Merchant,
- },
- api_locking::LockAction::NotApplicable,
- ))
- .await
- }
-
- pub async fn get_profile_api_event_filters(
- state: web::Data<AppState>,
- req: actix_web::HttpRequest,
- json_payload: web::Json<GetApiEventFiltersRequest>,
- ) -> impl Responder {
- let flow = AnalyticsFlow::GetApiEventFilters;
- Box::pin(api::server_wrap(
- flow,
- state.clone(),
- &req,
- json_payload.into_inner(),
- |state, auth: AuthenticationData, req, _| async move {
- let org_id = auth.merchant_account.get_org_id();
- let merchant_id = auth.merchant_account.get_id();
- let profile_id = auth
- .profile_id
- .ok_or(report!(UserErrors::JwtProfileIdMissing))
- .change_context(AnalyticsError::AccessForbiddenError)?;
- let auth: AuthInfo = AuthInfo::ProfileLevel {
- org_id: org_id.clone(),
- merchant_id: merchant_id.clone(),
- profile_ids: vec![profile_id.clone()],
- };
- analytics::api_event::get_filters(&state.pool, req, &auth)
+ analytics::api_event::get_filters(&state.pool, req, auth.merchant_account.get_id())
.await
.map(ApplicationResponse::Json)
},