1
1
use crate :: db:: DbPool ;
2
2
use bb8:: RunError ;
3
- use bb8_postgres:: tokio_postgres:: types:: ToSql ;
3
+ // use bb8_postgres::tokio_postgres::types::{ ToSql, FromSql} ;
4
4
use chrono:: { DateTime , Utc } ;
5
5
use primitives:: sentry:: EventAggregate ;
6
6
use primitives:: { ChannelId , ValidatorId } ;
7
+ use primitives:: BigNum ;
8
+ use std:: ops:: Add ;
9
+ use postgres_types:: { ToSql , FromSql } ;
7
10
8
11
pub async fn list_event_aggregates (
9
12
pool : & DbPool ,
@@ -53,6 +56,16 @@ pub async fn list_event_aggregates(
53
56
Ok ( event_aggregates)
54
57
}
55
58
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
+
56
69
pub async fn insert_event_aggregate (
57
70
pool : & DbPool ,
58
71
channel_id : & ChannelId ,
@@ -62,21 +75,29 @@ pub async fn insert_event_aggregate(
62
75
let mut index = 0 ;
63
76
let id = channel_id. to_string ( ) ;
64
77
65
- let mut data: Vec < String > = Vec :: new ( ) ;
78
+ let mut data: Vec < EventData > = Vec :: new ( ) ;
66
79
67
80
for ( event_type, aggr) in & event. events {
68
81
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 ( ) ;
73
86
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
+
79
95
] ) ;
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
+
80
101
//
81
102
// this is a work around for bulk inserts
82
103
// rust-postgres does not have native support for bulk inserts
@@ -95,6 +116,28 @@ pub async fn insert_event_aggregate(
95
116
) ) ;
96
117
index += 5 ;
97
118
}
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
+
98
141
}
99
142
}
100
143
0 commit comments