@@ -217,6 +217,8 @@ void Aggregator::publishData()
217217 diag_toplevel_state.level = DiagnosticStatus::STALE;
218218 int max_level = -1 ;
219219 int min_level = 255 ;
220+ int non_ok_status_depth = 0 ;
221+ std::shared_ptr<DiagnosticStatus> msg_to_report;
220222
221223 std::vector<std::shared_ptr<DiagnosticStatus>> processed;
222224 {
@@ -225,27 +227,57 @@ void Aggregator::publishData()
225227 }
226228 for (const auto & msg : processed) {
227229 diag_array.status .push_back (*msg);
230+ const auto depth = std::count (msg->name .begin (), msg->name .end (), ' /' );
228231
229232 if (msg->level > max_level) {
230233 max_level = msg->level ;
234+ non_ok_status_depth = depth;
235+ msg_to_report = msg;
236+ }
237+ if (msg->level == max_level && depth > non_ok_status_depth) {
238+ // On non okay diagnostics also copy the deepest message to toplevel state
239+ non_ok_status_depth = depth;
240+ msg_to_report = msg;
231241 }
232242 if (msg->level < min_level) {
233243 min_level = msg->level ;
234244 }
235245 }
246+ // When a non-ok item was found, copy the complete status message once
247+ if (max_level > DiagnosticStatus::OK) {
248+ diag_toplevel_state.name = msg_to_report->name ;
249+ diag_toplevel_state.message = msg_to_report->message ;
250+ diag_toplevel_state.hardware_id = msg_to_report->hardware_id ;
251+ diag_toplevel_state.values = msg_to_report->values ;
252+ }
236253
237254 std::vector<std::shared_ptr<DiagnosticStatus>> processed_other =
238255 other_analyzer_->report ();
239256 for (const auto & msg : processed_other) {
240257 diag_array.status .push_back (*msg);
258+ const auto depth = std::count (msg->name .begin (), msg->name .end (), ' /' );
241259
242260 if (msg->level > max_level) {
243261 max_level = msg->level ;
262+ non_ok_status_depth = depth;
263+ msg_to_report = msg;
264+ }
265+ if (msg->level == max_level && depth > non_ok_status_depth) {
266+ // On non okay diagnostics also copy the deepest message to toplevel state
267+ non_ok_status_depth = depth;
268+ msg_to_report = msg;
244269 }
245270 if (msg->level < min_level) {
246271 min_level = msg->level ;
247272 }
248273 }
274+ // When a non-ok item was found, copy the complete status message once
275+ if (max_level > DiagnosticStatus::OK) {
276+ diag_toplevel_state.name = msg_to_report->name ;
277+ diag_toplevel_state.message = msg_to_report->message ;
278+ diag_toplevel_state.hardware_id = msg_to_report->hardware_id ;
279+ diag_toplevel_state.values = msg_to_report->values ;
280+ }
249281
250282 diag_array.header .stamp = clock_->now ();
251283 agg_pub_->publish (diag_array);
0 commit comments