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

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

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/analytics/src/api_event/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use super::{
metrics::ApiEventMetricRow,
};
use crate::{
enums::AuthInfo,
errors::{AnalyticsError, AnalyticsResult},
metrics,
types::FiltersError,
Expand Down Expand Up @@ -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};

Expand All @@ -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()?
Expand All @@ -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> =
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions crates/analytics/src/api_event/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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>>
Expand All @@ -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();

Expand Down
32 changes: 26 additions & 6 deletions crates/analytics/src/api_event/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/analytics/src/api_event/metrics/api_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions crates/analytics/src/api_event/metrics/latency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use time::PrimitiveDateTime;

use super::ApiEventMetricRow;
use crate::{
enums::AuthInfo,
query::{
Aggregate, FilterTypes, GroupByClause, QueryBuilder, QueryFilter, SeriesBucket, ToSql,
Window,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions crates/analytics/src/api_event/metrics/status_code_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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,
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions crates/analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
Expand Down
Loading
Loading