markap14 commented on a change in pull request #3681: NIFI-6510 - Analytics 
framework
URL: https://github.com/apache/nifi/pull/3681#discussion_r320929176
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/status/analytics/StatusAnalyticsModelMapFactory.java
 ##########
 @@ -0,0 +1,126 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.controller.status.analytics;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import org.apache.nifi.controller.status.history.StatusHistoryUtil;
+import org.apache.nifi.nar.ExtensionManager;
+import org.apache.nifi.nar.NarThreadContextClassLoader;
+import org.apache.nifi.util.NiFiProperties;
+import org.apache.nifi.util.Tuple;
+import org.apache.nifi.web.api.dto.status.StatusHistoryDTO;
+import org.apache.nifi.web.api.dto.status.StatusSnapshotDTO;
+
+/**
+ * <p>
+ * This factory supports the creation of models and their associated 
extraction functions
+ * </p>
+ */
+public class StatusAnalyticsModelMapFactory {
+
+    private final static String QUEUED_COUNT_METRIC = "queuedCount";
+    private final static String QUEUED_BYTES_METRIC = "queuedBytes";
+    private final static String INPUT_COUNT_METRIC = "inputCount";
+    private final static String INPUT_BYTES_METRIC = "inputBytes";
+    private final static String OUTPUT_COUNT_METRIC = "outputCount";
+    private final static String OUTPUT_BYTES_METRIC = "outputBytes";
+
+    /**
+     * Return mapping of models and extraction functions for connection status 
analytics prediction instances
+     * @param extensionManager Extension Manager object for instantiating 
classes
+     * @param niFiProperties NiFi Properties object
+     * @return
+     */
+    public static Map<String, Tuple<StatusAnalyticsModel, 
StatusMetricExtractFunction>> getConnectionStatusModelMap(ExtensionManager 
extensionManager, NiFiProperties niFiProperties){
+            Map<String, Tuple<StatusAnalyticsModel, 
StatusMetricExtractFunction>> modelMap = new HashMap<>();
+            StatusMetricExtractFunction extract = 
getConnectionStatusExtractFunction();
+            Tuple<StatusAnalyticsModel, StatusMetricExtractFunction> 
countModelFunction = new Tuple<>(createModelInstance(extensionManager, 
niFiProperties), extract);
+            Tuple<StatusAnalyticsModel, StatusMetricExtractFunction> 
byteModelFunction = new Tuple<>(createModelInstance(extensionManager, 
niFiProperties), extract);
+            modelMap.put(QUEUED_COUNT_METRIC, countModelFunction);
+            modelMap.put(QUEUED_BYTES_METRIC, byteModelFunction);
+            return modelMap;
+    }
+
+    /**
+     * Create a connection model instance  using configurations set in NiFi 
properties
+     * @param extensionManager Extension Manager object for instantiating 
classes
+     * @param nifiProperties NiFi Properties object
+     * @return statusAnalyticsModel
+     */
+    private static StatusAnalyticsModel createModelInstance(ExtensionManager 
extensionManager, NiFiProperties nifiProperties) {
+        final String implementationClassName = 
nifiProperties.getProperty(NiFiProperties.ANALYTICS_CONNECTION_MODEL_IMPLEMENTATION,
 NiFiProperties.DEFAULT_ANALYTICS_CONNECTION_MODEL_IMPLEMENTATION);
+        if (implementationClassName == null) {
+            throw new RuntimeException("Cannot create Analytics Model because 
the NiFi Properties is missing the following property: "
+                    + 
NiFiProperties.ANALYTICS_CONNECTION_MODEL_IMPLEMENTATION);
+        }
+        try {
+            return 
NarThreadContextClassLoader.createInstance(extensionManager, 
implementationClassName, StatusAnalyticsModel.class, nifiProperties);
+        } catch (final Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Get a connection status extract function instance
+     * @return StatusMetricExtractFunction
+     */
+    private static StatusMetricExtractFunction 
getConnectionStatusExtractFunction() {
+
+        return (metric, statusHistory) -> {
 
 Review comment:
   Again, we should avoid the use of the Functional-style lambda when it 
doesn't save us much. Google's documentation of Guava does a great job of 
explaining the tradeoffs to consider when contemplating whether Functional or 
Imperative style should be used: 
https://github.com/google/guava/wiki/FunctionalExplained

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to