Skip to content

Commit

Permalink
WIP to insert cabooses (does not compile)
Browse files Browse the repository at this point in the history
  • Loading branch information
davepacheco committed Oct 6, 2023
1 parent 1be1e71 commit e9206be
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 20 deletions.
19 changes: 19 additions & 0 deletions nexus/db-model/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ impl_enum_type!(
RotSlotB => b"rot_slot_B"
);

impl From<nexus_types::inventory::CabooseWhich> for CabooseWhich {
fn from(c: nexus_types::inventory::CabooseWhich) -> Self {
match c {
nexus_types::inventory::CabooseWhich::SpSlot0 => {
CabooseWhich::SpSlot0
}
nexus_types::inventory::CabooseWhich::SpSlot1 => {
CabooseWhich::SpSlot1
}
nexus_types::inventory::CabooseWhich::RotSlotA => {
CabooseWhich::RotSlotA
}
nexus_types::inventory::CabooseWhich::RotSlotB => {
CabooseWhich::RotSlotB
}
}
}
}

#[derive(Queryable, Insertable, Clone, Debug, Selectable)]
#[diesel(table_name = inv_collection)]
pub struct InvCollection {
Expand Down
72 changes: 70 additions & 2 deletions nexus/db-queries/src/db/datastore/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use diesel::sql_types::Nullable;
use diesel::ExpressionMethods;
use diesel::IntoSql;
use diesel::QueryDsl;
use nexus_db_model::CabooseWhichEnum;
use nexus_db_model::HwBaseboardId;
use nexus_db_model::HwPowerState;
use nexus_db_model::HwPowerStateEnum;
Expand Down Expand Up @@ -268,8 +269,75 @@ impl DataStore {
}
}

// XXX-dap Insert the "found cabooses" information.
{}
// Insert rows for the cabooses that we found. Like service
// processors and roots of trust, we do this using INSERT INTO ...
// SELECT.
// XXX-dap I want a block similar to the above, but it's not clear
// how to extend it to two different dependent tables. I can do it
// with a CTE:
// https://www.db-fiddle.com/f/aaegKd3RXqaqyuxBLXSxaJ/0
{
use db::schema::hw_baseboard_id::dsl as baseboard_dsl;
use db::schema::inv_caboose::dsl as inv_caboose_dsl;
use db::schema::sw_caboose::dsl as sw_caboose_dsl;

for (which, tree) in &collection.cabooses_found {
let db_which = nexus_db_model::CabooseWhich::from(*which);
for (baseboard_id, found_caboose) in tree {
// XXX-dap
todo!();
// let selection = db::schema::hw_baseboard_id::table
// .select((
// collection_id.into_sql::<diesel::sql_types::Uuid>(),
// baseboard_dsl::id,
// found_caboose.time_collected
// .into_sql::<diesel::sql_types::Timestamptz>(),
// found_caboose.source
// .clone()
// .into_sql::<diesel::sql_types::Text>(),
// db_which.into_sql::<CabooseWhichEnum>(),
// ))
// .filter(
// baseboard_dsl::part_number
// .eq(baseboard_id.part_number.clone()),
// )
// .filter(
// baseboard_dsl::serial_number
// .eq(baseboard_id.serial_number.clone()),
// )
// .left_join(db::schema::sw_caboose::table
// .select(sw_caboose_dsl::id)
// .filter(sw_caboose_dsl::board.eq(found_caboose.board))
// .filter(sw_caboose_dsl::git_commit.eq(found_caboose.git_commit))
// .filter(sw_caboose_dsl::name.eq(found_caboose.name))
// .filter(sw_caboose_dsl::version.eq(found_caboose.version)));

// let _ = diesel::insert_into(
// db::schema::inv_root_of_trust::table,
// )
// .values(selection)
// .into_columns((
// inv_caboose_dsl::inv_collection_id,
// inv_caboose_dsl::hw_baseboard_id,
// inv_caboose_dsl::time_collected,
// inv_caboose_dsl::source,
// inv_caboose_dsl::which,
// inv_caboose_dsl::sw_caboose_id,
// ))
// .execute_async(&conn)
// .await
// .map_err(|e| {
// TransactionError::CustomError(
// public_error_from_diesel_pool(
// e.into(),
// ErrorHandler::Server,
// )
// .internal_context("inserting service processor"),
// )
// });
}
}
}

// Finally, insert the list of errors.
{
Expand Down
23 changes: 20 additions & 3 deletions nexus/inventory/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use nexus_types::inventory::CabooseWhich;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::sync::Arc;
use gateway_client::types::SpComponentCaboose;

// XXX-dap add rack id

Expand Down Expand Up @@ -141,8 +142,12 @@ impl CollectionBuilder {
&mut self,
baseboard: &BaseboardId,
which: CabooseWhich,
caboose: Caboose,
source: &str,
caboose: SpComponentCaboose,
) -> Result<(), anyhow::Error> {
// XXX-dap I messed around with the Caboose structure in nexus/types to
// include time_collected, source, etc. and now I need to unpack what
// needs to be fixed here.
let caboose = Self::enum_item(&mut self.cabooses, caboose);
let (baseboard, _) =
self.sps.get_key_value(baseboard).ok_or_else(|| {
Expand All @@ -153,8 +158,20 @@ impl CollectionBuilder {
})?;
let by_id =
self.cabooses_found.entry(which).or_insert_with(|| BTreeMap::new());
if let Some(previous) = by_id.insert(baseboard.clone(), caboose.clone())
{
if let Some(previous) = by_id.insert(
baseboard.clone(),
Caboose {
time_collected: Utc::now(),
source: source.to_owned(),
board: caboose.board,
git_commit: caboose.git_commit,
name: caboose.name,
// XXX-dap TODO-doc
version: caboose
.version
.unwrap_or_else(|| String::from("unspecified")),
},
) {
let error = if *previous == *caboose {
anyhow!("reported multiple times (same value)",)
} else {
Expand Down
2 changes: 1 addition & 1 deletion nexus/inventory/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Collector {
self.in_progress.found_error(error);
continue;
}
Ok(response) => Caboose::from(response.into_inner()),
Ok(response) => response.into_inner(),
};
self.in_progress.found_sp_caboose(
&baseboard_id,
Expand Down
17 changes: 3 additions & 14 deletions nexus/types/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,15 @@ pub struct BaseboardId {

#[derive(Clone, Debug, Ord, Eq, PartialOrd, PartialEq)]
pub struct Caboose {
pub time_collected: DateTime<Utc>,
pub source: String,

pub board: String,
pub git_commit: String,
pub name: String,
pub version: String,
}

impl From<gateway_client::types::SpComponentCaboose> for Caboose {
fn from(c: gateway_client::types::SpComponentCaboose) -> Self {
Caboose {
board: c.board,
git_commit: c.git_commit,
name: c.name,
// The MGS API uses an `Option` here because old SP versions did not
// supply it. But modern SP versions do. So we should never hit
// this `unwrap_or()`.
version: c.version.unwrap_or(String::from("<unspecified>")),
}
}
}

#[derive(Clone, Debug, Ord, Eq, PartialOrd, PartialEq)]
pub struct ServiceProcessor {
pub time_collected: DateTime<Utc>,
Expand Down

0 comments on commit e9206be

Please sign in to comment.