martinzink commented on a change in pull request #1152:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1152#discussion_r831006580



##########
File path: extensions/procfs/ProcFsJsonSerialization.h
##########
@@ -0,0 +1,179 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <string_view>
+
+#include "ProcFsSerialization.h"
+#include "rapidjson/stream.h"
+#include "rapidjson/document.h"
+
+namespace org::apache::nifi::minifi::extensions::procfs {
+
+namespace details {
+class UInt64Serializer {
+ public:
+  UInt64Serializer(rapidjson::Value& json, rapidjson::Document::AllocatorType& 
alloc) :
+      json_(json), alloc_(alloc) {
+  }
+  void operator()(const char(&key)[], const uint64_t value) {
+    json_.AddMember(rapidjson::StringRef(key), value, alloc_);
+  }
+ private:
+  rapidjson::Value& json_;
+  rapidjson::Document::AllocatorType& alloc_;
+};
+
+class DoubleSerializer {
+ public:
+  DoubleSerializer(rapidjson::Value& json, rapidjson::Document::AllocatorType& 
alloc) :
+      json_(json), alloc_(alloc) {
+  }
+  void operator()(const char(&key)[], const double value) {
+    json_.AddMember(rapidjson::StringRef(key), value, alloc_);
+  }
+ private:
+  rapidjson::Value& json_;
+  rapidjson::Document::AllocatorType& alloc_;
+};
+
+class StringSerializer {
+ public:
+  StringSerializer(rapidjson::Value& json, rapidjson::Document::AllocatorType& 
alloc) :
+      json_(json), alloc_(alloc) {
+  }
+  void operator()(const char(&key)[], const std::string_view& value) {
+    rapidjson::Value value_json(value.data(), value.size(), alloc_);
+    json_.AddMember(rapidjson::StringRef(key), value_json, alloc_);
+  }
+ private:
+  rapidjson::Value& json_;
+  rapidjson::Document::AllocatorType& alloc_;
+};
+}  // namespace details
+
+void addCPUStatToJson(const std::string& cpu_name,
+                      const CpuStatData& cpu_stat,
+                      rapidjson::Value& cpu_root,
+                      rapidjson::Document::AllocatorType& alloc) {
+  rapidjson::Value cpu_key(cpu_name.c_str(), cpu_name.length(), alloc);
+  cpu_root.AddMember(cpu_key.Move(), rapidjson::kObjectType, alloc);
+  rapidjson::Value& cpu_stat_json = cpu_root[cpu_name.c_str()];
+  SerializeCPUStatData(cpu_stat,
+                       details::UInt64Serializer(cpu_stat_json, alloc));
+}
+
+void addCPUStatPeriodToJson(const std::string& cpu_name,
+                            const CpuStatData& start,
+                            const CpuStatData& end,
+                            rapidjson::Value& cpu_root,
+                            rapidjson::Document::AllocatorType& alloc) {
+  rapidjson::Value cpu_key(cpu_name.c_str(), cpu_name.length(), alloc);
+  cpu_root.AddMember(cpu_key.Move(), rapidjson::kObjectType, alloc);
+  rapidjson::Value& cpu_stat_json = cpu_root[cpu_name.c_str()];
+  SerializeNormalizedCPUStat(end-start,
+                             details::DoubleSerializer(cpu_stat_json, alloc));
+}
+
+void addDiskStatToJson(const std::string& disk_name,
+                       const DiskStatData& disk_stat,
+                       rapidjson::Value& disk_root,
+                       rapidjson::Document::AllocatorType& alloc) {
+  rapidjson::Value disk_key(disk_name.c_str(), disk_name.length(), alloc);
+  disk_root.AddMember(disk_key.Move(), rapidjson::kObjectType, alloc);
+  rapidjson::Value& disk_json = disk_root[disk_name.c_str()];
+  SerializeDiskStatData(disk_stat,
+                        details::UInt64Serializer(disk_json, alloc));
+}
+
+void addDiskStatPerSecToJson(const std::string& disk_name,
+                             const DiskStatData disk_stat,
+                             const std::chrono::duration<double> duration,
+                             rapidjson::Value& disk_root,
+                             rapidjson::Document::AllocatorType& alloc) {
+  rapidjson::Value disk_key(disk_name.c_str(), disk_name.length(), alloc);
+  disk_root.AddMember(disk_key.Move(), rapidjson::kObjectType, alloc);
+  rapidjson::Value& disk_json = disk_root[disk_name.c_str()];
+  SerializeDiskStatDataPerSec(disk_stat,
+                              duration,
+                              details::DoubleSerializer(disk_json, alloc));
+}
+
+
+void addNetDevToJson(const std::string& net_name,

Review comment:
       makes sense :+1: , 
https://github.com/apache/nifi-minifi-cpp/pull/1152/commits/84e464c7b9501293616bcae170c4a12a710f7a6d#diff-8a4db62b36fb7340e48e062700fa2ae5b9c3851563ef91e310a51b84ba0e44c4R99
 and 
https://github.com/apache/nifi-minifi-cpp/pull/1152/commits/1513c2a722db3015170dc072267a03a656de0cda




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to