Skip to content

Commit

Permalink
Add conflict for saving, distinct for getting
Browse files Browse the repository at this point in the history
If we get sent a new report in the same day, we always want to use the latest supplied number.
  • Loading branch information
michaeldjeffrey committed Dec 5, 2024
1 parent a8238c7 commit 18649e7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions mobile_verifier/src/unique_connections/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ pub async fn get(

let rows = sqlx::query_as::<_, UniqueConnections>(
r#"
SELECT hotspot_pubkey, unique_connections
SELECT DISTINCT ON(hotspot_pubkey, received_timestamp)
hotspot_pubkey, unique_connections
FROM unique_connections
WHERE received_timestamp >= $1 AND received_timestamp <= $2
ORDER BY received_timestamp DESC
ORDER BY hotspot_pubkey, received_timestamp ASC
"#,
)
.bind(reward_period.start)
Expand All @@ -41,13 +42,15 @@ pub async fn save(
txn: &mut Transaction<'_, Postgres>,
report: &UniqueConnectionsIngestReport,
) -> Result<(), sqlx::Error> {
// TODO: on conflict?
sqlx::query(
r#"
INSERT INTO unique_connections
(hotspot_pubkey, unique_connections, start_timestamp, end_timestamp, received_timestamp)
VALUES
($1, $2, $3, $4, $5)
ON CONFLICT
(hotspot_pubkey, received_timestamp)
DO NOTHING
"#,
)
.bind(report.report.pubkey.to_string())
Expand Down

0 comments on commit 18649e7

Please sign in to comment.