package jp.ac.nii.prl.mape.autoscaling.analysis.service;

import java.util.Collection;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jp.ac.nii.prl.mape.autoscaling.analysis.model.Adaptation;
import jp.ac.nii.prl.mape.autoscaling.analysis.model.Deployment;
import jp.ac.nii.prl.mape.autoscaling.analysis.model.Instance;
import jp.ac.nii.prl.mape.autoscaling.analysis.repository.DeploymentRepository;

@Service("deploymentService")
public class DeploymentServiceImpl implements DeploymentService {

	@Autowired
	private DeploymentRepository deploymentRepository;
	
	@Autowired
	private InstanceService instanceService;
	
	@Override
	public Deployment save(Deployment deployment) {
		return deploymentRepository.save(deployment);
	}

	@Override
	public Collection<Deployment> findAll() {
		return deploymentRepository.findAll();
	}

	@Override
	public Optional<Deployment> findById(Integer deploymentId) {
		return deploymentRepository.findById(deploymentId);
	}

	@Override
	public Adaptation analyse(Deployment deployment) {
		double load = getAverageLoadPerCPU(deployment.getId());
		Adaptation adaptation = new Adaptation();
		if (load >= 2) {
			adaptation.setAdapt(true);
			adaptation.setScaleUp(true);
			adaptation.setCpuCount(Double.valueOf(load).intValue());
		} else if ((load <= 0.4) && (deployment.size() > 1)) {
			adaptation.setAdapt(true);
			adaptation.setScaleUp(false);
			adaptation.setCpuCount(1);
		} else {
			adaptation.setAdapt(false);
		}
		return adaptation;
	}

	@Override
	public Double getAverageLoadPerCPU(Integer deploymentId) {
		Collection<Instance> instances = instanceService.findByDeploymentId(deploymentId);
		Double load = 0d;
		for (Instance instance:instances) {
			load += instance.getInstLoad();
		}
		return load / instances.size();
	}

}

@@ -211,32 +211,113 @@
 otype.Service;%0A%0A
+import jp.ac.nii.prl.mape.autoscaling.analysis.configuration.AnalysisProperties;%0A
 import jp.ac.nii
@@ -361,24 +361,24 @@
 Adaptation;%0A
-
 import jp.ac
@@ -793,16 +793,78 @@
 vice;%0A%09%0A
+%09@Autowired%0A%09private AnalysisProperties analysisProperties;%0A%09%0A
 %09@Overri
@@ -1380,9 +1380,44 @@
  %3E= 
-2
+analysisProperties.getMaxThreshold()
 ) %7B%0A
@@ -1564,11 +1564,44 @@
  %3C= 
-0.4
+analysisProperties.getMinThreshold()
 ) &&

Use threshold properties in analysis