Skip to content

Commit

Permalink
Fix deletion and validated heartbeats stream
Browse files Browse the repository at this point in the history
  • Loading branch information
maplant committed Jul 25, 2023
1 parent 9c88a27 commit 45638d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions mobile_verifier/migrations/15_modeled_coverage.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREATE TABLE hex_coverage (
cbsd_id TEXT NOT NULL,
signal_level signal_level NOT NULL,
coverage_claim_time TIMESTAMPTZ NOT NULL,
inserted_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (uuid, hex)
);

Expand Down
31 changes: 22 additions & 9 deletions mobile_verifier/src/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ impl CoverageObject {
sqlx::query(
r#"
INSERT INTO hex_coverage
(uuid, hex, indoor, cbsd_id, signal_level, coverage_claim_time)
(uuid, hex, indoor, cbsd_id, signal_level, coverage_claim_time, inserted_at)
VALUES
($1, $2, $3, $4, $5, $6)
($1, $2, $3, $4, $5, $6, $7)
"#,
)
.bind(self.uuid)
Expand All @@ -197,6 +197,7 @@ impl CoverageObject {
.bind(&self.cbsd_id)
.bind(SignalLevel::from(hex.signal_level))
.bind(self.coverage_claim_time)
.bind(Utc::now())
.execute(&mut *transaction)
.await?;
}
Expand Down Expand Up @@ -327,13 +328,25 @@ impl CoveredHexStream for Pool<Postgres> {
.execute(self)
.await?;

// Is this a valid delete?
sqlx::query("DELETE FROM hex_coverage WHERE cbsd_id = $1 AND uuid != $2 AND coverage_claim_time < $3")
.bind(cbsd_id)
.bind(coverage_obj)
.bind(seniority.seniority_ts)
.execute(self)
.await?;
// Find the time of insertion for the currently in use coverage object
let current_inserted_at: DateTime<Utc> = sqlx::query_scalar(
"SELECT inserted_at FROM hex_coverage WHERE cbsd_id = $1 AND uuid = $2 LIMIT 1",
)
.bind(cbsd_id)
.bind(coverage_obj)
.fetch_one(self)
.await?;

// Delete any hex coverages that were inserted before the one we are currently using, as they are
// no longer useful.
sqlx::query(
"DELETE FROM hex_coverage WHERE cbsd_id = $1 AND uuid != $2 AND inserted_at < $3",
)
.bind(cbsd_id)
.bind(coverage_obj)
.bind(current_inserted_at)
.execute(self)
.await?;

Ok(
sqlx::query_as("SELECT * FROM hex_coverage WHERE cbsd_id = $1 AND uuid = $2")
Expand Down
2 changes: 2 additions & 0 deletions mobile_verifier/src/heartbeats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ impl HeartbeatReward {
SELECT MAX(t2.latest_timestamp)
FROM heartbeats t2
WHERE t2.cbsd_id = t1.cbsd_id
AND truncated_timestamp >= $1
AND truncated_timestamp < $2
)
)
SELECT
Expand Down

0 comments on commit 45638d5

Please sign in to comment.