Skip to content

Commit 99fe7df

Browse files
committed
fix: analytics query
1 parent 14311ea commit 99fe7df

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

sentry/src/db/analytics.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use bb8::RunError;
55
use chrono::Utc;
66
use primitives::analytics::{AnalyticsData, AnalyticsQuery, ANALYTICS_QUERY_LIMIT};
77
use primitives::sentry::{AdvancedAnalyticsResponse, ChannelReport, PublisherReport};
8+
use bb8_postgres::tokio_postgres::types::{ToSql};
89
use primitives::{ChannelId, ValidatorId};
910
use redis;
1011
use redis::aio::MultiplexedConnection;
@@ -40,18 +41,29 @@ pub async fn advertiser_channel_ids(
4041
}
4142

4243
pub async fn get_analytics(
43-
query: AnalyticsQuery,
44+
mut query: AnalyticsQuery,
4445
pool: &DbPool,
4546
analytics_type: AnalyticsType,
4647
segment_by_channel: bool,
4748
channel_id: Option<&ChannelId>,
4849
) -> Result<Vec<AnalyticsData>, RunError<bb8_postgres::tokio_postgres::Error>> {
50+
// converts metric to column
51+
query.metric_to_column();
52+
53+
let mut params = Vec::<&(dyn ToSql + Sync)>::new();
4954
let applied_limit = query.limit.min(ANALYTICS_QUERY_LIMIT);
5055
let (interval, period) = get_time_frame(&query.timeframe);
5156
let time_limit = Utc::now().timestamp() - period;
5257

5358
let mut where_clauses = vec![format!("created > to_timestamp({})", time_limit)];
5459

60+
params.push(&query.event_type);
61+
62+
where_clauses.extend(vec![
63+
format!("event_type = ${}", params.len()),
64+
format!("{} IS NOT NULL", query.metric)
65+
]);
66+
5567
if let Some(id) = channel_id {
5668
where_clauses.push(format!("channel_id = '{}'", id));
5769
}
@@ -60,26 +72,20 @@ pub async fn get_analytics(
6072
let mut select_clause = match analytics_type {
6173
AnalyticsType::Advertiser { session } => {
6274
if channel_id.is_none() {
63-
where_clauses.push(format!(
64-
"channel_id IN (SELECT id FROM channels WHERE creator = '{}')",
65-
session.uid
66-
));
75+
where_clauses.push(
76+
format!(
77+
"channel_id IN (SELECT id FROM channels WHERE creator = '{}')",
78+
session.uid
79+
)
80+
);
6781
}
6882

69-
where_clauses.push(format!("event_type = '{}'", query.event_type));
70-
71-
where_clauses.push(format!("{} IS NOT NULL", query.metric));
72-
7383
format!(
7484
"SUM({}::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time",
7585
query.metric, interval
7686
)
7787
}
7888
AnalyticsType::Global => {
79-
where_clauses.push(format!("event_type = '{}'", query.event_type));
80-
81-
where_clauses.push(format!("{} IS NOT NULL", query.metric));
82-
8389
where_clauses.push("earner IS NULL".to_string());
8490

8591
format!(
@@ -88,10 +94,6 @@ pub async fn get_analytics(
8894
)
8995
}
9096
AnalyticsType::Publisher { session } => {
91-
where_clauses.push(format!("event_type = '{}'", query.event_type));
92-
93-
where_clauses.push(format!("{} IS NOT NULL", query.metric));
94-
9597
where_clauses.push(format!("earner = '{}'", session.uid));
9698

9799
format!(
@@ -114,12 +116,10 @@ pub async fn get_analytics(
114116
applied_limit,
115117
);
116118

117-
println!("{}", sql_query);
118-
119119
// execute query
120120
pool.run(move |connection| async move {
121121
match connection.prepare(&sql_query).await {
122-
Ok(stmt) => match connection.query(&stmt, &[]).await {
122+
Ok(stmt) => match connection.query(&stmt, &params).await {
123123
Ok(rows) => {
124124
let analytics: Vec<AnalyticsData> =
125125
rows.iter().map(AnalyticsData::from).collect();

sentry/src/routes/analytics.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ pub async fn process_analytics<A: Adapter>(
8585
.is_valid()
8686
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
8787

88-
query.metric_to_column();
89-
9088
let channel_id = req.extensions().get::<ChannelId>();
9189

9290
let segment_channel = query

0 commit comments

Comments
 (0)