adamdebreceni commented on code in PR #1152: URL: https://github.com/apache/nifi-minifi-cpp/pull/1152#discussion_r847293647
########## extensions/procfs/processors/ProcFsMonitor.cpp: ########## @@ -0,0 +1,386 @@ +/** + * 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 "ProcFsMonitor.h" +#include <limits> +#include <utility> +#include <vector> +#include <unordered_map> + +#include "core/Resource.h" +#include "core/ProcessContext.h" +#include "core/ProcessSession.h" +#include "../ProcFsJsonSerialization.h" +#include "utils/JsonCallback.h" +#include "utils/OpenTelemetryLogDataModelUtils.h" +#include "utils/gsl.h" + +using namespace std::literals::chrono_literals; + +namespace org::apache::nifi::minifi::extensions::procfs { + +const core::Relationship ProcFsMonitor::Success("success", "All files are routed to success"); + +const core::Property ProcFsMonitor::OutputFormatProperty( + core::PropertyBuilder::createProperty("Output Format") + ->withDescription("The output type of the new flowfile") + ->withAllowableValues<std::string>(OutputFormat::values()) + ->withDefaultValue(toString(OutputFormat::JSON))->build()); + +const core::Property ProcFsMonitor::OutputCompactnessProperty( + core::PropertyBuilder::createProperty("Output Compactness") + ->withDescription("The output format of the new flowfile") + ->withAllowableValues<std::string>(OutputCompactness::values()) + ->withDefaultValue(toString(OutputCompactness::PRETTY))->build()); + +const core::Property ProcFsMonitor::DecimalPlaces( + core::PropertyBuilder::createProperty("Round to decimal places") + ->withDescription("The number of decimal places to round the values to (blank for no rounding)")->build()); + +const core::Property ProcFsMonitor::ResultRelativenessProperty( + core::PropertyBuilder::createProperty("Result Type") + ->withDescription("Absolute returns the current procfs values, relative calculates the usage between triggers") + ->withAllowableValues<std::string>(ResultRelativeness::values()) + ->withDefaultValue(toString(ResultRelativeness::ABSOLUTE))->build()); + + +void ProcFsMonitor::initialize() { + setSupportedProperties({OutputFormatProperty, OutputCompactnessProperty, DecimalPlaces, ResultRelativenessProperty}); + setSupportedRelationships({ProcFsMonitor::Success}); +} + +void ProcFsMonitor::onSchedule(const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::ProcessSessionFactory>&) { + gsl_Expects(context); + context->getProperty(OutputFormatProperty.getName(), output_format_); + context->getProperty(OutputCompactnessProperty.getName(), output_compactness_); + context->getProperty(ResultRelativenessProperty.getName(), result_relativeness_); + setupDecimalPlacesFromProperties(*context); +} + +namespace { +size_t number_of_cores(const std::vector<std::pair<std::string, CpuStatData>>& cpu_stat) { + gsl_Expects(cpu_stat.size() > 1); + return cpu_stat.size() - 1; +} + +bool cpu_stats_are_valid(const std::vector<std::pair<std::string, CpuStatData>>& cpu_stat) { + return cpu_stat.size() > 1 && cpu_stat[0].first == "cpu"; // needs the aggregate and at least one core information to be valid +} + +std::optional<std::chrono::duration<double>> getAggregateCpuDiff(std::vector<std::pair<std::string, CpuStatData>>& current_cpu_stats, + std::vector<std::pair<std::string, CpuStatData>>& last_cpu_stats) { + if (current_cpu_stats.size() != last_cpu_stats.size()) + return std::nullopt; + if (!cpu_stats_are_valid(current_cpu_stats) || !cpu_stats_are_valid(last_cpu_stats)) + return std::nullopt; + + return (current_cpu_stats[0].second.getTotal() - last_cpu_stats[0].second.getTotal()) / number_of_cores(current_cpu_stats); +} +} // namespace + +void ProcFsMonitor::onTrigger(core::ProcessContext*, core::ProcessSession* session) { + gsl_Expects(session); + std::shared_ptr<core::FlowFile> flowFile = session->create(); + if (!flowFile) { + logger_->log_error("Failed to create flowfile!"); + yield(); + return; Review Comment: I am conflicted about this, some processors check if `create` succeeds some don't, I'm leaning towards assuming that it does, what do you think? -- 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]
