Skip to content

Commit 5b3bc57

Browse files
committed
Changed chains to Vec instead of Option<Vec>
1 parent 2f7565f commit 5b3bc57

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

primitives/src/analytics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ pub struct AnalyticsQuery {
115115
pub hostname: Option<String>,
116116
pub country: Option<String>,
117117
pub os_name: Option<OperatingSystem>,
118-
pub chains: Option<Vec<ChainId>>,
118+
#[serde(default)]
119+
pub chains: Vec<ChainId>,
119120
}
120121

121122
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Display, Hash, Eq)]

primitives/src/chain.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ impl ChainOf<Campaign> {
118118

119119
#[cfg(feature = "postgres")]
120120
pub mod postgres {
121+
use super::ChainId;
121122
use bytes::BytesMut;
122-
use tokio_postgres::types::{accepts, to_sql_checked, FromSql, IsNull, ToSql, Type};
123123
use std::error::Error;
124-
use super::ChainId;
124+
use tokio_postgres::types::{accepts, to_sql_checked, FromSql, IsNull, ToSql, Type};
125125

126126
impl<'a> FromSql<'a> for ChainId {
127127
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<ChainId, Box<dyn Error + Sync + Send>> {
@@ -133,12 +133,16 @@ pub mod postgres {
133133
}
134134

135135
impl ToSql for ChainId {
136-
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
136+
fn to_sql(
137+
&self,
138+
ty: &Type,
139+
w: &mut BytesMut,
140+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
137141
<i32 as ToSql>::to_sql(&self.0.try_into()?, ty, w)
138142
}
139143

140144
accepts!(INT4);
141145

142146
to_sql_checked!();
143147
}
144-
}
148+
}

sentry/src/db/analytics.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ pub async fn get_analytics(
2121
let (mut where_clauses, params) =
2222
analytics_query_params(&query, auth_as.as_ref(), &allowed_keys);
2323

24-
if let Some(chains) = &query.chains {
24+
if !query.chains.is_empty() {
2525
where_clauses.push(format!(
2626
"chain_id IN ({})",
27-
chains
27+
query
28+
.chains
2829
.iter()
2930
.map(|id| id.as_u32().to_string())
3031
.collect::<Vec<String>>()
@@ -369,7 +370,7 @@ mod test {
369370
hostname: Some("localhost".into()),
370371
country: Some("Bulgaria".into()),
371372
os_name: Some(OperatingSystem::Linux),
372-
chains: None,
373+
chains: vec![],
373374
};
374375

375376
// Impression query - should count all inserted Analytics
@@ -474,7 +475,7 @@ mod test {
474475
hostname: Some("localhost".into()),
475476
country: Some("Estonia".into()),
476477
os_name: Some(OperatingSystem::Linux),
477-
chains: None,
478+
chains: vec![],
478479
};
479480

480481
// Click query - should count all inserted Analytics
@@ -624,7 +625,7 @@ mod test {
624625
hostname: None,
625626
country: None,
626627
os_name: None,
627-
chains: None,
628+
chains: vec![],
628629
};
629630

630631
let count_impressions = get_analytics(
@@ -680,7 +681,7 @@ mod test {
680681
hostname: None,
681682
country: None,
682683
os_name: None,
683-
chains: None,
684+
chains: vec![],
684685
};
685686

686687
let count_impressions = get_analytics(
@@ -736,7 +737,7 @@ mod test {
736737
hostname: None,
737738
country: None,
738739
os_name: None,
739-
chains: None,
740+
chains: vec![],
740741
};
741742

742743
let count_impressions = get_analytics(
@@ -792,7 +793,7 @@ mod test {
792793
hostname: None,
793794
country: None,
794795
os_name: None,
795-
chains: None,
796+
chains: vec![],
796797
};
797798

798799
let count_impressions = get_analytics(

sentry/src/routes/analytics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub async fn analytics<C: Locked + 'static>(
2424
// from the sentry's authentication, which guarantees the value will exist
2525
// This will also override a query parameter for the chain if it is provided
2626
if let Some(auth) = req.extensions().get::<Auth>() {
27-
query.chains = Some(vec![auth.chain.chain_id])
27+
query.chains = vec![auth.chain.chain_id]
2828
}
2929

3030
let applied_limit = query.limit.min(app.config.analytics_find_limit);

sentry/src/routes/routers.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ mod analytics_router_test {
431431
hostname: None,
432432
country: None,
433433
os_name: None,
434-
chains: Some(vec![ChainId::new(1)]),
434+
chains: vec![ChainId::new(1)],
435435
};
436436
let query = serde_qs::to_string(&query).expect("should parse query");
437437
let req = Request::builder()
@@ -478,7 +478,7 @@ mod analytics_router_test {
478478
hostname: None,
479479
country: None,
480480
os_name: None,
481-
chains: Some(vec![ChainId::new(1)]),
481+
chains: vec![ChainId::new(1)],
482482
};
483483
let query = serde_qs::to_string(&query).expect("should parse query");
484484
let req = Request::builder()
@@ -529,7 +529,7 @@ mod analytics_router_test {
529529
hostname: None,
530530
country: None,
531531
os_name: None,
532-
chains: Some(vec![ChainId::new(1)]),
532+
chains: vec![ChainId::new(1)],
533533
};
534534
let query = serde_qs::to_string(&query).expect("should parse query");
535535
let req = Request::builder()
@@ -581,7 +581,7 @@ mod analytics_router_test {
581581
hostname: None,
582582
country: Some("Bulgaria".into()),
583583
os_name: None,
584-
chains: Some(vec![ChainId::new(1)]),
584+
chains: vec![ChainId::new(1)],
585585
};
586586
let query = serde_qs::to_string(&query).expect("should parse query");
587587
let req = Request::builder()
@@ -850,7 +850,7 @@ mod analytics_router_test {
850850
hostname: None,
851851
country: None,
852852
os_name: None,
853-
chains: None,
853+
chains: vec![],
854854
};
855855
let query = serde_urlencoded::to_string(query).expect("should parse query");
856856
let req = Request::builder()
@@ -903,7 +903,7 @@ mod analytics_router_test {
903903
hostname: None,
904904
country: None,
905905
os_name: None,
906-
chains: Some(vec![ChainId::new(2)]),
906+
chains: vec![ChainId::new(2)],
907907
};
908908
let query = serde_qs::to_string(&query).expect("should parse query");
909909
let req = Request::builder()
@@ -1120,7 +1120,7 @@ mod analytics_router_test {
11201120
hostname: None,
11211121
country: None,
11221122
os_name: None,
1123-
chains: None,
1123+
chains: vec![],
11241124
};
11251125
let base_query = serde_urlencoded::to_string(query).expect("should parse query");
11261126

@@ -1259,7 +1259,7 @@ mod analytics_router_test {
12591259
hostname: Some("localhost".into()),
12601260
country: Some("Bulgaria".into()),
12611261
os_name: Some(OperatingSystem::map_os("Windows")),
1262-
chains: Some(vec![ChainId::new(1)]),
1262+
chains: vec![ChainId::new(1)],
12631263
};
12641264
let query = serde_qs::to_string(&query).expect("should parse query");
12651265
let req = Request::builder()
@@ -1313,7 +1313,6 @@ mod analytics_router_test {
13131313
);
13141314
}
13151315

1316-
13171316
// TODO: Move test to a analytics_router test
13181317
// test with no authUid
13191318
// let req = Request::builder()

0 commit comments

Comments
 (0)