From 47fbee41cc20864a12463c774ef926b1e90cea7b Mon Sep 17 00:00:00 2001 From: James Brunskill Date: Thu, 8 Feb 2024 17:04:25 +1300 Subject: [PATCH 1/2] Create a temporary breach record if a breach id is in a temp log, but not in db yet. --- .../types/src/types/temperature_breach.rs | 6 +-- .../server/src/cold_chain/temperature_log.rs | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/server/graphql/types/src/types/temperature_breach.rs b/server/graphql/types/src/types/temperature_breach.rs index f8868cac7b..ce339183e6 100644 --- a/server/graphql/types/src/types/temperature_breach.rs +++ b/server/graphql/types/src/types/temperature_breach.rs @@ -28,6 +28,7 @@ pub enum TemperatureBreachNodeType { ColdCumulative, HotConsecutive, HotCumulative, + Excursion, } #[derive(Enum, Copy, Clone, PartialEq, Eq)] @@ -169,9 +170,7 @@ impl TemperatureBreachNodeType { from::ColdCumulative => to::ColdCumulative, from::HotConsecutive => to::HotConsecutive, from::HotCumulative => to::HotCumulative, - from::Excursion => { - panic!("Excursion is not a valid type for TemperatureBreachNodeType") - } + from::Excursion => to::Excursion, } } @@ -184,6 +183,7 @@ impl TemperatureBreachNodeType { from::ColdCumulative => to::ColdCumulative, from::HotConsecutive => to::HotConsecutive, from::HotCumulative => to::HotCumulative, + from::Excursion => to::Excursion, } } } diff --git a/server/server/src/cold_chain/temperature_log.rs b/server/server/src/cold_chain/temperature_log.rs index 51cae82c69..30c7be98ee 100644 --- a/server/server/src/cold_chain/temperature_log.rs +++ b/server/server/src/cold_chain/temperature_log.rs @@ -9,6 +9,7 @@ use log::error; use mime_guess::mime; use openssl::error; use repository::RepositoryError; +use service::temperature_breach::insert::InsertTemperatureBreach; use service::{ auth_data::AuthData, service_provider::{ServiceContext, ServiceProvider}, @@ -114,6 +115,51 @@ fn upsert_temperature_log( let datetime = NaiveDateTime::from_timestamp_opt(log.unix_timestamp, 0) .context(format!("Unable to parse timestamp {}", log.unix_timestamp))?; + // If we have a temperature log with a breachid, we need to make sure the breach exists first, if it doesn't we create a temporary record so we don't loose data. + match &log.temperature_breach_id { + Some(breach_id) => { + let breach = service_provider + .temperature_breach_service + .get_temperature_breach(&ctx, breach_id.clone()); + + match breach { + Ok(_) => {} + Err(SingleRecordError::NotFound(_)) => { + // Create a temperature breach record as it doesn't exist yet + let breach = InsertTemperatureBreach { + id: breach_id.clone(), + sensor_id: log.sensor_id.clone(), + threshold_duration_milliseconds: 0, + duration_milliseconds: 0, + r#type: repository::TemperatureBreachRowType::HotConsecutive, + start_datetime: NaiveDateTime::from_timestamp_millis(log.unix_timestamp) + .unwrap_or_default(), + end_datetime: None, + unacknowledged: true, + threshold_minimum: 0.0, + threshold_maximum: 0.0, + comment: None, + location_id: None, + }; + service_provider + .temperature_breach_service + .insert_temperature_breach(&ctx, breach) + .map_err(|e| { + anyhow::anyhow!("Unable to insert temperature breach {:?}", e) + })?; + } + Err(e) => { + return Err(anyhow::anyhow!( + "Unable to get temperature breach for id '{}'. {:#?}", + breach_id.clone(), + e + )); + } + } + } + None => {} + }; + let result = match service.get_temperature_log(&ctx, id.clone()) { Ok(_) => { let log = UpdateTemperatureLog { From 29dc60ae1b1b46277244e57ea60ba1ac0aa11d7f Mon Sep 17 00:00:00 2001 From: James Brunskill Date: Thu, 8 Feb 2024 17:15:32 +1300 Subject: [PATCH 2/2] Roll back excursion change --- server/graphql/types/src/types/temperature_breach.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/graphql/types/src/types/temperature_breach.rs b/server/graphql/types/src/types/temperature_breach.rs index ce339183e6..f8868cac7b 100644 --- a/server/graphql/types/src/types/temperature_breach.rs +++ b/server/graphql/types/src/types/temperature_breach.rs @@ -28,7 +28,6 @@ pub enum TemperatureBreachNodeType { ColdCumulative, HotConsecutive, HotCumulative, - Excursion, } #[derive(Enum, Copy, Clone, PartialEq, Eq)] @@ -170,7 +169,9 @@ impl TemperatureBreachNodeType { from::ColdCumulative => to::ColdCumulative, from::HotConsecutive => to::HotConsecutive, from::HotCumulative => to::HotCumulative, - from::Excursion => to::Excursion, + from::Excursion => { + panic!("Excursion is not a valid type for TemperatureBreachNodeType") + } } } @@ -183,7 +184,6 @@ impl TemperatureBreachNodeType { from::ColdCumulative => to::ColdCumulative, from::HotConsecutive => to::HotConsecutive, from::HotCumulative => to::HotCumulative, - from::Excursion => to::Excursion, } } }