Skip to content

Commit f9b78c0

Browse files
Ferry SchoenmakersTimple
authored andcommitted
ROS2 port of feature to report stale when no errors but stale items
1 parent fca0add commit f9b78c0

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

diagnostic_aggregator/src/aggregator.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ void Aggregator::publishData()
216216
diag_toplevel_state.name = "toplevel_state";
217217
diag_toplevel_state.level = DiagnosticStatus::STALE;
218218
int max_level = -1;
219-
int min_level = 255;
219+
int8_t max_level_without_stale = 0;
220220
int non_ok_status_depth = 0;
221221
std::shared_ptr<DiagnosticStatus> msg_to_report;
222222

@@ -239,8 +239,11 @@ void Aggregator::publishData()
239239
non_ok_status_depth = depth;
240240
msg_to_report = msg;
241241
}
242-
if (msg->level < min_level) {
243-
min_level = msg->level;
242+
if (
243+
msg->level > max_level_without_stale &&
244+
msg->level != diagnostic_msgs::msg::DiagnosticStatus::STALE)
245+
{
246+
max_level_without_stale = msg->level;
244247
}
245248
}
246249
// When a non-ok item was found, copy the complete status message once
@@ -251,6 +254,7 @@ void Aggregator::publishData()
251254
diag_toplevel_state.values = msg_to_report->values;
252255
}
253256

257+
non_ok_status_depth = 0;
254258
std::vector<std::shared_ptr<DiagnosticStatus>> processed_other =
255259
other_analyzer_->report();
256260
for (const auto & msg : processed_other) {
@@ -267,8 +271,11 @@ void Aggregator::publishData()
267271
non_ok_status_depth = depth;
268272
msg_to_report = msg;
269273
}
270-
if (msg->level < min_level) {
271-
min_level = msg->level;
274+
if (
275+
msg->level > max_level_without_stale &&
276+
msg->level != diagnostic_msgs::msg::DiagnosticStatus::STALE)
277+
{
278+
max_level_without_stale = msg->level;
272279
}
273280
}
274281
// When a non-ok item was found, copy the complete status message once
@@ -282,14 +289,16 @@ void Aggregator::publishData()
282289
diag_array.header.stamp = clock_->now();
283290
agg_pub_->publish(diag_array);
284291

285-
diag_toplevel_state.level = max_level;
286-
if (max_level < 0 ||
287-
(max_level > DiagnosticStatus::ERROR && min_level <= DiagnosticStatus::ERROR))
292+
if (
293+
max_level == diagnostic_msgs::msg::DiagnosticStatus::STALE &&
294+
max_level_without_stale < diagnostic_msgs::msg::DiagnosticStatus::ERROR)
288295
{
289-
// Top level is error if we got no diagnostic level or
290-
// have stale items but not all are stale
291-
diag_toplevel_state.level = DiagnosticStatus::ERROR;
296+
diag_toplevel_state.level = diagnostic_msgs::msg::DiagnosticStatus::STALE;
297+
} else {
298+
diag_toplevel_state.level = max_level_without_stale;
292299
}
300+
301+
293302
last_top_level_state_ = diag_toplevel_state.level;
294303

295304
toplevel_state_pub_->publish(diag_toplevel_state);

diagnostic_aggregator/src/analyzer_group.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ std::vector<std::shared_ptr<diagnostic_msgs::msg::DiagnosticStatus>> AnalyzerGro
292292
return output;
293293
}
294294

295-
bool all_stale = true;
295+
unsigned char max_level_without_stale = 0;
296296

297297
for (auto j = 0u; j < analyzers_.size(); ++j) {
298298
std::string path = analyzers_[j]->getPath();
@@ -317,17 +317,23 @@ std::vector<std::shared_ptr<diagnostic_msgs::msg::DiagnosticStatus>> AnalyzerGro
317317
kv.key = nice_name;
318318
kv.value = processed[i]->message;
319319

320-
all_stale = all_stale &&
321-
(processed[i]->level == diagnostic_msgs::msg::DiagnosticStatus::STALE);
320+
if (processed[i]->level != diagnostic_msgs::msg::DiagnosticStatus::STALE) {
321+
max_level_without_stale = max(max_level_without_stale, processed[i]->level);
322+
}
322323
header_status->level = max(header_status->level, processed[i]->level);
323324
header_status->values.push_back(kv);
324325
}
325326
}
326327
}
327328

328-
// Report stale as errors unless all stale
329-
if (header_status->level == diagnostic_msgs::msg::DiagnosticStatus::STALE && !all_stale) {
330-
header_status->level = diagnostic_msgs::msg::DiagnosticStatus::ERROR;
329+
// If one STALE and no ERROR, report STALE
330+
if (
331+
header_status->level == diagnostic_msgs::msg::DiagnosticStatus::STALE &&
332+
max_level_without_stale < diagnostic_msgs::msg::DiagnosticStatus::ERROR)
333+
{
334+
header_status->level = diagnostic_msgs::msg::DiagnosticStatus::STALE;
335+
} else {
336+
header_status->level = max_level_without_stale;
331337
}
332338

333339
header_status->message = valToMsg(header_status->level);

diagnostic_aggregator/test/test_discard_behavior.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
foo_status=None,
136136
bar_discard=False,
137137
bar_status=None,
138-
agg_expected=DiagnosticStatus.ERROR, # <-- This is the case we are testing for.
138+
agg_expected=DiagnosticStatus.STALE, # <-- This is the case we are testing for.
139139
# if one of the children is *not* marked discard_stale := true and
140140
# there are no statuses, then the parent should roll up to ERROR.
141141
),
@@ -144,7 +144,7 @@
144144
foo_status=DiagnosticStatus.OK,
145145
bar_discard=False,
146146
bar_status=None,
147-
agg_expected=DiagnosticStatus.ERROR,
147+
agg_expected=DiagnosticStatus.STALE,
148148
),
149149
TestMetadata(
150150
foo_discard=True,

0 commit comments

Comments
 (0)