Skip to content

Commit

Permalink
Testrun stores gw identity key instead of gw pk
Browse files Browse the repository at this point in the history
  • Loading branch information
dynco-nym committed Oct 30, 2024
1 parent acb7be7 commit dc72c1e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions common/models/src/ns_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TestrunAssignment {
/// has nothing to do with GW identity key. This is PK from `gateways` table
pub testrun_id: i64,
pub gateway_pk_id: i64,
pub gateway_identity_key: String,
}
2 changes: 1 addition & 1 deletion nym-node-status-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[package]
name = "nym-node-status-api"
version = "0.1.3"
version = "0.1.4"
authors.workspace = true
repository.workspace = true
homepage.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion nym-node-status-api/migrations/000_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ CREATE TABLE
CREATE TABLE testruns
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
gateway_id INTEGER,
gateway_id INTEGER NOT NULL,
status INTEGER NOT NULL, -- 0=pending, 1=in-progress, 2=complete
timestamp_utc INTEGER NOT NULL,
ip_address VARCHAR NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions nym-node-status-api/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ pub(crate) mod gateway {
}
}

#[allow(dead_code)] // not dead code, this is SQL data model
#[derive(Debug, Clone)]
pub struct TestRunDto {
pub id: i64,
Expand Down
30 changes: 25 additions & 5 deletions nym-node-status-api/src/db/queries/testruns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ pub(crate) async fn update_testruns_older_than(db: &DbPool, age: Duration) -> an
pub(crate) async fn get_oldest_testrun_and_make_it_pending(
conn: &mut PoolConnection<Sqlite>,
) -> anyhow::Result<Option<TestrunAssignment>> {
let assignment = sqlx::query_as!(
TestrunAssignment,
// find & mark as "In progress" in the same transaction to avoid race conditions
let returning = sqlx::query!(
r#"UPDATE testruns
SET status = ?
WHERE rowid =
Expand All @@ -85,16 +85,36 @@ pub(crate) async fn get_oldest_testrun_and_make_it_pending(
LIMIT 1
)
RETURNING
id as "testrun_id!",
gateway_id as "gateway_pk_id!"
id as "id!",
gateway_id
"#,
TestRunStatus::InProgress as i64,
TestRunStatus::Queued as i64,
)
.fetch_optional(conn.as_mut())
.await?;

Ok(assignment)
if let Some(testrun) = returning {
let gw_identity = sqlx::query!(
r#"
SELECT
id,
gateway_identity_key
FROM gateways
WHERE id = ?
LIMIT 1"#,
testrun.gateway_id
)
.fetch_one(conn.as_mut())
.await?;

return Ok(Some(TestrunAssignment {
testrun_id: testrun.id,
gateway_identity_key: gw_identity.gateway_identity_key,
}));
} else {
return Ok(None);
}
}

pub(crate) async fn update_testrun_status(
Expand Down
10 changes: 1 addition & 9 deletions nym-node-status-api/src/http/api/testruns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,10 @@ async fn request_testrun(State(state): State<AppState>) -> HttpResult<Json<Testr
return match db::queries::testruns::get_oldest_testrun_and_make_it_pending(&mut conn).await {
Ok(res) => {
if let Some(testrun) = res {
let gw_identity =
db::queries::select_gateway_identity(&mut conn, testrun.gateway_pk_id)
.await
.map_err(|_| {
// should never happen:
HttpError::internal_with_logging("No gateway found for testrun")
})?;
// TODO dz consider adding a column to testruns table with agent's public key
tracing::debug!(
"🏃‍ Assigned testrun row_id {} gateway {} to agent",
&testrun.testrun_id,
gw_identity
testrun.gateway_identity_key
);
Ok(Json(testrun))
} else {
Expand Down
10 changes: 0 additions & 10 deletions nym-node-status-api/src/http/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::db::models::TestRunDto;
use nym_node_requests::api::v1::node::models::NodeDescription;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
Expand Down Expand Up @@ -75,12 +74,3 @@ pub(crate) struct SummaryHistory {
pub value_json: serde_json::Value,
pub timestamp_utc: String,
}

impl From<TestRunDto> for TestrunAssignment {
fn from(value: TestRunDto) -> Self {
Self {
gateway_pk_id: value.gateway_id,
testrun_id: value.id,
}
}
}

0 comments on commit dc72c1e

Please sign in to comment.