Skip to content

Commit d175733

Browse files
committed
update: insert event aggregate total
1 parent bc79147 commit d175733

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

sentry/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ regex = "1"
2323
redis = { version = "0.14", features = ["tokio-rt-core"] }
2424
bb8 = { git = "https://github.com/khuey/bb8" }
2525
bb8-postgres = { git = "https://github.com/khuey/bb8", features = ["with-chrono-0_4", "with-serde_json-1"] }
26+
postgres-types = { version = "0.1.0", features = ["derive"] }
27+
2628
# Migrations
2729
migrant_lib = { version = "0.27", features = ["d-postgres"] }
2830
# Logger

sentry/migrations/20190806011140_initial-tables/up.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ CREATE TABLE event_aggregates
3030
channel_id VARCHAR(66) NOT NULL REFERENCES channels (id) ON DELETE RESTRICT,
3131
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
3232
event_type VARCHAR(255) NOT NULL,
33-
earner VARCHAR(255) NOT NULL,
33+
earner VARCHAR(255) ,
3434
event_counts VARCHAR NOT NULL,
3535
event_payouts VARCHAR NOT NULL,
3636
);
3737

3838
CREATE INDEX idx_event_aggregates_created ON event_aggregates (created);
39+
CREATE INDEX idx_event_aggregates_channel ON event_aggregates (created);

sentry/src/db/event_aggregate.rs

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use crate::db::DbPool;
22
use bb8::RunError;
3-
use bb8_postgres::tokio_postgres::types::ToSql;
3+
// use bb8_postgres::tokio_postgres::types::{ToSql, FromSql};
44
use chrono::{DateTime, Utc};
55
use primitives::sentry::EventAggregate;
66
use primitives::{ChannelId, ValidatorId};
7+
use primitives::BigNum;
8+
use std::ops::Add;
9+
use postgres_types::{ToSql, FromSql};
710

811
pub async fn list_event_aggregates(
912
pool: &DbPool,
@@ -53,6 +56,16 @@ pub async fn list_event_aggregates(
5356
Ok(event_aggregates)
5457
}
5558

59+
#[derive(Debug, ToSql, FromSql)]
60+
struct EventData {
61+
id: String,
62+
event_type: String,
63+
earner: Option<String>,
64+
event_count: String,
65+
event_payout: String,
66+
}
67+
68+
5669
pub async fn insert_event_aggregate(
5770
pool: &DbPool,
5871
channel_id: &ChannelId,
@@ -62,21 +75,29 @@ pub async fn insert_event_aggregate(
6275
let mut index = 0;
6376
let id = channel_id.to_string();
6477

65-
let mut data: Vec<String> = Vec::new();
78+
let mut data: Vec<EventData> = Vec::new();
6679

6780
for (event_type, aggr) in &event.events {
6881
if let Some(event_counts) = &aggr.event_counts {
69-
for (earner, value) in event_counts {
70-
let event_count = value.to_string();
71-
let event_payout = aggr.event_payouts[earner].to_string();
72-
82+
let mut total_event_counts: BigNum = 0.into();
83+
let mut total_event_payouts: BigNum = 0.into();
84+
for (earner, event_count) in event_counts {
85+
let event_payout = aggr.event_payouts[earner].clone();
7386
data.extend(vec![
74-
id.clone(),
75-
event_type.clone(),
76-
earner.clone(),
77-
event_count,
78-
event_payout,
87+
EventData {
88+
id: id.clone(),
89+
event_type: event_type.clone(),
90+
earner: Some(earner.clone()),
91+
event_count: event_count.to_string(),
92+
event_payout: event_payout.to_string(),
93+
}
94+
7995
]);
96+
97+
// total sum
98+
total_event_counts = event_count.add(&total_event_counts);
99+
total_event_payouts = total_event_payouts.add(event_payout);
100+
80101
//
81102
// this is a work around for bulk inserts
82103
// rust-postgres does not have native support for bulk inserts
@@ -95,6 +116,28 @@ pub async fn insert_event_aggregate(
95116
));
96117
index += 5;
97118
}
119+
// extend with
120+
data.extend(vec![
121+
EventData {
122+
id: id.clone(),
123+
event_type: event_type.clone(),
124+
earner: None,
125+
event_count: total_event_counts.to_string(),
126+
event_payout: total_event_payouts.to_string(),
127+
}
128+
]);
129+
130+
values.push(format!(
131+
"(${}, ${}, ${}, ${}, ${})",
132+
index + 1,
133+
index + 2,
134+
index + 3,
135+
index + 4,
136+
index + 5
137+
));
138+
index += 5;
139+
140+
98141
}
99142
}
100143

0 commit comments

Comments
 (0)