markap14 commented on a change in pull request #3681: NIFI-6510 - Analytics framework URL: https://github.com/apache/nifi/pull/3681#discussion_r320909369
########## File path: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/status/analytics/ConnectionStatusAnalytics.java ########## @@ -0,0 +1,390 @@ +/* + * 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.NoSuchElementException; +import java.util.Optional; +import java.util.stream.Stream; + +import org.apache.commons.lang3.ArrayUtils; +import org.apache.nifi.connectable.Connection; +import org.apache.nifi.controller.flow.FlowManager; +import org.apache.nifi.controller.repository.FlowFileEvent; +import org.apache.nifi.controller.repository.FlowFileEventRepository; +import org.apache.nifi.controller.repository.RepositoryStatusReport; +import org.apache.nifi.controller.status.history.ComponentStatusRepository; +import org.apache.nifi.controller.status.history.StatusHistory; +import org.apache.nifi.groups.ProcessGroup; +import org.apache.nifi.processor.DataUnit; +import org.apache.nifi.util.Tuple; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.primitives.Doubles; +/** + * <p> + * An implementation of {@link StatusAnalytics} that is provides Connection related analysis/prediction for a given connection instance + * </p> + */ +public class ConnectionStatusAnalytics implements StatusAnalytics { + + private static final Logger LOG = LoggerFactory.getLogger(ConnectionStatusAnalytics.class); + private final Map<String, Tuple<StatusAnalyticsModel, StatusMetricExtractFunction>> modelMap; + private QueryWindow queryWindow; + private final ComponentStatusRepository componentStatusRepository; + private final FlowFileEventRepository flowFileEventRepository; + private final String connectionIdentifier; + private final FlowManager flowManager; + private final Boolean supportOnlineLearning; + private Boolean extendWindow = false; + private long intervalMillis = 3L * 60 * 1000; // Default is 3 minutes + private String scoreName = "rSquared"; + private double scoreThreshold = .90; + + public ConnectionStatusAnalytics(ComponentStatusRepository componentStatusRepository, FlowManager flowManager, FlowFileEventRepository flowFileEventRepository, + Map<String, Tuple<StatusAnalyticsModel, StatusMetricExtractFunction>> modelMap, String connectionIdentifier, Boolean supportOnlineLearning) { + this.componentStatusRepository = componentStatusRepository; + this.flowManager = flowManager; + this.flowFileEventRepository = flowFileEventRepository; + this.modelMap = modelMap; + this.connectionIdentifier = connectionIdentifier; + this.supportOnlineLearning = supportOnlineLearning; + } + + /** + * Retrieve observations and train available model(s) + */ + public void refresh() { + + if (supportOnlineLearning && this.queryWindow != null) { + //Obtain latest observations when available, extend window if needed to obtain minimum observations + this.queryWindow = new QueryWindow(extendWindow ? queryWindow.getStartTimeMillis() : queryWindow.getEndTimeMillis(), System.currentTimeMillis()); + } else { + this.queryWindow = new QueryWindow(System.currentTimeMillis() - getIntervalTimeMillis(), System.currentTimeMillis()); + } + + modelMap.forEach((metric, modelFunction) -> { Review comment: It is generally preferable to use imperative style coding over functional coding, if there's clear benefit to the functional style. In this case, it means that we're creating this lambda inline, which doesn't perform as well, is more complex to debug due to how IDE's enable stepping into the lambda, and provides more cryptic stack traces. A simple for-loop would be preferred. ---------------------------------------------------------------- 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
