fix(analytics): fixed response code to 501 (#3119)

Co-authored-by: Sampras Lopes <lsampras@pm.me>
This commit is contained in:
Sagar naik
2024-01-05 18:58:29 +05:30
committed by GitHub
parent 64babd3478
commit 00008c16c1
6 changed files with 42 additions and 22 deletions

View File

@ -8,6 +8,7 @@ use api_models::analytics::{
AnalyticsMetadata, ApiEventFiltersResponse, GetApiEventFiltersRequest, AnalyticsMetadata, ApiEventFiltersResponse, GetApiEventFiltersRequest,
GetApiEventMetricRequest, MetricsResponse, GetApiEventMetricRequest, MetricsResponse,
}; };
use common_utils::errors::ReportSwitchExt;
use error_stack::{IntoReport, ResultExt}; use error_stack::{IntoReport, ResultExt};
use router_env::{ use router_env::{
instrument, logger, instrument, logger,
@ -32,16 +33,18 @@ pub async fn api_events_core(
merchant_id: String, merchant_id: String,
) -> AnalyticsResult<Vec<ApiLogsResult>> { ) -> AnalyticsResult<Vec<ApiLogsResult>> {
let data = match pool { let data = match pool {
AnalyticsProvider::Sqlx(_) => Err(FiltersError::NotImplemented) AnalyticsProvider::Sqlx(_) => Err(FiltersError::NotImplemented(
.into_report() "API Events not implemented for SQLX",
.attach_printable("SQL Analytics is not implemented for API Events"), ))
.into_report()
.attach_printable("SQL Analytics is not implemented for API Events"),
AnalyticsProvider::Clickhouse(pool) => get_api_event(&merchant_id, req, pool).await, AnalyticsProvider::Clickhouse(pool) => get_api_event(&merchant_id, req, pool).await,
AnalyticsProvider::CombinedSqlx(_sqlx_pool, ckh_pool) AnalyticsProvider::CombinedSqlx(_sqlx_pool, ckh_pool)
| AnalyticsProvider::CombinedCkh(_sqlx_pool, ckh_pool) => { | AnalyticsProvider::CombinedCkh(_sqlx_pool, ckh_pool) => {
get_api_event(&merchant_id, req, ckh_pool).await get_api_event(&merchant_id, req, ckh_pool).await
} }
} }
.change_context(AnalyticsError::UnknownError)?; .switch()?;
Ok(data) Ok(data)
} }
@ -58,9 +61,11 @@ pub async fn get_filters(
let mut res = ApiEventFiltersResponse::default(); let mut res = ApiEventFiltersResponse::default();
for dim in req.group_by_names { for dim in req.group_by_names {
let values = match pool { let values = match pool {
AnalyticsProvider::Sqlx(_pool) => Err(FiltersError::NotImplemented) AnalyticsProvider::Sqlx(_pool) => Err(FiltersError::NotImplemented(
.into_report() "API Events not implemented for SQLX",
.attach_printable("SQL Analytics is not implemented for API Events"), ))
.into_report()
.attach_printable("SQL Analytics is not implemented for API Events"),
AnalyticsProvider::Clickhouse(ckh_pool) AnalyticsProvider::Clickhouse(ckh_pool)
| AnalyticsProvider::CombinedSqlx(_, ckh_pool) | AnalyticsProvider::CombinedSqlx(_, ckh_pool)
| AnalyticsProvider::CombinedCkh(_, ckh_pool) => { | AnalyticsProvider::CombinedCkh(_, ckh_pool) => {
@ -68,7 +73,7 @@ pub async fn get_filters(
.await .await
} }
} }
.change_context(AnalyticsError::UnknownError)? .switch()?
.into_iter() .into_iter()
.filter_map(|fil: ApiEventFilter| match dim { .filter_map(|fil: ApiEventFilter| match dim {
ApiEventDimensions::StatusCode => fil.status_code.map(|i| i.to_string()), ApiEventDimensions::StatusCode => fil.status_code.map(|i| i.to_string()),

View File

@ -7,6 +7,7 @@ use api_models::analytics::{
AnalyticsMetadata, GetSdkEventFiltersRequest, GetSdkEventMetricRequest, MetricsResponse, AnalyticsMetadata, GetSdkEventFiltersRequest, GetSdkEventMetricRequest, MetricsResponse,
SdkEventFiltersResponse, SdkEventFiltersResponse,
}; };
use common_utils::errors::ReportSwitchExt;
use error_stack::{IntoReport, ResultExt}; use error_stack::{IntoReport, ResultExt};
use router_env::{instrument, logger, tracing}; use router_env::{instrument, logger, tracing};
@ -28,16 +29,18 @@ pub async fn sdk_events_core(
publishable_key: String, publishable_key: String,
) -> AnalyticsResult<Vec<SdkEventsResult>> { ) -> AnalyticsResult<Vec<SdkEventsResult>> {
match pool { match pool {
AnalyticsProvider::Sqlx(_) => Err(FiltersError::NotImplemented) AnalyticsProvider::Sqlx(_) => Err(FiltersError::NotImplemented(
.into_report() "SDK Events not implemented for SQLX",
.attach_printable("SQL Analytics is not implemented for Sdk Events"), ))
.into_report()
.attach_printable("SQL Analytics is not implemented for Sdk Events"),
AnalyticsProvider::Clickhouse(pool) => get_sdk_event(&publishable_key, req, pool).await, AnalyticsProvider::Clickhouse(pool) => get_sdk_event(&publishable_key, req, pool).await,
AnalyticsProvider::CombinedSqlx(_sqlx_pool, ckh_pool) AnalyticsProvider::CombinedSqlx(_sqlx_pool, ckh_pool)
| AnalyticsProvider::CombinedCkh(_sqlx_pool, ckh_pool) => { | AnalyticsProvider::CombinedCkh(_sqlx_pool, ckh_pool) => {
get_sdk_event(&publishable_key, req, ckh_pool).await get_sdk_event(&publishable_key, req, ckh_pool).await
} }
} }
.change_context(AnalyticsError::UnknownError) .switch()
} }
#[instrument(skip_all)] #[instrument(skip_all)]
@ -159,9 +162,11 @@ pub async fn get_filters(
if let Some(publishable_key) = publishable_key { if let Some(publishable_key) = publishable_key {
for dim in req.group_by_names { for dim in req.group_by_names {
let values = match pool { let values = match pool {
AnalyticsProvider::Sqlx(_pool) => Err(FiltersError::NotImplemented) AnalyticsProvider::Sqlx(_pool) => Err(FiltersError::NotImplemented(
.into_report() "SDK Events not implemented for SQLX",
.attach_printable("SQL Analytics is not implemented for SDK Events"), ))
.into_report()
.attach_printable("SQL Analytics is not implemented for SDK Events"),
AnalyticsProvider::Clickhouse(pool) => { AnalyticsProvider::Clickhouse(pool) => {
get_sdk_event_filter_for_dimension(dim, publishable_key, &req.time_range, pool) get_sdk_event_filter_for_dimension(dim, publishable_key, &req.time_range, pool)
.await .await

View File

@ -8,6 +8,7 @@ use common_utils::{
use error_stack::{report, Report, ResultExt}; use error_stack::{report, Report, ResultExt};
use super::query::QueryBuildingError; use super::query::QueryBuildingError;
use crate::errors::AnalyticsError;
#[derive(serde::Deserialize, Debug, serde::Serialize)] #[derive(serde::Deserialize, Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
@ -124,8 +125,8 @@ pub enum FiltersError {
#[error("Error running Query")] #[error("Error running Query")]
QueryExecutionFailure, QueryExecutionFailure,
#[allow(dead_code)] #[allow(dead_code)]
#[error("Not Implemented")] #[error("Not Implemented: {0}")]
NotImplemented, NotImplemented(&'static str),
} }
impl ErrorSwitch<FiltersError> for QueryBuildingError { impl ErrorSwitch<FiltersError> for QueryBuildingError {
@ -134,4 +135,13 @@ impl ErrorSwitch<FiltersError> for QueryBuildingError {
} }
} }
impl ErrorSwitch<AnalyticsError> for FiltersError {
fn switch(&self) -> AnalyticsError {
match self {
Self::QueryBuildingError | Self::QueryExecutionFailure => AnalyticsError::UnknownError,
Self::NotImplemented(a) => AnalyticsError::NotImplemented(a),
}
}
}
impl_misc_api_event_type!(AnalyticsDomain); impl_misc_api_event_type!(AnalyticsDomain);

View File

@ -938,7 +938,7 @@ pub async fn webhooks_wrapper<W: types::OutgoingWebhookType, Ctx: PaymentMethodR
None, None,
api_event, api_event,
req, req,
Some(req.method().to_string()), req.method(),
); );
match api_event.clone().try_into() { match api_event.clone().try_into() {
Ok(event) => { Ok(event) => {

View File

@ -41,7 +41,7 @@ pub struct ApiEvent {
#[serde(flatten)] #[serde(flatten)]
event_type: ApiEventsType, event_type: ApiEventsType,
hs_latency: Option<u128>, hs_latency: Option<u128>,
http_method: Option<String>, http_method: String,
} }
impl ApiEvent { impl ApiEvent {
@ -59,7 +59,7 @@ impl ApiEvent {
error: Option<serde_json::Value>, error: Option<serde_json::Value>,
event_type: ApiEventsType, event_type: ApiEventsType,
http_req: &HttpRequest, http_req: &HttpRequest,
http_method: Option<String>, http_method: &http::Method,
) -> Self { ) -> Self {
Self { Self {
merchant_id, merchant_id,
@ -83,7 +83,7 @@ impl ApiEvent {
url_path: http_req.path().to_string(), url_path: http_req.path().to_string(),
event_type, event_type,
hs_latency, hs_latency,
http_method, http_method: http_method.to_string(),
} }
} }
} }

View File

@ -938,7 +938,7 @@ where
error, error,
event_type.unwrap_or(ApiEventsType::Miscellaneous), event_type.unwrap_or(ApiEventsType::Miscellaneous),
request, request,
Some(request.method().to_string()), request.method(),
); );
match api_event.clone().try_into() { match api_event.clone().try_into() {
Ok(event) => { Ok(event) => {