Skip to content

Commit 3219f31

Browse files
authored
fix: temperature filter panics when bounds not configured (#175)
## Problem The `filter_temperature` function in the temperature WASM operator panics when `temperature_lower_bound` and `temperature_upper_bound` are not provided via `moduleConfigurations`. The complex graph YAML (`graph-complex.yaml`) does not include these parameters, so deploying the complex graph example causes the temperature filter to crash on every message with: ``` Lower bound not initialized ``` This means the complex graph never produces temperature statistics in its output (humidity and object detection work fine). ## Fix Two changes (belt and suspenders): 1. **Code fix** (`temperature/src/lib.rs`): Use sensible defaults (`-40°C` lower, `3422°C` upper / melting point of tungsten) instead of panicking. Logs which defaults are in use. 2. **Graph fix** (`graph-complex.yaml`): Add `temperature_lower_bound` and `temperature_upper_bound` to `moduleConfigurations` so the parameters are discoverable and configurable. ## Testing - Built and verified with `cargo build --target wasm32-wasip2 --release` (Rust 1.87) - Also verified with `wasm32-wasip1` target - Discovered during end-to-end documentation validation of the WASM data flow graph docs on a live AIO k3s cluster
1 parent 425f9c8 commit 3219f31

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

samples/wasm/graph-complex.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ moduleConfigurations:
2020
snapshot_topic:
2121
name: snapshot_topic
2222
description: Transform app snapshot_topic in snapshot branch's init routine
23+
- name: module-temperature/filter
24+
parameters:
25+
temperature_lower_bound:
26+
name: temperature_lower_bound
27+
description: "Minimum valid temperature in Celsius (default: -40)"
28+
temperature_upper_bound:
29+
name: temperature_upper_bound
30+
description: "Maximum valid temperature in Celsius (default: 3422)"
2331
operations:
2432
- operationType: "source"
2533
name: "source"

samples/wasm/operators/temperature/src/lib.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,15 @@ mod filter_temperature {
291291
use wasm_graph_sdk::macros::filter_operator;
292292
use wasm_graph_sdk::metrics::{self, CounterValue, Label};
293293

294+
const DEFAULT_LOWER_BOUND: f64 = -40.0; // reasonable minimum for sensor data
295+
const DEFAULT_UPPER_BOUND: f64 = 3422.0; // melting point of tungsten
296+
294297
static LOWER_BOUND: OnceLock<f64> = OnceLock::new();
295298
static UPPER_BOUND: OnceLock<f64> = OnceLock::new();
296299

297-
// Note!: The initialization parameters LOWER_BOUND and UPPER_BOUND must be set via
298-
// configuration properties. If these values are not configured, the function
299-
// filter_temperature will panic when attempting to access them.
300-
//
301-
// Users can define these parameters either by using default values or by specifying
302-
// them during application setup.
300+
// The initialization parameters LOWER_BOUND and UPPER_BOUND can be set via
301+
// configuration properties. If not configured, sensible defaults are used
302+
// (-40°C lower bound, 3422°C upper bound / melting point of tungsten).
303303
#[allow(clippy::needless_pass_by_value)]
304304
fn filter_temperature_init(configuration: ModuleConfiguration) -> bool {
305305
logger::log(
@@ -311,7 +311,7 @@ mod filter_temperature {
311311
if let Some(value_string) = configuration
312312
.properties
313313
.iter()
314-
.find(|(key, _value)| key == "temperature_lower_bound") // or whatever it is
314+
.find(|(key, _value)| key == "temperature_lower_bound")
315315
.map(|(_key, value)| value.clone())
316316
{
317317
match value_string.parse::<f64>() {
@@ -331,12 +331,18 @@ mod filter_temperature {
331331
);
332332
}
333333
}
334+
} else {
335+
logger::log(
336+
Level::Info,
337+
"module-temperature/filter",
338+
&format!("temperature_lower_bound not configured, using default: {DEFAULT_LOWER_BOUND}"),
339+
);
334340
}
335341

336342
if let Some(value_string) = configuration
337343
.properties
338344
.iter()
339-
.find(|(key, _value)| key == "temperature_upper_bound") // or whatever it is
345+
.find(|(key, _value)| key == "temperature_upper_bound")
340346
.map(|(_key, value)| value.clone())
341347
{
342348
match value_string.parse::<f64>() {
@@ -356,6 +362,12 @@ mod filter_temperature {
356362
);
357363
}
358364
}
365+
} else {
366+
logger::log(
367+
Level::Info,
368+
"module-temperature/filter",
369+
&format!("temperature_upper_bound not configured, using default: {DEFAULT_UPPER_BOUND}"),
370+
);
359371
}
360372

361373
true
@@ -386,8 +398,8 @@ mod filter_temperature {
386398
&format!("incoming measurement {measurement:?}"),
387399
);
388400

389-
let lower_bound = LOWER_BOUND.get().expect("Lower bound not initialized");
390-
let upper_bound = UPPER_BOUND.get().expect("Upper bound not initialized");
401+
let lower_bound = LOWER_BOUND.get().copied().unwrap_or(DEFAULT_LOWER_BOUND);
402+
let upper_bound = UPPER_BOUND.get().copied().unwrap_or(DEFAULT_UPPER_BOUND);
391403

392404
// Malfunctioning probe sometimes reports higher temperature than melting point of tungsten.
393405
// Ignore these values.
@@ -402,7 +414,7 @@ mod filter_temperature {
402414
last: _,
403415
unit: MeasurementTemperatureUnit::Celsius,
404416
overtemp: _,
405-
}) if value.unwrap() < *upper_bound && value.unwrap() > *lower_bound,
417+
}) if value.unwrap() < upper_bound && value.unwrap() > lower_bound,
406418
))
407419
}
408420
}

0 commit comments

Comments
 (0)