Skip to content

Commit 33e0357

Browse files
authored
Merge pull request #306 from AdExNetwork/supermarket-changes
Supermarket changes
2 parents 8045b6b + c918034 commit 33e0357

File tree

6 files changed

+99
-72
lines changed

6 files changed

+99
-72
lines changed

primitives/src/ad_slot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct AdSlot {
5959
#[serde(default)]
6060
pub description: Option<String>,
6161
#[serde(default)]
62-
pub webiste: Option<String>,
62+
pub website: Option<String>,
6363
/// user can change it - used for filtering in platform UI
6464
#[serde(default)]
6565
pub archived: bool,

primitives/src/sentry.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,40 @@ impl fmt::Display for ChannelReport {
215215
}
216216
}
217217

218+
pub mod channel_list {
219+
use crate::ValidatorId;
220+
use chrono::serde::ts_seconds::deserialize as ts_seconds;
221+
use chrono::{DateTime, Utc};
222+
use serde::Deserialize;
223+
224+
#[derive(Debug, Deserialize)]
225+
pub struct ChannelListQuery {
226+
#[serde(default = "default_page")]
227+
pub page: u64,
228+
/// filters the list on `valid_until >= valid_until_ge`
229+
/// It should be the same timestamp format as the `Channel.valid_until`: **seconds**
230+
#[serde(
231+
deserialize_with = "ts_seconds",
232+
default = "Utc::now",
233+
rename = "validUntil"
234+
)]
235+
pub valid_until_ge: DateTime<Utc>,
236+
pub creator: Option<String>,
237+
/// filters the channels containing a specific validator if provided
238+
pub validator: Option<ValidatorId>,
239+
}
240+
241+
#[derive(Debug, Deserialize)]
242+
#[serde(rename_all = "camelCase")]
243+
pub struct LastApprovedQuery {
244+
pub with_heartbeat: Option<String>,
245+
}
246+
247+
fn default_page() -> u64 {
248+
0
249+
}
250+
}
251+
218252
#[cfg(feature = "postgres")]
219253
mod postgres {
220254
use super::{

primitives/src/targeting.rs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ impl Input {
3030
.map(|ad_view| Value::Bool(ad_view.has_custom_preferences))
3131
.ok_or(Error::UnknownVariable),
3232
"adSlotId" => Ok(Value::String(self.global.ad_slot_id.clone())),
33+
"adSlotType" => Ok(Value::String(self.global.ad_slot_type.clone())),
3334
"adUnitId" => Ok(Value::String(self.global.ad_unit_id.clone())),
34-
"adUnitType" => Ok(Value::String(self.global.ad_unit_type.clone())),
3535
"publisherId" => Ok(Value::String(self.global.publisher_id.clone())),
3636
"advertiserId" => Ok(Value::String(self.global.advertiser_id.clone())),
37-
"country" => Ok(Value::String(self.global.country.clone())),
37+
"country" => self
38+
.global
39+
.country
40+
.clone()
41+
.map(Value::String)
42+
.ok_or(Error::UnknownVariable),
3843
"eventType" => Ok(Value::String(self.global.event_type.clone())),
39-
"campaignId" => Ok(Value::String(self.global.campiagn_id.clone())),
44+
"campaignId" => Ok(Value::String(self.global.campaign_id.clone())),
4045
"campaignTotalSpent" => Ok(Value::String(self.global.campaign_total_spent.clone())),
4146
"campaignSecondsActive" => {
4247
Ok(Value::Number(self.global.campaign_seconds_active.into()))
@@ -51,10 +56,18 @@ impl Input {
5156
self.global.publisher_earned_from_campaign.clone(),
5257
)),
5358
"secondsSinceEpoch" => Ok(Value::Number(self.global.seconds_since_epoch.into())),
54-
"userAgentOS" => Ok(Value::String(self.global.user_agent_os.clone())),
55-
"userAgentBrowserFamily" => {
56-
Ok(Value::String(self.global.user_agent_browser_family.clone()))
57-
}
59+
"userAgentOS" => self
60+
.global
61+
.user_agent_os
62+
.clone()
63+
.map(Value::String)
64+
.ok_or(Error::UnknownVariable),
65+
"userAgentBrowserFamily" => self
66+
.global
67+
.user_agent_browser_family
68+
.clone()
69+
.map(Value::String)
70+
.ok_or(Error::UnknownVariable),
5871
"adSlot.categories" => self
5972
.ad_slot
6073
.as_ref()
@@ -70,12 +83,14 @@ impl Input {
7083
"adSlot.hostname" => self
7184
.ad_slot
7285
.as_ref()
73-
.map(|ad_slot| Value::String(ad_slot.hostname.clone()))
86+
.map(|ad_slot| Value::String(ad_slot.hostname.clone().unwrap_or_default()))
7487
.ok_or(Error::UnknownVariable),
7588
"adSlot.alexaRank" => {
89+
// @TODO: Decide how to handle Alexa rank values
7690
let ad_slot = self.ad_slot.as_ref().ok_or(Error::UnknownVariable)?;
91+
let alexa_rank = ad_slot.alexa_rank.ok_or(Error::UnknownVariable)?;
7792

78-
match serde_json::Number::from_f64(ad_slot.alexa_rank) {
93+
match serde_json::Number::from_f64(alexa_rank) {
7994
Some(number) => Ok(Value::Number(number)),
8095
None => Err(Error::TypeError),
8196
}
@@ -95,32 +110,35 @@ pub struct AdView {
95110
#[derive(Debug, Clone)]
96111
#[cfg_attr(test, derive(Default))]
97112
pub struct Global {
113+
/// Global scope, accessible everywhere
98114
pub ad_slot_id: String,
99-
pub ad_unit_id: String,
100-
pub ad_unit_type: String,
115+
pub ad_slot_type: String,
101116
pub publisher_id: String,
102-
pub advertiser_id: String,
103-
pub country: String,
117+
pub country: Option<String>,
104118
pub event_type: String,
105-
pub campiagn_id: String,
119+
pub seconds_since_epoch: u64,
120+
pub user_agent_os: Option<String>,
121+
pub user_agent_browser_family: Option<String>,
122+
/// Global scope, accessible everywhere, campaign-dependant
123+
pub ad_unit_id: String,
124+
// adUnitCategories
125+
pub advertiser_id: String,
126+
pub campaign_id: String,
106127
pub campaign_total_spent: String,
107128
pub campaign_seconds_active: u64,
108129
pub campaign_seconds_duration: u64,
109130
pub campaign_budget: BigNum,
110131
pub event_min_price: BigNum,
111132
pub event_max_price: BigNum,
112133
pub publisher_earned_from_campaign: BigNum,
113-
pub seconds_since_epoch: u64,
114-
pub user_agent_os: String,
115-
pub user_agent_browser_family: String,
116134
}
117135

118136
#[derive(Debug, Clone)]
119137
#[cfg_attr(test, derive(Default))]
120138
pub struct AdSlot {
121139
pub categories: Vec<String>,
122-
pub hostname: String,
123-
pub alexa_rank: f64,
140+
pub hostname: Option<String>,
141+
pub alexa_rank: Option<f64>,
124142
}
125143

126144
#[derive(Debug)]
@@ -197,6 +215,15 @@ mod test {
197215
.expect("Should get the global.campaign_budget field");
198216

199217
assert_eq!(Value::BigNum(BigNum::from(50)), global_campaign_budget);
218+
219+
assert_eq!(
220+
Err(Error::UnknownVariable),
221+
input.try_get("adSlot.alexaRank")
222+
);
223+
let mut ad_slot = AdSlot::default();
224+
ad_slot.alexa_rank = Some(20.0);
225+
input.ad_slot = Some(ad_slot);
226+
assert!(input.try_get("adSlot.alexaRank").is_ok());
200227
}
201228

202229
#[test]

primitives/src/targeting/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ mod test {
628628
input.ad_slot = Some(AdSlot {
629629
categories: vec!["Bitcoin".to_string(), "Ethereum".to_string()],
630630
hostname: Default::default(),
631-
alexa_rank: 0.0,
631+
alexa_rank: Some(0.0),
632632
});
633633

634634
let mut output = Output {
@@ -655,7 +655,7 @@ mod test {
655655
input.ad_slot = Some(AdSlot {
656656
categories: vec!["Advertisement".to_string(), "Programming".to_string()],
657657
hostname: Default::default(),
658-
alexa_rank: 0.0,
658+
alexa_rank: Some(0.0),
659659
});
660660

661661
let result = rules.eval(&input, &mut output).expect("Should eval rules");

sentry/src/db/event_aggregate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use primitives::BigNum;
1313
use primitives::{Channel, ChannelId, ValidatorId};
1414
use std::ops::Add;
1515

16-
pub async fn lastest_approve_state(
16+
pub async fn latest_approve_state(
1717
pool: &DbPool,
1818
channel: &Channel,
1919
) -> Result<Option<ApproveStateValidatorMessage>, RunError<bb8_postgres::tokio_postgres::Error>> {
@@ -108,7 +108,7 @@ pub async fn list_event_aggregates(
108108
let statement = format!(
109109
"
110110
WITH aggregates AS (
111-
SELECT
111+
SELECT
112112
channel_id,
113113
created,
114114
event_type,
@@ -124,7 +124,7 @@ pub async fn list_event_aggregates(
124124
jsonb_build_object(
125125
earner, payout
126126
)
127-
)
127+
)
128128
)
129129
as data
130130
FROM event_aggregates WHERE {} GROUP BY channel_id, event_type, created ORDER BY created DESC LIMIT {}

sentry/src/routes/channel.rs

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
use self::channel_list::{ChannelListQuery, LastApprovedQuery};
2-
use crate::db::event_aggregate::{lastest_approve_state, latest_heartbeats, latest_new_state};
1+
use crate::db::event_aggregate::{latest_approve_state, latest_heartbeats, latest_new_state};
32
use crate::db::{get_channel_by_id, insert_channel, insert_validator_messages, list_channels};
4-
use crate::success_response;
5-
use crate::Application;
6-
use crate::ResponseError;
7-
use crate::RouteParams;
8-
use crate::{Auth, Session};
3+
use crate::{success_response, Application, Auth, ResponseError, RouteParams, Session};
94
use bb8::RunError;
105
use bb8_postgres::tokio_postgres::error;
116
use futures::future::try_join_all;
127
use hex::FromHex;
138
use hyper::{Body, Request, Response};
14-
use primitives::adapter::Adapter;
15-
use primitives::sentry::{Event, LastApproved, LastApprovedResponse, SuccessResponse};
16-
use primitives::validator::MessageTypes;
17-
use primitives::{Channel, ChannelId};
9+
use primitives::{
10+
adapter::Adapter,
11+
sentry::{
12+
channel_list::{ChannelListQuery, LastApprovedQuery},
13+
Event, LastApproved, LastApprovedResponse, SuccessResponse,
14+
},
15+
validator::MessageTypes,
16+
Channel, ChannelId,
17+
};
1818
use slog::error;
1919
use std::collections::HashMap;
2020

@@ -130,7 +130,7 @@ pub async fn last_approved<A: Adapter>(
130130
)
131131
.expect("should build response");
132132

133-
let approve_state = match lastest_approve_state(&app.pool, &channel).await? {
133+
let approve_state = match latest_approve_state(&app.pool, &channel).await? {
134134
Some(approve_state) => approve_state,
135135
None => return Ok(default_response),
136136
};
@@ -139,7 +139,7 @@ pub async fn last_approved<A: Adapter>(
139139
MessageTypes::ApproveState(approve_state) => approve_state.state_root,
140140
_ => {
141141
error!(&app.logger, "{}", "failed to retrieve approved"; "module" => "last_approved");
142-
return Err(ResponseError::BadRequest("an error occured".to_string()));
142+
return Err(ResponseError::BadRequest("an error occurred".to_string()));
143143
}
144144
};
145145

@@ -252,37 +252,3 @@ pub async fn create_validator_messages<A: Adapter + 'static>(
252252
}
253253
}
254254
}
255-
256-
mod channel_list {
257-
use chrono::serde::ts_seconds::deserialize as ts_seconds;
258-
use chrono::{DateTime, Utc};
259-
use primitives::ValidatorId;
260-
use serde::Deserialize;
261-
262-
#[derive(Debug, Deserialize)]
263-
pub(super) struct ChannelListQuery {
264-
#[serde(default = "default_page")]
265-
pub page: u64,
266-
/// filters the list on `valid_until >= valid_until_ge`
267-
/// It should be the same timestamp format as the `Channel.valid_until`: **seconds**
268-
#[serde(
269-
deserialize_with = "ts_seconds",
270-
default = "Utc::now",
271-
rename = "validUntil"
272-
)]
273-
pub valid_until_ge: DateTime<Utc>,
274-
pub creator: Option<String>,
275-
/// filters the channels containing a specific validator if provided
276-
pub validator: Option<ValidatorId>,
277-
}
278-
279-
#[derive(Debug, Deserialize)]
280-
#[serde(rename_all = "camelCase")]
281-
pub(super) struct LastApprovedQuery {
282-
pub with_heartbeat: Option<String>,
283-
}
284-
285-
fn default_page() -> u64 {
286-
0
287-
}
288-
}

0 commit comments

Comments
 (0)