You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by sz...@apache.org on 2023/02/08 19:33:58 UTC

[nifi-minifi-cpp] 03/04: MINIFICPP-2009 CWEL should add resolved attributes with json output as well

This is an automated email from the ASF dual-hosted git repository.

szaszm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git

commit 3c44ebaff0ace8454b2245ee3fe23fa484cd4a5a
Author: Martin Zink <ma...@apache.org>
AuthorDate: Wed Feb 8 16:28:55 2023 +0100

    MINIFICPP-2009 CWEL should add resolved attributes with json output as well
    
    Closes #1482
    Signed-off-by: Marton Szasz <sz...@apache.org>
---
 .../windows-event-log/ConsumeWindowsEventLog.cpp   | 54 ++++++++++------------
 .../windows-event-log/ConsumeWindowsEventLog.h     |  9 +++-
 .../tests/ConsumeWindowsEventLogTests.cpp          | 12 +++++
 3 files changed, 45 insertions(+), 30 deletions(-)

diff --git a/extensions/windows-event-log/ConsumeWindowsEventLog.cpp b/extensions/windows-event-log/ConsumeWindowsEventLog.cpp
index 714023acd..fa48bf2ac 100644
--- a/extensions/windows-event-log/ConsumeWindowsEventLog.cpp
+++ b/extensions/windows-event-log/ConsumeWindowsEventLog.cpp
@@ -629,10 +629,10 @@ nonstd::expected<EventRender, std::string> ConsumeWindowsEventLog::createEventRe
   if (output_.xml || output_.json) {
     substituteXMLPercentageItems(doc);
     logger_->log_trace("Finish substituting %% in XML");
+  }
 
-    if (resolve_as_attributes_) {
-      result.matched_fields = walker.getFieldValues();
-    }
+  if (resolve_as_attributes_) {
+    result.matched_fields = walker.getFieldValues();
   }
 
   if (output_.xml) {
@@ -699,42 +699,38 @@ void ConsumeWindowsEventLog::refreshTimeZoneData() {
 }
 
 void ConsumeWindowsEventLog::putEventRenderFlowFileToSession(const EventRender& eventRender, core::ProcessSession& session) const {
-  auto commitFlowFile = [&] (const std::shared_ptr<core::FlowFile>& flowFile, const std::string& content, const std::string& mimeType) {
-    session.writeBuffer(flowFile, content);
-    session.putAttribute(flowFile, core::SpecialFlowAttribute::MIME_TYPE, mimeType);
-    session.putAttribute(flowFile, "timezone.name", timezone_name_);
-    session.putAttribute(flowFile, "timezone.offset", timezone_offset_);
-    session.getProvenanceReporter()->receive(flowFile, provenanceUri_, getUUIDStr(), "Consume windows event logs", 0ms);
-    session.transfer(flowFile, Success);
+  auto commitFlowFile = [&] (const std::string& content, const std::string& mimeType) {
+    auto flow_file = session.create();
+    addMatchedFieldsAsAttributes(eventRender, session, flow_file);
+    session.writeBuffer(flow_file, content);
+    session.putAttribute(flow_file, core::SpecialFlowAttribute::MIME_TYPE, mimeType);
+    session.putAttribute(flow_file, "timezone.name", timezone_name_);
+    session.putAttribute(flow_file, "timezone.offset", timezone_offset_);
+    session.getProvenanceReporter()->receive(flow_file, provenanceUri_, getUUIDStr(), "Consume windows event logs", 0ms);
+    session.transfer(flow_file, Success);
   };
 
   if (output_.xml) {
-    auto flowFile = session.create();
     logger_->log_trace("Writing rendered XML to a flow file");
-
-    for (const auto &fieldMapping : eventRender.matched_fields) {
-      if (!fieldMapping.second.empty()) {
-        session.putAttribute(flowFile, fieldMapping.first, fieldMapping.second);
-      }
-    }
-
-    commitFlowFile(flowFile, eventRender.xml, "application/xml");
+    commitFlowFile(eventRender.xml, "application/xml");
   }
 
   if (output_.plaintext) {
     logger_->log_trace("Writing rendered plain text to a flow file");
-    commitFlowFile(session.create(), eventRender.plaintext, "text/plain");
+    commitFlowFile(eventRender.plaintext, "text/plain");
   }
 
-  if (output_.json.type == JSONType::Raw) {
-    logger_->log_trace("Writing rendered raw JSON to a flow file");
-    commitFlowFile(session.create(), eventRender.json, "application/json");
-  } else if (output_.json.type == JSONType::Simple) {
-    logger_->log_trace("Writing rendered simple JSON to a flow file");
-    commitFlowFile(session.create(), eventRender.json, "application/json");
-  } else if (output_.json.type == JSONType::Flattened) {
-    logger_->log_trace("Writing rendered flattened JSON to a flow file");
-    commitFlowFile(session.create(), eventRender.json, "application/json");
+  if (output_.json) {
+    logger_->log_trace("Writing rendered %s JSON to a flow file", output_.json.type.toString());
+    commitFlowFile(eventRender.json, "application/json");
+  }
+}
+
+void ConsumeWindowsEventLog::addMatchedFieldsAsAttributes(const EventRender& eventRender, core::ProcessSession& session, const std::shared_ptr<core::FlowFile>& flowFile) const {
+  for (const auto &fieldMapping : eventRender.matched_fields) {
+    if (!fieldMapping.second.empty()) {
+      session.putAttribute(flowFile, fieldMapping.first, fieldMapping.second);
+    }
   }
 }
 
diff --git a/extensions/windows-event-log/ConsumeWindowsEventLog.h b/extensions/windows-event-log/ConsumeWindowsEventLog.h
index ef430204c..258846b1e 100644
--- a/extensions/windows-event-log/ConsumeWindowsEventLog.h
+++ b/extensions/windows-event-log/ConsumeWindowsEventLog.h
@@ -42,6 +42,7 @@
 #include "FlowFileRecord.h"
 #include "concurrentqueue.h"
 #include "pugixml.hpp"
+#include "utils/Enum.h"
 #include "utils/Export.h"
 #include "utils/RegexUtils.h"
 
@@ -145,6 +146,8 @@ class ConsumeWindowsEventLog : public core::Processor {
                                                     const std::shared_ptr<core::ProcessSession>& session,
                                                     const EVT_HANDLE& event_query_results);
 
+  void addMatchedFieldsAsAttributes(const EventRender &eventRender, core::ProcessSession &session, const std::shared_ptr<core::FlowFile> &flowFile) const;
+
   std::shared_ptr<core::logging::Logger> logger_;
   core::StateManager* state_manager_{nullptr};
   wel::METADATA_NAMES header_names_;
@@ -161,7 +164,11 @@ class ConsumeWindowsEventLog : public core::Processor {
   std::map<std::string, wel::WindowsEventLogHandler> providers_;
   uint64_t batch_commit_size_{};
 
-  enum class JSONType { None, Raw, Simple, Flattened };
+  SMART_ENUM(JSONType,
+      (None, "None"),
+      (Raw, "Raw"),
+      (Simple, "Simple"),
+      (Flattened, "Flattened"))
 
   struct OutputFormat {
     bool xml{false};
diff --git a/extensions/windows-event-log/tests/ConsumeWindowsEventLogTests.cpp b/extensions/windows-event-log/tests/ConsumeWindowsEventLogTests.cpp
index c16767ee5..c5f9d052d 100644
--- a/extensions/windows-event-log/tests/ConsumeWindowsEventLogTests.cpp
+++ b/extensions/windows-event-log/tests/ConsumeWindowsEventLogTests.cpp
@@ -238,6 +238,18 @@ TEST_CASE("ConsumeWindowsEventLog extracts some attributes by default", "[onTrig
   auto logger_processor = test_plan->addProcessor("LogAttribute", "logger", Success, true);
   test_plan->setProperty(logger_processor, LogAttribute::FlowFilesToLog.getName(), "0");
 
+  SECTION("XML output") {
+    REQUIRE(test_plan->setProperty(cwel_processor, ConsumeWindowsEventLog::OutputFormat.getName(), "XML"));
+  }
+
+  SECTION("Json output") {
+    REQUIRE(test_plan->setProperty(cwel_processor, ConsumeWindowsEventLog::OutputFormat.getName(), "JSON"));
+  }
+
+  SECTION("Plaintext output") {
+    REQUIRE(test_plan->setProperty(cwel_processor, ConsumeWindowsEventLog::OutputFormat.getName(), "Plaintext"));
+  }
+
   // 0th event, only to create a bookmark
   {
     reportEvent(APPLICATION_CHANNEL, "Event zero: this is in the past");