Skip to content

Commit

Permalink
Merge pull request #279 from msupply-foundation/278-send-ok-message-c…
Browse files Browse the repository at this point in the history
…an-trigger-unexpectedly

278 send ok message can trigger unexpectedly
  • Loading branch information
jmbrunskill authored Dec 21, 2023
2 parents 11be9d8 + c4186a0 commit e4fc7f6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
6 changes: 6 additions & 0 deletions backend/coldchain/src/alerts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use service::{
service_provider::ServiceContext,
};

use crate::sensor_state::SensorStatus;

/*
Temperature Alerts will look something like this...
Expand Down Expand Up @@ -46,6 +48,7 @@ pub struct ColdchainAlert {
pub temperature: String,
pub alert_type: AlertType,
pub reminder_number: usize,
pub old_status: Option<SensorStatus>,
}

// Later this function probably won't exist, but serves as a reminder/POC...
Expand Down Expand Up @@ -135,6 +138,7 @@ mod tests {
temperature: 10.12345.to_string(),
alert_type: AlertType::High,
reminder_number: 0,
old_status: None,
};

let recipient1 = NotificationTarget {
Expand Down Expand Up @@ -202,6 +206,7 @@ mod tests {
temperature: 1.01.to_string(),
alert_type: AlertType::Low,
reminder_number: 0,
old_status: None,
};

let recipient1 = NotificationTarget {
Expand Down Expand Up @@ -269,6 +274,7 @@ mod tests {
temperature: 1.01.to_string(),
alert_type: AlertType::NoData,
reminder_number: 0,
old_status: None,
};

let recipient1 = NotificationTarget {
Expand Down
22 changes: 18 additions & 4 deletions backend/coldchain/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ pub fn try_process_sensor_notification(
temperature: current_temp,
alert_type: AlertType::Ok,
reminder_number,
old_status: Some(prev_sensor_state.status.clone()),
};

let alert = match curr_sensor_status {
Expand All @@ -407,10 +408,23 @@ pub fn try_process_sensor_notification(
}
},
SensorStatus::Ok => match config.confirm_ok {
true => Some(ColdchainAlert {
alert_type: AlertType::Ok,
..base_alert
}),
true => {
// only send an ok alert if the previous state was No Data see https://github.com/msupply-foundation/notify/issues/278
if prev_sensor_state.status == SensorStatus::NoData {
log::info!("Sending Ok alert for sensor {}", sensor_row.id);
Some(ColdchainAlert {
alert_type: AlertType::Ok,
..base_alert
})
} else {
log::info!(
"Not sending Ok alert for sensor {} as previous state was {:?}",
sensor_row.id,
prev_sensor_state.status
);
None
}
}
false => {
log::info!("Confirm Ok alert disabled for sensor {}", sensor_row.id);
None
Expand Down
8 changes: 2 additions & 6 deletions backend/coldchain/src/test/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,7 @@ fn test_try_process_sensor_notification_prev_high() {
latest_temperature_row,
);
assert_eq!(sensor_state.status, SensorStatus::Ok);
assert_eq!(alert.is_some(), true);
let alert = alert.unwrap();
assert_eq!(alert.alert_type, AlertType::Ok);
assert_eq!(alert.is_none(), true);

/*
Test 2: Was High 1 Minute ago, Now High Temp -> No Alert!
Expand Down Expand Up @@ -516,9 +514,7 @@ fn test_try_process_sensor_notification_prev_low() {
latest_temperature_row,
);
assert_eq!(sensor_state.status, SensorStatus::Ok);
assert_eq!(alert.is_some(), true);
let alert = alert.unwrap();
assert_eq!(alert.alert_type, AlertType::Ok);
assert_eq!(alert.is_none(), true);

/*
Test 2: Was Low 1 Minute ago, Now High Temp -> Alert!
Expand Down
2 changes: 1 addition & 1 deletion backend/templates/coldchain/no_data.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**Monitoring connection lost**
**📶 Monitoring connection lost!**

**Facility**: {{ store_name }}
{% if location_name %}
Expand Down
4 changes: 2 additions & 2 deletions backend/templates/coldchain/recovered.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**Sensor is now ok!**
**{% if old_status == "NoData" %}✅ Monitoring connection restored{% else %}✅ Sensor is now ok!{% endif %}**

**Facility**: {{ store_name }}
{% if location_name %}
Expand All @@ -9,5 +9,5 @@
**Date**: {{ last_data_time | date(format="%d %b %Y") }}
**Time**: {{ last_data_time | date(format="%H:%M")}}

**Temperature**: {{ temperature }}°C
**Temperature**: {{ temperature }} °C
**Last data received**: {{ data_age }} ago
2 changes: 1 addition & 1 deletion backend/templates/coldchain/temperature.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
**Date**: {{ last_data_time | date(format="%d %b %Y") }}
**Time**: {{ last_data_time | date(format="%H:%M")}}

**Temperature**: {{ temperature }}°C
**Temperature**: {{ temperature }} °C
{% if reminder_number %}
**Reminder Number**: {{ reminder_number }}
{% endif %}
4 changes: 2 additions & 2 deletions frontend/packages/common/src/intl/locales/en/system.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"label.coldchain-low-temp-alerts": "Send low temperature excursion alerts when less than",
"label.coldchain-no-data-alerts": "Send 'no temperature data' alert after",
"label.coldchain-reminder-alerts": "Send follow-up reminders until alert is resolved, every",
"label.coldchain-message-alerts-resolved": "Send confirmation message when alert is resolved",
"label.coldchain-message-alerts-resolved": "Send confirmation message when monitoring connection is restored",
"label.disable-user": "Disable User",
"label.download-example": "Download an example file here",
"label.edit-recipient": "Edit Recipient",
Expand All @@ -53,7 +53,7 @@
"label.notification-type-TELEGRAM": "Telegram",
"label.notification-type-UNKNOWN": "ERROR-UNSUPPORTED-NOTIFICATION-TYPE",
"label.parameters-query-results": "Parameters and Query Results",
"label.parameters-save-local":"Save parameters in local storage",
"label.parameters-save-local": "Save parameters in local storage",
"label.password-strength": "Password Strength",
"label.password-warning": "Warning",
"label.phone-number": "Phone Number",
Expand Down

0 comments on commit e4fc7f6

Please sign in to comment.