]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: refactor ServiceMap::Service::get_task_summary() 36139/head
authorKefu Chai <kchai@redhat.com>
Thu, 16 Jul 2020 12:08:56 +0000 (20:08 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 16 Jul 2020 12:11:52 +0000 (20:11 +0800)
for better readability

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mgr/ServiceMap.cc

index 490b118bcf0c618def658dab74d2fbefe3c05e8c..812691a84781c60eaabda76fac87a8b8c3758b93 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "mgr/ServiceMap.h"
 
+#include <fmt/format.h>
+
 #include "common/Formatter.h"
 
 using ceph::bufferlist;
@@ -67,7 +69,7 @@ void ServiceMap::Daemon::generate_test_instances(std::list<Daemon*>& ls)
 
 std::string ServiceMap::Service::get_summary() const
 {
-  if (summary.size()) {
+  if (!summary.empty()) {
     return summary;
   }
   if (daemons.empty()) {
@@ -105,24 +107,19 @@ std::string ServiceMap::Service::get_task_summary(const std::string_view task_pr
   //     {"service2 status" -> {"service2.0" -> "idle"},
   //                           {"service2.1" -> "running"}}
   std::map<std::string, std::map<std::string, std::string>> by_task;
-  for (const auto &p : daemons) {
-    std::stringstream d;
-    d << task_prefix << "." << p.first;
-    for (const auto &q : p.second.task_status) {
-      auto p1 = by_task.emplace(q.first, std::map<std::string, std::string>{}).first;
-      auto p2 = p1->second.emplace(d.str(), std::string()).first;
-      p2->second = q.second;
+  for (const auto& [service_id, daemon] : daemons) {
+    for (const auto& [task_name, status] : daemon.task_status) {
+      by_task[task_name].emplace(fmt::format("{}.{}", task_prefix, service_id),
+                                status);
     }
   }
-
   std::stringstream ss;
-  for (const auto &p : by_task) {
-    ss << "\n    " << p.first << ":";
-    for (auto q : p.second) {
-      ss << "\n        " << q.first << ": " << q.second;
+  for (const auto &[task_name, status_by_service] : by_task) {
+    ss << "\n    " << task_name << ":";
+    for (auto& [service, status] : status_by_service) {
+      ss << "\n        " << service << ": " << status;
     }
   }
-
   return ss.str();
 }