Skip to content

Commit 0f80192

Browse files
committed
The default config of flow transformation is {}
The default config was previously null, forcing explicit checks in the scripts. Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
1 parent cae0eaa commit 0f80192

File tree

10 files changed

+25
-26
lines changed

10 files changed

+25
-26
lines changed

crates/extensions/tedge_gen_mapper/pipelines/circuit-breaker.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export function process (timestamp, message, config) {
1919
State.total += 1
2020
State.batch[0] += 1
2121
if (State.open) {
22-
let back_to_normal = config?.back_to_normal || 100
22+
let back_to_normal = config.back_to_normal || 100
2323
if (State.total < back_to_normal) {
2424
State.open = false
25-
if (config?.message_on_back_to_normal) {
25+
if (config.message_on_back_to_normal) {
2626
return [config?.message_on_back_to_normal, message]
2727
} else {
2828
return [message]
@@ -31,13 +31,13 @@ export function process (timestamp, message, config) {
3131
return []
3232
}
3333
} else {
34-
let too_many = config?.too_many || 1000
34+
let too_many = config.too_many || 1000
3535
if (State.total < too_many) {
3636
return [message]
3737
} else {
3838
State.open = true
39-
if (config?.message_on_too_many) {
40-
return [config?.message_on_too_many]
39+
if (config.message_on_too_many) {
40+
return [config.message_on_too_many]
4141
} else {
4242
return []
4343
}
@@ -47,15 +47,15 @@ export function process (timestamp, message, config) {
4747

4848

4949
export function tick(timestamp, config) {
50-
let max_batch_count = config?.tick_count || 10
50+
let max_batch_count = config.tick_count || 10
5151
let new_batch_count = State.batch.unshift(0)
5252
if (new_batch_count > max_batch_count) {
5353
State.total -= State.batch.pop()
5454
}
5555

56-
if (config?.stats_topic) {
56+
if (config.stats_topic) {
5757
return [{
58-
topic: config?.stats_topic,
58+
topic: config.stats_topic,
5959
payload: `{"circuit-breaker-open": ${State.open}, "total": ${State.total}, "batch": ${State.batch}}`
6060
}]
6161
} else {

crates/extensions/tedge_gen_mapper/pipelines/collectd-to-te.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function process(_timestamp, message, config) {
88
let value = data[1]
99

1010
return [{
11-
topic: config?.topic || "te/device/main///m/collectd",
11+
topic: config.topic || "te/device/main///m/collectd",
1212
payload: `{"time": ${time}, "${group}": {"${measurement}": ${value}}}`
1313
}]
1414
}

crates/extensions/tedge_gen_mapper/pipelines/drop_stragglers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export function process (timestamp, message, config) {
1212
}
1313

1414
let time = timestamp.seconds + (timestamp.nanoseconds / 1e9)
15-
let max = time + (config?.max_advance || 1);
16-
let min = time - (config?.max_delay || 10);
15+
let max = time + (config.max_advance || 1);
16+
let min = time - (config.max_delay || 10);
1717

1818
if (min <= msg_timestamp && msg_timestamp <= max) {
1919
return [message]

crates/extensions/tedge_gen_mapper/pipelines/measurements.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ input_topics = ["te/+/+/+/+/m/+"]
22

33
stages = [
44
{ filter = "add_timestamp.js" },
5-
{ filter = "drop_stragglers.js", config = { max_delay = 60 } },
65
{ filter = "te_to_c8y.js", meta_topics = ["te/+/+/+/+/m/+/meta"] }
76
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function process (timestamp, message, config) {
22
return [{
3-
topic: config?.topic || "te/error",
3+
topic: config.topic || "te/error",
44
payload: message.payload
55
}]
66
}

crates/extensions/tedge_gen_mapper/pipelines/te_to_c8y.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ export function process(t, message, config) {
5151
type: type
5252
}
5353

54-
let meta = (config || {})[`${message.topic}/meta`] || {}
54+
let meta = config[`${message.topic}/meta`] || {}
5555

5656
for (let [k, v] of Object.entries(payload)) {
57-
let k_meta = (meta || {})[k] || {}
57+
let k_meta = meta[k] || {}
5858
if (k === "time") {
5959
let t = v
6060
if (typeof(v) === "number") {
@@ -123,9 +123,6 @@ export function update_config(message, config) {
123123
let fragment = {
124124
[type]: metadata
125125
}
126-
if (!config) {
127-
config = {}
128-
}
129126
Object.assign(config, fragment)
130127

131128
return config

crates/extensions/tedge_gen_mapper/src/js_filter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ pub struct JsFilter {
2020
pub tick_every_seconds: u64,
2121
}
2222

23-
#[derive(Clone, Debug, Default)]
23+
#[derive(Clone, Debug)]
2424
pub struct JsonValue(serde_json::Value);
2525

26+
impl Default for JsonValue {
27+
fn default() -> Self {
28+
JsonValue(serde_json::Value::Object(Default::default()))
29+
}
30+
}
31+
2632
impl JsFilter {
2733
pub fn new(pipeline: PathBuf, index: usize, path: PathBuf) -> Self {
2834
let module_name = format!("{}|{}|{}", pipeline.display(), index, path.display());

tests/RobotFramework/tests/tedge_gen_mapper/pipelines/count-messages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function process (timestamp, message) {
1313

1414
export function tick(timestamp, config) {
1515
let message = {
16-
topic: config?.topic || "te/error",
16+
topic: config.topic || "te/error",
1717
payload: JSON.stringify(State.count_per_topic)
1818
}
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function process (timestamp, message, config) {
22
return [{
3-
topic: config?.topic || "te/error",
3+
topic: config.topic || "te/error",
44
payload: message.payload
55
}]
66
}

tests/RobotFramework/tests/tedge_gen_mapper/pipelines/te_to_c8y.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ export function process(t, message, config) {
5151
type: type
5252
}
5353

54-
let meta = (config || {})[`${message.topic}/meta`] || {}
54+
let meta = config[`${message.topic}/meta`] || {}
5555

5656
for (let [k, v] of Object.entries(payload)) {
57-
let k_meta = (meta || {})[k] || {}
57+
let k_meta = meta[k] || {}
5858
if (k === "time") {
5959
let t = v
6060
if (typeof(v) === "number") {
@@ -123,9 +123,6 @@ export function update_config(message, config) {
123123
let fragment = {
124124
[type]: metadata
125125
}
126-
if (!config) {
127-
config = {}
128-
}
129126
Object.assign(config, fragment)
130127

131128
return config

0 commit comments

Comments
 (0)