Testrun stores gw identity key instead of gw pk

This commit is contained in:
dynco-nym
2024-10-30 03:15:11 +01:00
parent acb7be7ea2
commit dc72c1eadd
8 changed files with 31 additions and 29 deletions
Generated
+1 -1
View File
@@ -6591,7 +6591,7 @@ dependencies = [
[[package]]
name = "nym-node-status-api"
version = "0.1.3"
version = "0.1.4"
dependencies = [
"anyhow",
"axum 0.7.7",
+1 -2
View File
@@ -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,
}
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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,
+1
View File
@@ -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,
+25 -5
View File
@@ -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 =
@@ -85,8 +85,8 @@ 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,
@@ -94,7 +94,27 @@ pub(crate) async fn get_oldest_testrun_and_make_it_pending(
.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(
+1 -9
View File
@@ -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 {
-10
View File
@@ -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;
@@ -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,
}
}
}