Skip to content

Commit 38333b2

Browse files
committed
fix: add segment by channel
1 parent b0947b7 commit 38333b2

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

primitives/src/analytics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod postgres {
2525
}
2626

2727
#[derive(Debug, Deserialize)]
28+
#[serde(rename_all = "camelCase")]
2829
pub struct AnalyticsQuery {
2930
#[serde(default = "default_limit")]
3031
pub limit: u32,
@@ -34,6 +35,7 @@ pub struct AnalyticsQuery {
3435
pub metric: String,
3536
#[serde(default = "default_timeframe")]
3637
pub timeframe: String,
38+
pub segment_by_channel: Option<String>
3739
}
3840

3941
impl AnalyticsQuery {

sentry/src/db/analytics.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub async fn get_analytics(
4848
query: AnalyticsQuery,
4949
pool: &DbPool,
5050
analytics_type: AnalyticsType,
51+
segment_by_channel: bool,
5152
) -> Result<Vec<AnalyticsResponse>, RunError<bb8_postgres::tokio_postgres::Error>> {
5253
let applied_limit = query.limit.min(ANALYTICS_QUERY_LIMIT);
5354
let (interval, period) = get_time_frame(&query.timeframe);
@@ -103,11 +104,18 @@ pub async fn get_analytics(
103104
}
104105
};
105106

107+
let group = if segment_by_channel {
108+
"time, channelId"
109+
} else {
110+
"time"
111+
};
112+
106113
let sql_query = format!(
107-
"{} WHERE {} GROUP BY time LIMIT {}",
114+
"{} WHERE {} GROUP BY {} LIMIT {}",
108115
select_query,
109116
where_clauses.join(" AND "),
110-
applied_limit
117+
group,
118+
applied_limit,
111119
);
112120

113121
// execute query

sentry/src/routes/analytics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ pub async fn process_analytics<A: Adapter>(
9393
.is_valid()
9494
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
9595

96-
let result = get_analytics(query, &app.pool, analytics_type).await?;
96+
let segment_channel = query.segment_by_channel.clone().map(|_| true).unwrap_or_else(|| false);
97+
98+
let result = get_analytics(query, &app.pool, analytics_type, segment_channel).await?;
9799

98100
serde_json::to_string(&result)
99101
.map_err(|_| ResponseError::BadRequest("error occurred; try again later".to_string()))

0 commit comments

Comments
 (0)