Skip to content

Commit 87b8d73

Browse files
committed
fix: db/event_aggregates params, store error logging
1 parent 0219e45 commit 87b8d73

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

primitives/src/big_num.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,4 @@ mod test {
283283
let expected: BigNum = 11.into();
284284
assert_eq!(expected, &big_num * &ratio);
285285
}
286-
}
286+
}

sentry/src/db/event_aggregate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ pub async fn list_event_aggregates(
1919
) -> Result<Vec<EventAggregate>, RunError<bb8_postgres::tokio_postgres::Error>> {
2020
let (mut where_clauses, mut params) = (vec![], Vec::<&(dyn ToSql + Sync)>::new());
2121
let id = channel_id.to_string();
22-
where_clauses.push(format!("channel_id = '{}'", id));
22+
params.push(&id);
23+
where_clauses.push(format!("channel_id = ${}", params.len()));
2324

2425
if let Some(from) = from {
2526
where_clauses.push(format!("earner = '{}'", from.to_string()));
26-
where_clauses.push("event_type = 'IMPRESSION'".to_string());
27+
params.push(&"IMPRESSION");
28+
where_clauses.push(format!("event_type = ${}", params.len()));
2729
} else {
2830
where_clauses.push("earner is NOT NULL".to_string());
2931
}
@@ -52,13 +54,13 @@ pub async fn list_event_aggregates(
5254
'eventCounts',
5355
jsonb_object_agg(
5456
jsonb_build_object(
55-
earner, count::text
57+
earner, count
5658
)
5759
),
5860
'eventPayouts',
5961
jsonb_object_agg(
6062
jsonb_build_object(
61-
earner, payout::text
63+
earner, payout
6264
)
6365
)
6466
)

sentry/src/event_aggregator.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::access::check_access;
22
use crate::db::event_aggregate::insert_event_aggregate;
3+
use crate::db::get_channel_by_id;
34
use crate::db::DbPool;
45
use crate::event_reducer;
56
use crate::Application;
@@ -15,21 +16,18 @@ use std::collections::HashMap;
1516
use std::sync::Arc;
1617
use std::time::Duration;
1718
use tokio::time::delay_for;
18-
use crate::db::{get_channel_by_id};
19-
2019

2120
#[derive(Debug)]
2221
struct Record {
2322
channel: Channel,
24-
aggregate: EventAggregate
23+
aggregate: EventAggregate,
2524
}
2625

27-
type Recorder = Arc<RwLock<HashMap<ChannelId, Record>>>;
26+
type Recorder = Arc<RwLock<HashMap<ChannelId, Record>>>;
2827

2928
#[derive(Default, Clone)]
3029
pub struct EventAggregator {
31-
// aggregate: Aggregate,
32-
recorder: Recorder
30+
recorder: Recorder,
3331
}
3432

3533
pub fn new_aggr(channel_id: &ChannelId) -> EventAggregate {
@@ -45,12 +43,12 @@ async fn store(db: &DbPool, channel_id: &ChannelId, logger: &Logger, recorder: R
4543
let record: Option<&Record> = channel_recorder.get(channel_id);
4644
if let Some(data) = record {
4745
if let Err(e) = insert_event_aggregate(&db, &channel_id, &data.aggregate).await {
48-
error!(&logger, "{}", e; "event_aggregator" => "store");
46+
error!(&logger, "{}", e; "module" => "event_aggregator", "in" => "store");
4947
} else {
5048
// reset aggr record
5149
let record = Record {
5250
channel: data.channel.to_owned(),
53-
aggregate: new_aggr(&channel_id)
51+
aggregate: new_aggr(&channel_id),
5452
};
5553
channel_recorder.insert(channel_id.to_owned(), record);
5654
};
@@ -76,24 +74,24 @@ impl EventAggregator {
7674
None => {
7775
// fetch channel
7876
let channel = get_channel_by_id(&app.pool, &channel_id)
79-
.await?
80-
.ok_or_else(|| ResponseError::NotFound)?;
77+
.await?
78+
.ok_or_else(|| ResponseError::NotFound)?;
8179

8280
let withdraw_period_start = channel.spec.withdraw_period_start;
8381
let channel_id = channel.id;
8482
let record = Record {
8583
channel,
86-
aggregate: new_aggr(&channel_id)
84+
aggregate: new_aggr(&channel_id),
8785
};
8886

8987
// insert into
9088
channel_recorder.insert(channel_id.to_owned(), record);
9189

92-
//
90+
//
9391
// spawn async task that persists
9492
// the channel events to database
95-
let recorder = recorder.clone();
9693
if aggr_throttle > 0 {
94+
let recorder = recorder.clone();
9795
tokio::spawn(async move {
9896
loop {
9997
// break loop if the

0 commit comments

Comments
 (0)