| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
use chrono::{DateTime, Utc}; |
| 30 |
use sqlx::PgPool; |
| 31 |
|
| 32 |
use super::id_types::{EmailSendId, ListId, UserId}; |
| 33 |
use crate::error::Result; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 38 |
pub enum SendKind { |
| 39 |
|
| 40 |
Broadcast, |
| 41 |
|
| 42 |
Announcement, |
| 43 |
} |
| 44 |
|
| 45 |
impl SendKind { |
| 46 |
|
| 47 |
pub const fn as_str(self) -> &'static str { |
| 48 |
match self { |
| 49 |
Self::Broadcast => "broadcast", |
| 50 |
Self::Announcement => "announcement", |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 57 |
pub enum IncidentKind { |
| 58 |
|
| 59 |
HardBounce, |
| 60 |
|
| 61 |
Complaint, |
| 62 |
} |
| 63 |
|
| 64 |
impl IncidentKind { |
| 65 |
|
| 66 |
pub const fn as_str(self) -> &'static str { |
| 67 |
match self { |
| 68 |
Self::HardBounce => "hard_bounce", |
| 69 |
Self::Complaint => "complaint", |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 81 |
pub struct Rate { |
| 82 |
|
| 83 |
pub sent: i64, |
| 84 |
|
| 85 |
pub complaints: i64, |
| 86 |
|
| 87 |
pub bounces: i64, |
| 88 |
} |
| 89 |
|
| 90 |
impl Rate { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[must_use] |
| 97 |
pub fn complaint_rate(&self) -> f64 { |
| 98 |
if self.sent <= 0 { |
| 99 |
return 0.0; |
| 100 |
} |
| 101 |
#[allow(clippy::cast_precision_loss)] |
| 102 |
{ |
| 103 |
self.complaints as f64 / self.sent as f64 |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#[must_use] |
| 110 |
pub fn bounce_rate(&self) -> f64 { |
| 111 |
if self.sent <= 0 { |
| 112 |
return 0.0; |
| 113 |
} |
| 114 |
#[allow(clippy::cast_precision_loss)] |
| 115 |
{ |
| 116 |
self.bounces as f64 / self.sent as f64 |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#[tracing::instrument(skip_all)] |
| 128 |
pub async fn record_send( |
| 129 |
pool: &PgPool, |
| 130 |
creator_id: UserId, |
| 131 |
list_id: Option<ListId>, |
| 132 |
kind: SendKind, |
| 133 |
recipients: i64, |
| 134 |
) -> Result<EmailSendId> { |
| 135 |
let id = sqlx::query_scalar::<_, EmailSendId>( |
| 136 |
"INSERT INTO email_sends (creator_id, list_id, kind, recipients) \ |
| 137 |
VALUES ($1, $2, $3, $4) RETURNING id", |
| 138 |
) |
| 139 |
.bind(creator_id) |
| 140 |
.bind(list_id) |
| 141 |
.bind(kind.as_str()) |
| 142 |
.bind(recipients) |
| 143 |
.fetch_one(pool) |
| 144 |
.await?; |
| 145 |
Ok(id) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 152 |
pub struct Attribution { |
| 153 |
|
| 154 |
pub creator_id: UserId, |
| 155 |
|
| 156 |
pub list_id: Option<ListId>, |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
#[tracing::instrument(skip_all)] |
| 168 |
pub async fn record_incident( |
| 169 |
pool: &PgPool, |
| 170 |
send_id: Option<EmailSendId>, |
| 171 |
email: &str, |
| 172 |
kind: IncidentKind, |
| 173 |
) -> Result<Option<Attribution>> { |
| 174 |
let row = sqlx::query_as::<_, (Option<UserId>, Option<ListId>)>( |
| 175 |
"INSERT INTO email_incidents (send_id, creator_id, list_id, email, kind) \ |
| 176 |
SELECT s.id, s.creator_id, s.list_id, LOWER($2), $3 \ |
| 177 |
FROM email_sends s WHERE s.id = $1 \ |
| 178 |
UNION ALL \ |
| 179 |
SELECT NULL, NULL, NULL, LOWER($2), $3 \ |
| 180 |
WHERE NOT EXISTS (SELECT 1 FROM email_sends WHERE id = $1) \ |
| 181 |
LIMIT 1 \ |
| 182 |
RETURNING creator_id, list_id", |
| 183 |
) |
| 184 |
.bind(send_id) |
| 185 |
.bind(email) |
| 186 |
.bind(kind.as_str()) |
| 187 |
.fetch_optional(pool) |
| 188 |
.await?; |
| 189 |
|
| 190 |
Ok(row.and_then(|(creator_id, list_id)| { |
| 191 |
creator_id.map(|creator_id| Attribution { |
| 192 |
creator_id, |
| 193 |
list_id, |
| 194 |
}) |
| 195 |
})) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
#[tracing::instrument(skip_all)] |
| 200 |
pub async fn creator_rate(pool: &PgPool, creator_id: UserId, since: DateTime<Utc>) -> Result<Rate> { |
| 201 |
rate_from(pool, "creator_id", creator_id.into(), since).await |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
#[tracing::instrument(skip_all)] |
| 206 |
pub async fn list_rate(pool: &PgPool, list_id: ListId, since: DateTime<Utc>) -> Result<Rate> { |
| 207 |
rate_from(pool, "list_id", list_id.into(), since).await |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
async fn rate_from( |
| 217 |
pool: &PgPool, |
| 218 |
column: &'static str, |
| 219 |
id: uuid::Uuid, |
| 220 |
since: DateTime<Utc>, |
| 221 |
) -> Result<Rate> { |
| 222 |
let sent = sqlx::query_scalar::<_, Option<i64>>(&format!( |
| 223 |
"SELECT SUM(recipients) FROM email_sends WHERE {column} = $1 AND created_at >= $2" |
| 224 |
)) |
| 225 |
.bind(id) |
| 226 |
.bind(since) |
| 227 |
.fetch_one(pool) |
| 228 |
.await? |
| 229 |
.unwrap_or(0); |
| 230 |
|
| 231 |
let row = sqlx::query_as::<_, (i64, i64)>(&format!( |
| 232 |
"SELECT \ |
| 233 |
COUNT(*) FILTER (WHERE kind = 'complaint'), \ |
| 234 |
COUNT(*) FILTER (WHERE kind = 'hard_bounce') \ |
| 235 |
FROM email_incidents WHERE {column} = $1 AND created_at >= $2" |
| 236 |
)) |
| 237 |
.bind(id) |
| 238 |
.bind(since) |
| 239 |
.fetch_one(pool) |
| 240 |
.await?; |
| 241 |
|
| 242 |
Ok(Rate { |
| 243 |
sent, |
| 244 |
complaints: row.0, |
| 245 |
bounces: row.1, |
| 246 |
}) |
| 247 |
} |
| 248 |
|
| 249 |
#[cfg(test)] |
| 250 |
mod tests { |
| 251 |
use super::*; |
| 252 |
|
| 253 |
#[test] |
| 254 |
fn a_rate_over_nothing_sent_is_unmeasured_rather_than_perfect() { |
| 255 |
let nothing = Rate::default(); |
| 256 |
assert!((nothing.complaint_rate() - 0.0).abs() < f64::EPSILON); |
| 257 |
assert!((nothing.bounce_rate() - 0.0).abs() < f64::EPSILON); |
| 258 |
assert_eq!(nothing.sent, 0); |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn a_rate_is_the_fraction_of_what_went_out() { |
| 263 |
let measured = Rate { |
| 264 |
sent: 4000, |
| 265 |
complaints: 3, |
| 266 |
bounces: 12, |
| 267 |
}; |
| 268 |
assert!((measured.complaint_rate() - 0.00075).abs() < 1e-9); |
| 269 |
assert!((measured.bounce_rate() - 0.003).abs() < 1e-9); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
#[test] |
| 275 |
fn the_stored_tokens_are_the_ones_the_queries_filter_on() { |
| 276 |
assert_eq!(IncidentKind::Complaint.as_str(), "complaint"); |
| 277 |
assert_eq!(IncidentKind::HardBounce.as_str(), "hard_bounce"); |
| 278 |
assert_eq!(SendKind::Broadcast.as_str(), "broadcast"); |
| 279 |
assert_eq!(SendKind::Announcement.as_str(), "announcement"); |
| 280 |
} |
| 281 |
} |
| 282 |
|