Skip to content

Commit 43f6fea

Browse files
committed
fix: advanced analytics recorder
1 parent 99fe7df commit 43f6fea

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

sentry/src/analytics_recorder.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::Session;
33
use primitives::sentry::Event;
44
use primitives::sentry::{ChannelReport, PublisherReport};
55
use primitives::{BigNum, Channel};
6-
use redis;
6+
use redis::pipe;
77
use redis::aio::MultiplexedConnection;
88
use slog::{error, Logger};
99

@@ -32,7 +32,7 @@ pub async fn record(
3232
events: Vec<Event>,
3333
logger: Logger,
3434
) {
35-
let mut db = redis::pipe();
35+
let mut db = pipe();
3636

3737
events
3838
.iter()
@@ -121,12 +121,10 @@ pub async fn record(
121121
.ignore();
122122
}
123123

124-
let hostname = match (referrer, &session.referrer_header) {
125-
(Some(referrer), _) | (_, Some(referrer)) => {
126-
referrer.split('/').nth(2).map(ToString::to_string)
127-
}
128-
_ => None,
129-
};
124+
let hostname = (referrer.as_ref())
125+
.or_else(|| session.referrer_header.as_ref())
126+
.map(|rf| rf.split('/').nth(2).map(ToString::to_string))
127+
.flatten();
130128

131129
if let Some(hostname) = &hostname {
132130
db.zincr(

sentry/src/db/analytics.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ pub async fn advertiser_channel_ids(
4040
.await
4141
}
4242

43+
fn metric_to_column(metric: &str) -> String {
44+
match metric {
45+
"eventCounts" => "count".to_string(),
46+
"eventPayouts" => "payout".to_string(),
47+
_ => "count".to_string(),
48+
}
49+
}
50+
4351
pub async fn get_analytics(
44-
mut query: AnalyticsQuery,
52+
query: AnalyticsQuery,
4553
pool: &DbPool,
4654
analytics_type: AnalyticsType,
4755
segment_by_channel: bool,
4856
channel_id: Option<&ChannelId>,
4957
) -> Result<Vec<AnalyticsData>, RunError<bb8_postgres::tokio_postgres::Error>> {
5058
// converts metric to column
51-
query.metric_to_column();
59+
let metric = metric_to_column(&query.metric);
5260

5361
let mut params = Vec::<&(dyn ToSql + Sync)>::new();
5462
let applied_limit = query.limit.min(ANALYTICS_QUERY_LIMIT);
@@ -61,7 +69,7 @@ pub async fn get_analytics(
6169

6270
where_clauses.extend(vec![
6371
format!("event_type = ${}", params.len()),
64-
format!("{} IS NOT NULL", query.metric)
72+
format!("{} IS NOT NULL", metric)
6573
]);
6674

6775
if let Some(id) = channel_id {
@@ -82,23 +90,23 @@ pub async fn get_analytics(
8290

8391
format!(
8492
"SUM({}::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time",
85-
query.metric, interval
93+
metric, interval
8694
)
8795
}
8896
AnalyticsType::Global => {
8997
where_clauses.push("earner IS NULL".to_string());
9098

9199
format!(
92100
"SUM({}::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time",
93-
query.metric, interval
101+
metric, interval
94102
)
95103
}
96104
AnalyticsType::Publisher { session } => {
97105
where_clauses.push(format!("earner = '{}'", session.uid));
98106

99107
format!(
100108
"SUM({}::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time",
101-
query.metric, interval
109+
metric, interval
102110
)
103111
}
104112
};

sentry/src/routes/analytics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub async fn process_analytics<A: Adapter>(
8080
app: &Application<A>,
8181
analytics_type: AnalyticsType,
8282
) -> Result<String, ResponseError> {
83-
let mut query = serde_urlencoded::from_str::<AnalyticsQuery>(&req.uri().query().unwrap_or(""))?;
83+
let query = serde_urlencoded::from_str::<AnalyticsQuery>(&req.uri().query().unwrap_or(""))?;
8484
query
8585
.is_valid()
8686
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;

0 commit comments

Comments
 (0)