Skip to content

Commit 76f14ff

Browse files
committed
add: analytics advertiser route id param
1 parent 31ce5b1 commit 76f14ff

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

sentry/src/db/analytics.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use crate::db::DbPool;
2-
use crate::RouteParams;
32
use crate::Session;
43
use bb8::RunError;
54
use chrono::Utc;
65
use primitives::analytics::{AnalyticsQuery, AnalyticsResponse};
76

87
pub async fn get_analytics(
98
query: AnalyticsQuery,
10-
route_params: Option<&RouteParams>,
9+
channels: Option<String>,
1110
sess: Option<&Session>,
1211
pool: &DbPool,
1312
is_advertiser: bool,
@@ -20,17 +19,15 @@ pub async fn get_analytics(
2019
let mut where_clauses = vec![format!("created > to_timestamp({})", time_limit)];
2120

2221
if is_advertiser {
23-
match route_params {
24-
Some(params) => where_clauses.push(format!("channel_id IN ({})", params.index(0))),
22+
match channels {
23+
Some(id) => where_clauses.push(format!("channel_id IN ({})", id)),
2524
None => where_clauses.push(format!(
2625
"channel_id IN (SELECT id FROM channels WHERE creator = {})",
2726
sess.unwrap().uid.to_string()
2827
)),
2928
};
30-
} else if let Some(params) = route_params {
31-
if let Some(id) = params.get(0) {
32-
where_clauses.push(format!("channel_id = {}", id));
33-
};
29+
} else if let Some(id) = channels {
30+
where_clauses.push(format!("channel_id = {}", id));
3431
}
3532

3633
let select_query = match (skip_publisher_filter, sess) {

sentry/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ lazy_static! {
4747
static ref CHANNEL_VALIDATOR_MESSAGES: Regex = Regex::new(r"^/channel/0x([a-zA-Z0-9]{64})/validator-messages(/.*)?$").expect("The regex should be valid");
4848
// @TODO define other regex routes
4949
static ref ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
50+
static ref ADVERTISER_ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/for-advertiser/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
5051
static ref PUBLISHER_ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/for-publisher/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
5152

5253
}
@@ -134,6 +135,7 @@ impl<A: Adapter + 'static> Application<A> {
134135

135136
("/analytics", &Method::GET) => analytics(req, &self).await,
136137
("/analytics/for-advertiser", &Method::GET) => {
138+
// @TODO get advertiser channels
137139
let req = match chain(req, &self, vec![auth_required_middleware]).await {
138140
Ok(req) => req,
139141
Err(error) => {
@@ -191,6 +193,14 @@ async fn analytics_router<A: Adapter>(
191193
let req = chain(req, app, vec![auth_required_middleware]).await?;
192194

193195
publisher_analytics(req, app).await
196+
} else if let Some(caps) = ADVERTISER_ANALYTICS_BY_CHANNEL_ID.captures(route) {
197+
let param = RouteParams(vec![caps
198+
.get(1)
199+
.map_or("".to_string(), |m| m.as_str().to_string())]);
200+
req.extensions_mut().insert(param);
201+
202+
let req = chain(req, app, vec![auth_required_middleware]).await?;
203+
advertiser_analytics(req, app).await
194204
} else {
195205
Err(ResponseError::NotFound)
196206
}

sentry/src/routes/analytics.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub async fn analytics<A: Adapter>(
3232
{
3333
Ok(response) => Ok(success_response(response)),
3434
_ => {
35+
// checks if /:id route param is present cache
3536
let cache_timeframe = match req.extensions().get::<RouteParams>() {
3637
Some(_) => 600,
3738
None => 300,
@@ -64,11 +65,14 @@ pub async fn process_analytics<A: Adapter>(
6465
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
6566

6667
let sess = req.extensions().get::<Session>();
67-
let params = req.extensions().get::<RouteParams>();
68+
let channels = match req.extensions().get::<RouteParams>() {
69+
Some(param) => param.get(0),
70+
None => None,
71+
};
6872

6973
let result = get_analytics(
7074
query,
71-
params,
75+
channels,
7276
sess,
7377
&app.pool,
7478
is_advertiser,

0 commit comments

Comments
 (0)