Skip to content

Commit db1b128

Browse files
committed
fix: apply proper formatting
1 parent 80cde9c commit db1b128

File tree

8 files changed

+67
-84
lines changed

8 files changed

+67
-84
lines changed

primitives/src/analytics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use crate::ChannelId;
12
use crate::DomainError;
23
use serde::{Deserialize, Serialize};
3-
use crate::ChannelId;
44

55
pub const ANALYTICS_QUERY_LIMIT: u32 = 200;
66

@@ -10,7 +10,7 @@ pub struct AnalyticsData {
1010
pub time: f64,
1111
pub value: String,
1212
#[serde(default, skip_serializing_if = "Option::is_none")]
13-
pub channel_id: Option<ChannelId>
13+
pub channel_id: Option<ChannelId>,
1414
}
1515

1616
#[derive(Debug, Serialize, Deserialize)]
@@ -52,7 +52,7 @@ pub struct AnalyticsQuery {
5252
impl AnalyticsQuery {
5353
pub fn metric_to_column(&mut self) {
5454
self.metric = match self.metric.as_str() {
55-
"eventCounts"=> "count".to_string(),
55+
"eventCounts" => "count".to_string(),
5656
"eventPayouts" => "payout".to_string(),
5757
_ => "count".to_string(),
5858
};

primitives/src/validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ pub enum MessageTypes {
174174
#[cfg(feature = "postgres")]
175175
pub mod postgres {
176176
use super::ValidatorId;
177+
use crate::ToETHChecksum;
177178
use bytes::BytesMut;
178179
use postgres_types::{FromSql, IsNull, ToSql, Type};
179180
use std::convert::TryFrom;
180181
use std::error::Error;
181-
use crate::ToETHChecksum;
182182

183183
impl<'a> FromSql<'a> for ValidatorId {
184184
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {

sentry/src/db/analytics.rs

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::epoch;
33
use crate::Session;
44
use bb8::RunError;
55
use chrono::Utc;
6-
use primitives::analytics::{AnalyticsQuery, AnalyticsData, ANALYTICS_QUERY_LIMIT};
6+
use primitives::analytics::{AnalyticsData, AnalyticsQuery, ANALYTICS_QUERY_LIMIT};
77
use primitives::sentry::{AdvancedAnalyticsResponse, ChannelReport, PublisherReport};
88
use primitives::{ChannelId, ValidatorId};
99
use redis;
@@ -12,13 +12,9 @@ use std::collections::HashMap;
1212
use std::error::Error;
1313

1414
pub enum AnalyticsType {
15-
Advertiser {
16-
session: Session,
17-
},
15+
Advertiser { session: Session },
1816
Global,
19-
Publisher {
20-
session: Session,
21-
},
17+
Publisher { session: Session },
2218
}
2319

2420
pub async fn advertiser_channel_ids(
@@ -70,31 +66,19 @@ pub async fn get_analytics(
7066
));
7167
}
7268

73-
where_clauses.push(format!(
74-
"event_type = '{}'",
75-
query.event_type
76-
));
69+
where_clauses.push(format!("event_type = '{}'", query.event_type));
7770

78-
where_clauses.push(format!(
79-
"{} IS NOT NULL",
80-
query.metric
81-
));
71+
where_clauses.push(format!("{} IS NOT NULL", query.metric));
8272

8373
format!(
8474
"SUM({}::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time",
8575
query.metric, interval
8676
)
8777
}
88-
AnalyticsType::Global => {
89-
where_clauses.push(format!(
90-
"event_type = '{}'",
91-
query.event_type
92-
));
78+
AnalyticsType::Global => {
79+
where_clauses.push(format!("event_type = '{}'", query.event_type));
9380

94-
where_clauses.push(format!(
95-
"{} IS NOT NULL",
96-
query.metric
97-
));
81+
where_clauses.push(format!("{} IS NOT NULL", query.metric));
9882

9983
where_clauses.push("earner IS NULL".to_string());
10084

@@ -104,21 +88,12 @@ pub async fn get_analytics(
10488
)
10589
}
10690
AnalyticsType::Publisher { session } => {
107-
where_clauses.push(format!(
108-
"event_type = '{}'",
109-
query.event_type
110-
));
91+
where_clauses.push(format!("event_type = '{}'", query.event_type));
11192

112-
where_clauses.push(format!(
113-
"{} IS NOT NULL",
114-
query.metric
115-
));
93+
where_clauses.push(format!("{} IS NOT NULL", query.metric));
94+
95+
where_clauses.push(format!("earner = '{}'", session.uid));
11696

117-
where_clauses.push(format!(
118-
"earner = '{}'",
119-
session.uid
120-
));
121-
12297
format!(
12398
"SUM({}::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time",
12499
query.metric, interval
@@ -139,8 +114,6 @@ pub async fn get_analytics(
139114
applied_limit,
140115
);
141116

142-
println!("{}", sql_query);
143-
144117
// execute query
145118
pool.run(move |connection| async move {
146119
match connection.prepare(&sql_query).await {
@@ -202,7 +175,6 @@ pub async fn get_advanced_reports(
202175
publisher: &ValidatorId,
203176
channel_ids: &[ChannelId],
204177
) -> Result<AdvancedAnalyticsResponse, Box<dyn Error>> {
205-
println!("get advnaces");
206178
let publisher_reports = [
207179
PublisherReport::ReportPublisherToAdUnit,
208180
PublisherReport::ReportPublisherToAdSlot,
@@ -250,7 +222,6 @@ pub async fn get_advanced_reports(
250222

251223
by_channel_stats.insert(channel_id.to_owned(), channel_stat);
252224
}
253-
254225

255226
Ok(AdvancedAnalyticsResponse {
256227
publisher_stats,

sentry/src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ lazy_static! {
5757
static ref CHANNEL_EVENTS_AGGREGATES: Regex = Regex::new(r"^/channel/0x([a-zA-Z0-9]{64})/events-aggregates/?$").expect("The regex should be valid");
5858
static ref ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
5959
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");
60-
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");
60+
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");
6161
static ref CREATE_EVENTS_BY_CHANNEL_ID: Regex = Regex::new(r"^/channel/0x([a-zA-Z0-9]{64})/events(/.*)?$").expect("The regex should be valid");
6262

6363
}
@@ -199,10 +199,12 @@ async fn analytics_router<A: Adapter + 'static>(
199199
.map_or("".to_string(), |m| m.as_str().to_string())]);
200200
req.extensions_mut().insert(param);
201201

202-
let req = chain(req, app, vec![
203-
Box::new(channel_load),
204-
Box::new(get_channel_id)
205-
]).await?;
202+
let req = chain(
203+
req,
204+
app,
205+
vec![Box::new(channel_load), Box::new(get_channel_id)],
206+
)
207+
.await?;
206208

207209
analytics(req, app).await
208210
} else if let Some(caps) = ADVERTISER_ANALYTICS_BY_CHANNEL_ID.captures(route) {
@@ -211,10 +213,12 @@ async fn analytics_router<A: Adapter + 'static>(
211213
.map_or("".to_string(), |m| m.as_str().to_string())]);
212214
req.extensions_mut().insert(param);
213215

214-
let req = chain(req, app, vec![
215-
Box::new(auth_required_middleware),
216-
Box::new(get_channel_id)
217-
]).await?;
216+
let req = chain(
217+
req,
218+
app,
219+
vec![Box::new(auth_required_middleware), Box::new(get_channel_id)],
220+
)
221+
.await?;
218222

219223
advertiser_analytics(req, app).await
220224
} else if let Some(caps) = PUBLISHER_ANALYTICS_BY_CHANNEL_ID.captures(route) {
@@ -223,10 +227,12 @@ async fn analytics_router<A: Adapter + 'static>(
223227
.map_or("".to_string(), |m| m.as_str().to_string())]);
224228
req.extensions_mut().insert(param);
225229

226-
let req = chain(req, app, vec![
227-
Box::new(auth_required_middleware),
228-
Box::new(get_channel_id)
229-
]).await?;
230+
let req = chain(
231+
req,
232+
app,
233+
vec![Box::new(auth_required_middleware), Box::new(get_channel_id)],
234+
)
235+
.await?;
230236

231237
publisher_analytics(req, app).await
232238
} else {

sentry/src/middleware/auth.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub(crate) async fn for_request(
2323
.and_then(|hv| {
2424
hv.to_str()
2525
.map(|token_str| {
26-
println!("token str {}", token_str );
2726
if token_str.starts_with(prefix) {
2827
Some(token_str[prefix.len()..].to_string())
2928
} else {

sentry/src/middleware/channel.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,22 @@ pub fn channel_if_active<'a, A: Adapter + 'static>(
6767
.boxed()
6868
}
6969

70-
7170
pub fn get_channel_id<'a, A: Adapter + 'static>(
7271
mut req: Request<Body>,
73-
_: &'a Application<A>
72+
_: &'a Application<A>,
7473
) -> BoxFuture<'a, Result<Request<Body>, ResponseError>> {
7574
async move {
7675
match req.extensions().get::<RouteParams>() {
77-
Some(param) => {
78-
let id = param.get(0).expect("should have channel id");
79-
let channel_id = ChannelId::from_hex(id)
80-
.map_err(|_| ResponseError::BadRequest("Invalid Channel Id".to_string()))?;
81-
req.extensions_mut().insert(channel_id);
76+
Some(param) => {
77+
let id = param.get(0).expect("should have channel id");
78+
let channel_id = ChannelId::from_hex(id)
79+
.map_err(|_| ResponseError::BadRequest("Invalid Channel Id".to_string()))?;
80+
req.extensions_mut().insert(channel_id);
8281

83-
Ok(req)
84-
},
85-
None => Ok(req),
82+
Ok(req)
83+
}
84+
None => Ok(req),
8685
}
8786
}
8887
.boxed()
89-
}
88+
}

sentry/src/routes/analytics.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use crate::db::analytics::{
2-
advertiser_channel_ids, get_advanced_reports, get_analytics, AnalyticsType
2+
advertiser_channel_ids, get_advanced_reports, get_analytics, AnalyticsType,
33
};
44
use crate::success_response;
55
use crate::Application;
66
use crate::ResponseError;
77
use crate::RouteParams;
88
use crate::Session;
99
use hyper::{Body, Request, Response};
10-
use primitives::ChannelId;
1110
use primitives::adapter::Adapter;
1211
use primitives::analytics::{AnalyticsQuery, AnalyticsResponse};
12+
use primitives::ChannelId;
1313
use redis::aio::MultiplexedConnection;
1414
use slog::{error, Logger};
1515

16-
1716
pub async fn publisher_analytics<A: Adapter>(
1817
req: Request<Body>,
1918
app: &Application<A>,
@@ -44,7 +43,7 @@ pub async fn analytics<A: Adapter>(
4443
Ok(Some(response)) => Ok(success_response(response)),
4544
_ => {
4645
// checks if /:id route param is present
47-
let cache_timeframe= match req.extensions().get::<RouteParams>() {
46+
let cache_timeframe = match req.extensions().get::<RouteParams>() {
4847
Some(_) => 600,
4948
None => 300,
5049
};
@@ -85,7 +84,7 @@ pub async fn process_analytics<A: Adapter>(
8584
query
8685
.is_valid()
8786
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
88-
87+
8988
query.metric_to_column();
9089

9190
let channel_id = req.extensions().get::<ChannelId>();
@@ -97,12 +96,16 @@ pub async fn process_analytics<A: Adapter>(
9796
.unwrap_or_else(|| false);
9897
let limit = query.limit;
9998

100-
let aggr = get_analytics(query, &app.pool, analytics_type, segment_channel, channel_id).await?;
99+
let aggr = get_analytics(
100+
query,
101+
&app.pool,
102+
analytics_type,
103+
segment_channel,
104+
channel_id,
105+
)
106+
.await?;
101107

102-
let response = AnalyticsResponse {
103-
limit,
104-
aggr
105-
};
108+
let response = AnalyticsResponse { limit, aggr };
106109

107110
serde_json::to_string(&response)
108111
.map_err(|_| ResponseError::BadRequest("error occurred; try again later".to_string()))
@@ -117,9 +120,14 @@ pub async fn advanced_analytics<A: Adapter>(
117120

118121
let query = serde_urlencoded::from_str::<AnalyticsQuery>(&req.uri().query().unwrap_or(""))?;
119122

120-
let response = get_advanced_reports(&app.redis, &query.event_type, &sess.uid, &advertiser_channels)
121-
.await
122-
.map_err(|_| ResponseError::BadRequest("error occurred; try again later".to_string()))?;
123+
let response = get_advanced_reports(
124+
&app.redis,
125+
&query.event_type,
126+
&sess.uid,
127+
&advertiser_channels,
128+
)
129+
.await
130+
.map_err(|_| ResponseError::BadRequest("error occurred; try again later".to_string()))?;
123131

124132
Ok(success_response(serde_json::to_string(&response)?))
125133
}

sentry/src/routes/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub async fn insert_events<A: Adapter + 'static>(
187187
let into_body = req.into_body();
188188
let body = hyper::body::to_bytes(into_body).await?;
189189
let request_body = serde_json::from_slice::<HashMap<String, Vec<Event>>>(&body)?;
190-
190+
191191
let events = request_body
192192
.get("events")
193193
.ok_or_else(|| ResponseError::BadRequest("invalid request".to_string()))?;

0 commit comments

Comments
 (0)