// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.maintenance;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.Deployer;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.autoscale.Autoscaler;
import com.yahoo.vespa.hosted.provision.autoscale.ClusterResources;
import com.yahoo.vespa.hosted.provision.autoscale.NodeMetricsDb;
import com.yahoo.vespa.hosted.provision.provisioning.HostResourcesCalculator;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Maintainer making automatic scaling decisions
 *
 * @author bratseth
 */
public class AutoscalingMaintainer extends Maintainer {

    private final Autoscaler autoscaler;
    private final Deployer deployer;

    public AutoscalingMaintainer(NodeRepository nodeRepository,
                                 HostResourcesCalculator hostResourcesCalculator,
                                 NodeMetricsDb metricsDb,
                                 Deployer deployer,
                                 Duration interval) {
        super(nodeRepository, interval);
        this.autoscaler = new Autoscaler(hostResourcesCalculator, metricsDb, nodeRepository);
        this.deployer = deployer;
    }

    @Override
    protected void maintain() {
        if ( ! nodeRepository().zone().environment().isProduction()) return;

        activeNodesByApplication().forEach((applicationId, nodes) -> autoscale(applicationId, nodes));
    }

    private void autoscale(ApplicationId application, List<Node> applicationNodes) {
        MaintenanceDeployment deployment = new MaintenanceDeployment(application, deployer, nodeRepository());
        if ( ! deployment.isValid()) return; // Another config server will consider this application
        nodesByCluster(applicationNodes).forEach((clusterSpec, clusterNodes) -> {
            Optional<ClusterResources> target = autoscaler.autoscale(application, clusterSpec, clusterNodes);
            target.ifPresent(t -> log.info("Autoscale: Application " + application + " cluster " + clusterSpec +
                                           " from " + applicationNodes.size() + " * " + applicationNodes.get(0).flavor().resources() +
                                           " to " + t.nodes() + " * " + t.nodeResources()));
        });
    }

    private Map<ClusterSpec, List<Node>> nodesByCluster(List<Node> applicationNodes) {
        return applicationNodes.stream().collect(Collectors.groupingBy(n -> n.allocation().get().membership().cluster()));
    }

}

@@ -1842,16 +1842,21 @@
         
+try (
 Maintena
@@ -1948,26 +1948,31 @@
 itory())
-;%0A
+) %7B%0A   
         
 if ( ! d
@@ -1959,24 +1959,25 @@
 %0A           
+ 
 if ( ! deplo
@@ -2065,16 +2065,20 @@
         
+    
 nodesByC
@@ -2155,16 +2155,20 @@
         
+ 
+   
 Optional
@@ -2269,16 +2269,20 @@
         
+   
+ 
 target.i
@@ -2374,16 +2374,20 @@
 rSpec +%0A
+    
         
@@ -2556,16 +2556,20 @@
         
+    
 %22 to %22 +
@@ -2622,12 +2622,26 @@
     
+   
+ 
 %7D);%0A
+        %7D%0A
     

Create MaintenanceDeployment in try-with-resources