Skip to content

Commit 96d3d8c

Browse files
committed
sentry - db - evnet_aggregate - list_event_aggregates
1 parent e2b060b commit 96d3d8c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

sentry/src/db.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ use std::env;
88
use lazy_static::lazy_static;
99

1010
mod channel;
11+
mod event_aggregate;
1112
mod validator_message;
1213

1314
pub use self::channel::*;
15+
pub use self::event_aggregate::*;
1416
pub use self::validator_message::*;
1517

1618
pub type DbPool = Pool<PostgresConnectionManager<NoTls>>;

sentry/src/db/event_aggregate.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::db::DbPool;
2+
use bb8::RunError;
3+
use bb8_postgres::tokio_postgres::types::ToSql;
4+
use chrono::{DateTime, Utc};
5+
use primitives::sentry::EventAggregate;
6+
use primitives::ValidatorId;
7+
8+
pub async fn list_event_aggregates(
9+
pool: &DbPool,
10+
limit: u32,
11+
from: &Option<ValidatorId>,
12+
after: &Option<DateTime<Utc>>,
13+
) -> Result<Vec<EventAggregate>, RunError<bb8_postgres::tokio_postgres::Error>> {
14+
let (mut where_clauses, mut params) = (vec![], Vec::<&(dyn ToSql + Sync)>::new());
15+
if let Some(from) = from {
16+
let key_counts = format!(
17+
"events->'IMPRESSION'->'eventPayouts'->'{}'",
18+
from.to_string()
19+
);
20+
where_clauses.push(format!("{} IS NOT NULL", key_counts));
21+
}
22+
if let Some(after) = after {
23+
params.push(after);
24+
where_clauses.push(format!("created > {}", params.len()));
25+
}
26+
27+
let event_aggregates = pool
28+
.run(move |connection| {
29+
async move {
30+
let where_clause = if !where_clauses.is_empty() {
31+
format!("WHERE {}", where_clauses.join(" AND "))
32+
} else {
33+
"".to_string()
34+
};
35+
let statement = format!("SELECT channel_id, created, events FROM event_aggregates {} ORDER BY created DESC LIMIT {}", where_clause, limit);
36+
match connection.prepare(&statement).await {
37+
Ok(stmt) => {
38+
match connection.query(&stmt, params.as_slice()).await {
39+
Ok(rows) => {
40+
let event_aggregates = rows.iter().map(EventAggregate::from).collect();
41+
42+
Ok((event_aggregates, connection))
43+
},
44+
Err(e) => Err((e, connection)),
45+
}
46+
},
47+
Err(e) => Err((e, connection)),
48+
}
49+
}
50+
})
51+
.await?;
52+
53+
Ok(event_aggregates)
54+
}

0 commit comments

Comments
 (0)