summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-11-29 14:37:57 +0100
committerSanto Cariotti <santo@dcariotti.me>2024-11-29 14:37:57 +0100
commitdce41a67f711005a859e909ca93710b93cab8d08 (patch)
tree4bf6c2ee980524292d07626e8bf43a52583b2d23
parent26d8aed5540677d539957dea16f95491499eff58 (diff)
Cache system to avoid duplicated alerts within 10 mins
-rw-r--r--src/graphql/types/alert.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/graphql/types/alert.rs b/src/graphql/types/alert.rs
index cce2bee..9b71188 100644
--- a/src/graphql/types/alert.rs
+++ b/src/graphql/types/alert.rs
@@ -180,6 +180,41 @@ pub mod mutations {
return Err(AppError::BadRequest("Polygon is not valid".to_string()).into());
}
+ // This is like a cache system to avoid multiple alerts for the same area within 10
+ // minutes of interval
+ if let Some(previous_alert) = client.query(
+ &format!(
+ "SELECT
+ id, user_id, extract(epoch from created_at)::double precision as created_at,
+ ST_AsText(area) as area,
+ ST_AsText(ST_Buffer(area::geography, 1000)) as area_level2,
+ ST_AsText(ST_Buffer(area::geography, 2000)) as area_level3,
+ text1, text2, text3,
+ reached_users
+ FROM alerts WHERE area = {} AND created_at >= NOW() - INTERVAL '10 MINUTE'",
+ polygon
+ ),
+ &[]
+ ).await?
+ .iter()
+ .map(|row| Alert {
+ id: row.get("id"),
+ user_id: row.get("user_id"),
+ created_at: row.get::<_, f64>("created_at") as i64,
+ area: row.get("area"),
+ area_level2: row.get("area_level2"),
+ area_level3: row.get("area_level3"),
+ text1: row.get("text1"),
+ text2: row.get("text2"),
+ text3: row.get("text3"),
+ reached_users: row.get("reached_users"),
+ })
+ .collect::<Vec<Alert>>()
+ .first()
+ .cloned() {
+ return Ok(previous_alert);
+ }
+
let insert_query = format!(
"INSERT INTO alerts (user_id, area, text1, text2, text3)
VALUES($1, {}, $2, $3, $4)