martinzink commented on code in PR #1152: URL: https://github.com/apache/nifi-minifi-cpp/pull/1152#discussion_r847434377
########## 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 all for removing boilerplate code :+1: so if thats acceptable then I'll remove it aswell https://github.com/apache/nifi-minifi-cpp/pull/1152/commits/788817423251101ee68dc79a708e1b9dcc0a2939#diff-d776bd6998730fd9d2699bfd677cabc117092a8692c9b1a73392e7ba5310a529L98-L102 ########## extensions/procfs/ProcessStat.cpp: ########## @@ -0,0 +1,54 @@ +/** + * 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 "ProcessStat.h" +#include <sstream> + +namespace org::apache::nifi::minifi::extensions::procfs { + +std::optional<ProcessStatData> ProcessStatData::parseProcessStatFile(std::istream& stat_file) { + std::stringstream stat_stream; + copy(std::istreambuf_iterator<char>(stat_file), + std::istreambuf_iterator<char>(), + std::ostreambuf_iterator<char>(stat_stream)); + + ProcessStatData process_stat_data; + stat_stream >> process_stat_data.pid_; + + std::string stat_stream_string = stat_stream.str(); + size_t comm_start = stat_stream_string.find_first_of('('); + size_t comm_end = stat_stream_string.find_last_of(')'); + if (comm_start == std::string::npos || comm_end == std::string::npos) + return std::nullopt; + process_stat_data.comm_ = stat_stream_string.substr(comm_start+1, comm_end-comm_start-1); + stat_stream.seekg(comm_end+2); Review Comment: yeah there is a mandatory space after the `)`, but you are right this would work just as good with `comm_end+1` (because istream skips the whitespaces) Should I change it? -- 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]
