martinzink commented on a change in pull request #1081: URL: https://github.com/apache/nifi-minifi-cpp/pull/1081#discussion_r639760892
########## File path: libminifi/include/utils/MathUtils.h ########## @@ -0,0 +1,43 @@ +/** + * 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 <math.h> + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +class MathUtils { + public: + static double round_to(double original, int8_t precision) { + if (precision < 0) { + return original; + } else { + double power_ten = pow(10, precision); + return std::round(original * power_ten) / power_ten; Review comment: You are right, changed the function to round_to_decimal_places so its more clear what the function does. ########## File path: extensions/pdh/PerformanceDataMonitor.cpp ########## @@ -264,16 +281,29 @@ void PerformanceDataMonitor::setupMembersFromProperties(const std::shared_ptr<co std::string output_format_string; if (context->getProperty(OutputFormatProperty.getName(), output_format_string)) { - if (output_format_string == OPEN_TELEMETRY_FORMAT_STR) { - logger_->log_trace("OutputFormat is configured to be OpenTelemetry"); + if (output_format_string == PRETTY_OPEN_TELEMETRY_FORMAT_STR || output_format_string == COMPACT_OPEN_TELEMETRY_FORMAT_STR) { output_format_ = OutputFormat::OPENTELEMETRY; - } else if (output_format_string == JSON_FORMAT_STR) { - logger_->log_trace("OutputFormat is configured to be JSON"); + pretty_output_ = output_format_string == PRETTY_OPEN_TELEMETRY_FORMAT_STR; + logger_->log_trace("OutputFormat is configured to be %s OpenTelemetry", pretty_output_ ? "pretty" : "compact"); + } else if (output_format_string == PRETTY_JSON_FORMAT_STR || output_format_string == COMPACT_JSON_FORMAT_STR) { output_format_ = OutputFormat::JSON; + pretty_output_ = output_format_string == PRETTY_JSON_FORMAT_STR; + logger_->log_trace("OutputFormat is configured to be %s JSON", pretty_output_ ? "pretty" : "compact"); } else { - logger_->log_error("Invalid OutputFormat, defaulting to JSON"); output_format_ = OutputFormat::JSON; + pretty_output_ = true; + logger_->log_error("Invalid OutputFormat, defaulting to %s JSON", pretty_output_ ? "pretty" : "compact"); Review comment: good idea changed it in [26802f7](https://github.com/martinzink/nifi-minifi-cpp/commit/26802f77ef539757fcedbffea0f596b2ef0cc828) ########## File path: extensions/pdh/PerformanceDataMonitor.cpp ########## @@ -102,25 +108,36 @@ void PerformanceDataMonitor::onTrigger(core::ProcessContext* context, core::Proc rapidjson::Value& body = prepareJSONBody(root); for (auto& counter : resource_consumption_counters_) { if (counter->collectData()) - counter->addToJson(body, root.GetAllocator()); + counter->addToJson(body, root.GetAllocator(), double_precision_); + } + if (pretty_output_) { + utils::PrettyJsonOutputCallback callback(std::move(root)); + session->write(flowFile, &callback); + session->transfer(flowFile, Success); + } else { + utils::JsonOutputCallback callback(std::move(root)); + session->write(flowFile, &callback); + session->transfer(flowFile, Success); } - utils::JsonOutputCallback callback(std::move(root)); - session->write(flowFile, &callback); - session->transfer(flowFile, Success); } void PerformanceDataMonitor::initialize() { - setSupportedProperties({ CustomPDHCounters, PredefinedGroups, OutputFormatProperty }); + setSupportedProperties({ CustomPDHCounters, PredefinedGroups, OutputFormatProperty, DoublePrecisionProperty }); Review comment: changed to DecimalPlaces in [26802f7](https://github.com/martinzink/nifi-minifi-cpp/commit/26802f77ef539757fcedbffea0f596b2ef0cc828) ########## File path: extensions/pdh/PerformanceDataMonitor.cpp ########## @@ -45,9 +45,15 @@ core::Property PerformanceDataMonitor::CustomPDHCounters( core::Property PerformanceDataMonitor::OutputFormatProperty( core::PropertyBuilder::createProperty("Output Format")-> withDescription("Format of the created flowfiles")-> - withAllowableValue<std::string>(JSON_FORMAT_STR)-> - withAllowableValue(OPEN_TELEMETRY_FORMAT_STR)-> - withDefaultValue(JSON_FORMAT_STR)->build()); + withAllowableValue(PRETTY_JSON_FORMAT_STR)-> + withAllowableValue(COMPACT_JSON_FORMAT_STR)-> + withAllowableValue(PRETTY_OPEN_TELEMETRY_FORMAT_STR)-> + withAllowableValue(COMPACT_OPEN_TELEMETRY_FORMAT_STR)-> Review comment: good idea, changed it in [26802f7](https://github.com/martinzink/nifi-minifi-cpp/commit/26802f77ef539757fcedbffea0f596b2ef0cc828) ########## File path: extensions/pdh/PerformanceDataMonitor.h ########## @@ -75,7 +78,9 @@ class PerformanceDataMonitor : public core::Processor { void addCustomPDHCountersFromProperty(const std::string& custom_pdh_counters); OutputFormat output_format_; + bool pretty_output_; Review comment: good catch, fixed in [26802f7](https://github.com/martinzink/nifi-minifi-cpp/commit/26802f77ef539757fcedbffea0f596b2ef0cc828) ########## File path: extensions/pdh/PerformanceDataMonitor.cpp ########## @@ -264,16 +281,29 @@ void PerformanceDataMonitor::setupMembersFromProperties(const std::shared_ptr<co std::string output_format_string; if (context->getProperty(OutputFormatProperty.getName(), output_format_string)) { - if (output_format_string == OPEN_TELEMETRY_FORMAT_STR) { - logger_->log_trace("OutputFormat is configured to be OpenTelemetry"); + if (output_format_string == PRETTY_OPEN_TELEMETRY_FORMAT_STR || output_format_string == COMPACT_OPEN_TELEMETRY_FORMAT_STR) { output_format_ = OutputFormat::OPENTELEMETRY; - } else if (output_format_string == JSON_FORMAT_STR) { - logger_->log_trace("OutputFormat is configured to be JSON"); + pretty_output_ = output_format_string == PRETTY_OPEN_TELEMETRY_FORMAT_STR; + logger_->log_trace("OutputFormat is configured to be %s OpenTelemetry", pretty_output_ ? "pretty" : "compact"); + } else if (output_format_string == PRETTY_JSON_FORMAT_STR || output_format_string == COMPACT_JSON_FORMAT_STR) { output_format_ = OutputFormat::JSON; + pretty_output_ = output_format_string == PRETTY_JSON_FORMAT_STR; + logger_->log_trace("OutputFormat is configured to be %s JSON", pretty_output_ ? "pretty" : "compact"); } else { - logger_->log_error("Invalid OutputFormat, defaulting to JSON"); output_format_ = OutputFormat::JSON; + pretty_output_ = true; + logger_->log_error("Invalid OutputFormat, defaulting to %s JSON", pretty_output_ ? "pretty" : "compact"); + } + } + + std::string double_precision_string; + if (context->getProperty(DoublePrecisionProperty.getName(), double_precision_string)) { Review comment: Good idea, unfortunetly int8_t is not yet supported by Value.h and ValueParser.h (which the getProperty uses) but left TODO and will replace this in a separate PR if thats okay with you (I didnt want to increase the scope of this PR with the int8_t Value implementation) ########## File path: extensions/pdh/PerformanceDataMonitor.cpp ########## @@ -264,16 +281,29 @@ void PerformanceDataMonitor::setupMembersFromProperties(const std::shared_ptr<co std::string output_format_string; if (context->getProperty(OutputFormatProperty.getName(), output_format_string)) { - if (output_format_string == OPEN_TELEMETRY_FORMAT_STR) { - logger_->log_trace("OutputFormat is configured to be OpenTelemetry"); + if (output_format_string == PRETTY_OPEN_TELEMETRY_FORMAT_STR || output_format_string == COMPACT_OPEN_TELEMETRY_FORMAT_STR) { output_format_ = OutputFormat::OPENTELEMETRY; - } else if (output_format_string == JSON_FORMAT_STR) { - logger_->log_trace("OutputFormat is configured to be JSON"); + pretty_output_ = output_format_string == PRETTY_OPEN_TELEMETRY_FORMAT_STR; + logger_->log_trace("OutputFormat is configured to be %s OpenTelemetry", pretty_output_ ? "pretty" : "compact"); + } else if (output_format_string == PRETTY_JSON_FORMAT_STR || output_format_string == COMPACT_JSON_FORMAT_STR) { output_format_ = OutputFormat::JSON; + pretty_output_ = output_format_string == PRETTY_JSON_FORMAT_STR; + logger_->log_trace("OutputFormat is configured to be %s JSON", pretty_output_ ? "pretty" : "compact"); } else { - logger_->log_error("Invalid OutputFormat, defaulting to JSON"); output_format_ = OutputFormat::JSON; + pretty_output_ = true; + logger_->log_error("Invalid OutputFormat, defaulting to %s JSON", pretty_output_ ? "pretty" : "compact"); + } Review comment: extracted the different PropertyHandling functions to separete member functions in [26802f7](https://github.com/martinzink/nifi-minifi-cpp/commit/26802f77ef539757fcedbffea0f596b2ef0cc828) ########## File path: libminifi/include/utils/MathUtils.h ########## @@ -0,0 +1,43 @@ +/** + * 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 <math.h> + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +class MathUtils { + public: + static double round_to(double original, int8_t precision) { Review comment: i left the precision here to int8_t but changed the member in PerformanceDataMonitor to be utils::optional<int8_t> in [26802f7](https://github.com/martinzink/nifi-minifi-cpp/commit/26802f77ef539757fcedbffea0f596b2ef0cc828) ########## File path: libminifi/include/utils/MathUtils.h ########## @@ -0,0 +1,43 @@ +/** + * 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 <math.h> + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +class MathUtils { + public: + static double round_to(double original, int8_t precision) { + if (precision < 0) { + return original; + } else { + double power_ten = pow(10, precision); + return std::round(original * power_ten) / power_ten; + } Review comment: removed the IF now it works with negative decimal places as well (also renamed to round_to_decimal_places) in [26802f7](https://github.com/martinzink/nifi-minifi-cpp/commit/26802f77ef539757fcedbffea0f596b2ef0cc828) -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org