Skip to content

Commit afcf824

Browse files
committed
fix: validator sql, analytics query
1 parent da0dda9 commit afcf824

File tree

7 files changed

+76
-18
lines changed

7 files changed

+76
-18
lines changed

adapter/src/ethereum.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,10 @@ mod test {
594594
token_contract
595595
.call(
596596
"setBalanceTo",
597-
(Token::Address(leader_account), Token::Uint(U256::from(2000 as u64))),
597+
(
598+
Token::Address(leader_account),
599+
Token::Uint(U256::from(2000 as u64)),
600+
),
598601
leader_account,
599602
Options::default(),
600603
)
@@ -645,7 +648,7 @@ mod test {
645648
nonce: None,
646649
withdraw_period_start: Utc::now() + Duration::days(1),
647650
ad_units: vec![],
648-
pricing_bounds: None
651+
pricing_bounds: None,
649652
},
650653
};
651654

primitives/src/analytics.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ use serde::{Deserialize, Serialize};
33

44
pub const ANALYTICS_QUERY_LIMIT: u32 = 200;
55

6+
#[derive(Debug, Serialize, Deserialize)]
7+
pub struct AnalyticsData {
8+
pub time: f64,
9+
pub value: String,
10+
}
11+
612
#[derive(Debug, Serialize, Deserialize)]
713
pub struct AnalyticsResponse {
8-
time: u32,
9-
value: String,
14+
pub aggr: Vec<AnalyticsData>,
15+
pub limit: u32
1016
}
1117

1218
#[cfg(feature = "postgres")]
1319
pub mod postgres {
14-
use super::AnalyticsResponse;
20+
use super::AnalyticsData;
1521
use tokio_postgres::Row;
1622

17-
impl From<&Row> for AnalyticsResponse {
23+
impl From<&Row> for AnalyticsData {
1824
fn from(row: &Row) -> Self {
1925
Self {
2026
time: row.get("time"),
@@ -39,6 +45,13 @@ pub struct AnalyticsQuery {
3945
}
4046

4147
impl AnalyticsQuery {
48+
pub fn metric_to_column(&mut self) {
49+
self.metric = match self.metric.as_str() {
50+
"eventCounts"=> "count".to_string(),
51+
"eventPayouts" => "payout".to_string(),
52+
_ => "count".to_string(),
53+
};
54+
}
4255
pub fn is_valid(&self) -> Result<(), DomainError> {
4356
let valid_event_types = ["IMPRESSION", "CLICK"];
4457
let valid_metric = ["eventPayouts", "eventCounts"];

primitives/src/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ pub struct Pricing {
7272
#[derive(Serialize, Deserialize, Debug, Clone)]
7373
#[serde(rename_all = "UPPERCASE")]
7474
pub struct PricingBounds {
75-
pub impression: Pricing,
76-
pub click: Pricing,
75+
pub impression: Option<Pricing>,
76+
pub click: Option<Pricing>,
7777
}
7878

7979
#[derive(Serialize, Deserialize, Debug, Clone)]

primitives/src/sentry.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct LastApproved {
1212
/// NewState can be None if the channel is brand new
1313
pub new_state: Option<NewStateValidatorMessage>,
1414
/// ApproveState can be None if the channel is brand new
15-
pub approved_state: Option<ApproveStateValidatorMessage>,
15+
pub approve_state: Option<ApproveStateValidatorMessage>,
1616
}
1717

1818
#[derive(Serialize, Deserialize, Debug)]
@@ -29,6 +29,13 @@ pub struct ApproveStateValidatorMessage {
2929
pub msg: ApproveState,
3030
}
3131

32+
#[derive(Serialize, Deserialize, Debug)]
33+
pub struct HeartbeatValidatorMessage {
34+
pub from: String,
35+
pub received: DateTime<Utc>,
36+
pub msg: Heartbeat,
37+
}
38+
3239
#[serde(tag = "type", rename_all = "SCREAMING_SNAKE_CASE")]
3340
#[derive(Serialize, Deserialize, Clone)]
3441
pub enum Event {
@@ -126,7 +133,7 @@ pub struct ChannelListResponse {
126133
#[serde(rename_all = "camelCase")]
127134
pub struct LastApprovedResponse {
128135
pub last_approved: Option<LastApproved>,
129-
pub heartbeats: Option<Vec<Heartbeat>>,
136+
pub heartbeats: Option<Vec<HeartbeatValidatorMessage>>,
130137
}
131138

132139
#[derive(Serialize, Deserialize, Debug)]
@@ -202,9 +209,12 @@ impl fmt::Display for ChannelReport {
202209

203210
#[cfg(feature = "postgres")]
204211
mod postgres {
205-
use super::ValidatorMessage;
212+
use super::{
213+
ApproveStateValidatorMessage, HeartbeatValidatorMessage, NewStateValidatorMessage,
214+
ValidatorMessage,
215+
};
206216
use crate::sentry::EventAggregate;
207-
use crate::validator::MessageTypes;
217+
use crate::validator::{ApproveState, Heartbeat, MessageTypes, NewState};
208218
use bytes::BytesMut;
209219
use postgres_types::{accepts, to_sql_checked, IsNull, Json, ToSql, Type};
210220
use std::error::Error;
@@ -230,6 +240,36 @@ mod postgres {
230240
}
231241
}
232242

243+
impl From<&Row> for ApproveStateValidatorMessage {
244+
fn from(row: &Row) -> Self {
245+
Self {
246+
from: row.get("from"),
247+
received: row.get("received"),
248+
msg: row.get::<_, Json<ApproveState>>("msg").0,
249+
}
250+
}
251+
}
252+
253+
impl From<&Row> for NewStateValidatorMessage {
254+
fn from(row: &Row) -> Self {
255+
Self {
256+
from: row.get("from"),
257+
received: row.get("received"),
258+
msg: row.get::<_, Json<NewState>>("msg").0,
259+
}
260+
}
261+
}
262+
263+
impl From<&Row> for HeartbeatValidatorMessage {
264+
fn from(row: &Row) -> Self {
265+
Self {
266+
from: row.get("from"),
267+
received: row.get("received"),
268+
msg: row.get::<_, Json<Heartbeat>>("msg").0,
269+
}
270+
}
271+
}
272+
233273
impl ToSql for MessageTypes {
234274
fn to_sql(
235275
&self,

primitives/src/validator.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ pub mod postgres {
178178
use postgres_types::{FromSql, IsNull, ToSql, Type};
179179
use std::convert::TryFrom;
180180
use std::error::Error;
181+
use crate::ToETHChecksum;
181182

182183
impl<'a> FromSql<'a> for ValidatorId {
183184
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
@@ -201,7 +202,7 @@ pub mod postgres {
201202
ty: &Type,
202203
w: &mut BytesMut,
203204
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
204-
let string = format!("0x{}", self.to_hex_non_prefix_string());
205+
let string = self.to_checksum();
205206

206207
<String as ToSql>::to_sql(&string, ty, w)
207208
}
@@ -215,7 +216,7 @@ pub mod postgres {
215216
ty: &Type,
216217
out: &mut BytesMut,
217218
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
218-
let string = format!("0x{}", self.to_hex_non_prefix_string());
219+
let string = self.to_checksum();
219220

220221
<String as ToSql>::to_sql_checked(&string, ty, out)
221222
}

sentry/src/db/analytics.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ pub async fn get_analytics(
5555
let time_limit = Utc::now().timestamp() - period;
5656

5757
let mut where_clauses = vec![format!("created > to_timestamp({})", time_limit)];
58+
5859
if let Some(id) = channel_id {
59-
where_clauses.push(format!("channel_id = {}", id));
60+
where_clauses.push(format!("channel_id = '{}'", id));
6061
}
6162

6263
let mut group_clause = "time".to_string();
6364
let mut select_clause = match analytics_type {
6465
AnalyticsType::Advertiser { session } => {
6566
if channel_id.is_none() {
6667
where_clauses.push(format!(
67-
"channel_id IN (SELECT id FROM channels WHERE creator = {})",
68+
"channel_id IN (SELECT id FROM channels WHERE creator = '{}')",
6869
session.uid
6970
));
7071
}
@@ -125,7 +126,7 @@ pub async fn get_analytics(
125126
// ));
126127

127128
format!(
128-
"SUM(({}::numeric) as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time from event_aggregates",
129+
"SUM({}::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time from event_aggregates",
129130
query.metric, interval
130131
)
131132
}

sentry/src/routes/analytics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub async fn analytics<A: Adapter>(
4545
_ => {
4646
// checks if /:id route param is present
4747
let cache_timeframe= match req.extensions().get::<RouteParams>() {
48-
Some(param) => 600,
48+
Some(_) => 600,
4949
None => 300,
5050
};
5151
let response = process_analytics(req, app, AnalyticsType::Global).await?;

0 commit comments

Comments
 (0)