lordgamez commented on code in PR #1400:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1400#discussion_r997095999


##########
libminifi/include/utils/Averager.h:
##########
@@ -0,0 +1,91 @@
+/**
+ * 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 <vector>
+#include <mutex>
+
+namespace org::apache::nifi::minifi::utils {
+
+template<typename T>
+concept Summable = requires(T x) { x + x; };  // NOLINT(readability/braces)
+
+template<typename T>
+concept DividableByInteger = requires(T x, uint32_t divisor) { x / divisor; }; 
 // NOLINT(readability/braces)
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+class Averager {
+ public:
+  explicit Averager(uint32_t sample_size) : SAMPLE_SIZE_(sample_size) {
+    values_.reserve(SAMPLE_SIZE_);
+  }
+
+  ValueType getAverage() const;
+  ValueType getLastValue() const;
+  void addValue(ValueType runtime);
+
+ private:
+  const uint32_t SAMPLE_SIZE_;
+  mutable std::mutex average_value_mutex_;
+  uint32_t next_average_index_ = 0;
+  std::vector<ValueType> values_;
+};
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+ValueType Averager<ValueType>::getAverage() const {
+  if (values_.empty()) {
+    return {};
+  }
+  ValueType sum{};
+  std::lock_guard<std::mutex> lock(average_value_mutex_);
+  for (const auto& value : values_) {

Review Comment:
   Added `accumulate` usage in c164ede1b258f2d320e0313456650a253da5de5e



##########
libminifi/include/utils/Averager.h:
##########
@@ -0,0 +1,91 @@
+/**
+ * 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 <vector>
+#include <mutex>
+
+namespace org::apache::nifi::minifi::utils {
+
+template<typename T>
+concept Summable = requires(T x) { x + x; };  // NOLINT(readability/braces)
+
+template<typename T>
+concept DividableByInteger = requires(T x, uint32_t divisor) { x / divisor; }; 
 // NOLINT(readability/braces)
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+class Averager {
+ public:
+  explicit Averager(uint32_t sample_size) : SAMPLE_SIZE_(sample_size) {
+    values_.reserve(SAMPLE_SIZE_);
+  }
+
+  ValueType getAverage() const;
+  ValueType getLastValue() const;
+  void addValue(ValueType runtime);
+
+ private:
+  const uint32_t SAMPLE_SIZE_;
+  mutable std::mutex average_value_mutex_;
+  uint32_t next_average_index_ = 0;
+  std::vector<ValueType> values_;
+};
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+ValueType Averager<ValueType>::getAverage() const {
+  if (values_.empty()) {
+    return {};
+  }
+  ValueType sum{};
+  std::lock_guard<std::mutex> lock(average_value_mutex_);
+  for (const auto& value : values_) {
+    sum += value;
+  }
+  return sum / values_.size();
+}
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+void Averager<ValueType>::addValue(ValueType runtime) {
+  std::lock_guard<std::mutex> lock(average_value_mutex_);
+  if (values_.size() < SAMPLE_SIZE_) {
+    values_.push_back(runtime);
+  } else {
+    if (next_average_index_ >= values_.size()) {
+      next_average_index_ = 0;
+    }
+    values_[next_average_index_] = runtime;
+    ++next_average_index_;
+  }
+}
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+ValueType Averager<ValueType>::getLastValue() const {
+  std::lock_guard<std::mutex> lock(average_value_mutex_);
+  if (values_.empty()) {
+    return {};
+  } else if (values_.size() < SAMPLE_SIZE_) {
+    return values_[values_.size() - 1];
+  } else {
+    return values_[next_average_index_ - 1];

Review Comment:
   Good catch, fixed in c164ede1b258f2d320e0313456650a253da5de5e and added some 
tests for this case.



##########
libminifi/src/core/ProcessorMetrics.cpp:
##########
@@ -0,0 +1,104 @@
+/**
+ * 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.
+ */
+#include "core/ProcessorMetrics.h"
+
+#include "core/Processor.h"
+#include "utils/gsl.h"
+
+using namespace std::literals::chrono_literals;
+
+namespace org::apache::nifi::minifi::core {
+
+ProcessorMetrics::ProcessorMetrics(const Processor& source_processor)
+    : source_processor_(source_processor),
+      on_trigger_runtime_averager_(STORED_ON_TRIGGER_RUNTIME_COUNT) {
+}
+
+std::string ProcessorMetrics::getName() const {
+  return source_processor_.getProcessorType() + "Metrics";
+}
+
+std::unordered_map<std::string, std::string> 
ProcessorMetrics::getCommonLabels() const {
+  return {{"metric_class", getName()}, {"processor_name", 
source_processor_.getName()}, {"processor_uuid", 
source_processor_.getUUIDStr()}};
+}
+
+std::vector<state::response::SerializedResponseNode> 
ProcessorMetrics::serialize() {
+  std::vector<state::response::SerializedResponseNode> resp;
+
+  state::response::SerializedResponseNode root_node {
+    .name = source_processor_.getUUIDStr(),
+    .children = {
+      {.name = "OnTriggerInvocations", .value = 
static_cast<uint32_t>(iterations.load())},
+      {.name = "AverageOnTriggerRunTime", .value = 
static_cast<uint64_t>(getAverageOnTriggerRuntime().count())},
+      {.name = "LastOnTriggerRunTime", .value = 
static_cast<uint64_t>(getLastOnTriggerRuntime().count())},
+      {.name = "TransferredFlowFiles", .value = 
static_cast<uint32_t>(transferred_flow_files.load())},
+      {.name = "TransferredBytes", .value = transferred_bytes.load()}
+    }
+  };
+
+  for (const auto& [relationship, count] : transferred_relationships_) {

Review Comment:
   Good point, added lock in c164ede1b258f2d320e0313456650a253da5de5e



-- 
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