package io.highway.to.urhell.service;

import io.highway.to.urhell.dao.BreakerLogDao;
import io.highway.to.urhell.dao.ThunderStatDao;
import io.highway.to.urhell.domain.EntryPathData;
import io.highway.to.urhell.domain.ThunderApp;
import io.highway.to.urhell.domain.ThunderStat;
import io.highway.to.urhell.rest.domain.MessageStat;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

@Named
@Lazy(false)
public class ThunderStatService {
	private static final Logger LOG = LoggerFactory
			.getLogger(ThunderStatService.class);

	@Inject
	private ThunderStatDao thunderStatDao;
	@Inject
	private BreakerLogDao breakerLogDao;

	public MessageStat analysisStat(String token){
		MessageStat ms = new MessageStat();
		List<ThunderStat> listThunderStat = thunderStatDao.findByToken(token);
		ms.setListThunderStat(listThunderStat);
		ms.setTotalStat(listThunderStat.size());
		Integer falsePositive = 0;
		Integer totalNoTest = 0;
		for(ThunderStat ts : listThunderStat){
			if(ts.getFalsePositive()){
				falsePositive++;
			}
			if(ts.getCount()==0){
				totalNoTest++;
			}
		}
		ms.setTotalFalsePositive(falsePositive);
		ms.setTotalNoTest(totalNoTest);
		ms.setToken(token);
		return ms;
	}
	
	@Transactional
	public void createOrUpdateThunderStat(EntryPathData entry,
			ThunderApp ta) {
		String className=entry.getClassName();
		String methodName=entry.getMethodName();
		String uri = entry.getUri();
		String httpmethod=entry.getHttpMethod().name();
		String pathClassMethodName = className + "." + methodName;
		ThunderStat ts = thunderStatDao.findByPathClassMethodNameAndToken(
				pathClassMethodName, ta.getToken());
		if (ts == null) {
			ts = new ThunderStat();
			ts.setPathClassMethodName(pathClassMethodName);
			ts.setThunderApp(ta);
			ts.setCount(new Long(0));
			ts.setHttpmethod(httpmethod);
			ts.setUri(uri);
			thunderStatDao.save(ts);
		} else {
			ts.setCount(new Long(0));
		}

	}

	@Transactional
	public void updateThunderStatFalsePositive(String id,String status){
		ThunderStat ts = thunderStatDao.findOne(id);
		Boolean res = !Boolean.valueOf(status);
		ts.setFalsePositive(res);
		thunderStatDao.save(ts);
	}
	
	@Scheduled(fixedDelay = 60 * 1000)
	public void updateThunderApp() {
		LOG.info("Launch schedule updateThunderApp");
		List<ThunderStat> listThunderStat = thunderStatDao.findAll();
		for (ThunderStat ts : listThunderStat) {
			Long count = breakerLogDao.findByPathClassMethodNameAndToken(
					ts.getPathClassMethodName(), ts.getThunderApp().getToken());
			ts.setCount(count);
			thunderStatDao.save(ts);
			LOG.info("save thunderStat " + ts.getPathClassMethodName());
		}

	}

}

@@ -536,69 +536,8 @@
 y;%0D%0A
-import org.springframework.scheduling.annotation.Scheduled;%0D%0A
 impo
@@ -598,16 +598,16 @@
 ional;%0D%0A
+
 %0D%0A@Named
@@ -851,18 +851,19 @@
 ogDao;%0D%0A
+%09
 %0D%0A
-
 %09public 
@@ -938,24 +938,68 @@
 ageStat();%0D%0A
+%09%09LOG.info(%22analysis for token %7B%7D%22,token);%0D%0A
 %09%09List%3CThund
@@ -1056,24 +1056,230 @@
 en(token);%0D%0A
+%09%09for (ThunderStat ts : listThunderStat) %7B%0D%0A%09%09%09Long count = breakerLogDao.findByPathClassMethodNameAndToken(%0D%0A%09%09%09%09%09ts.getPathClassMethodName(), ts.getThunderApp().getToken());%0D%0A%09%09%09ts.setCount(count);%0D%0A%09%09%7D%0D%0A
 %09%09ms.setList
@@ -1692,16 +1692,19 @@
 %0A%09%7D%0D%0A%09%0D%0A
+%09%0D%0A
 %09@Transa
@@ -2676,504 +2676,10 @@
 %09%7D%0D%0A
+
 %09%0D%0A
-%09@Scheduled(fixedDelay = 60 * 1000)%0D%0A%09public void updateThunderApp() %7B%0D%0A%09%09LOG.info(%22Launch schedule updateThunderApp%22);%0D%0A%09%09List%3CThunderStat%3E listThunderStat = thunderStatDao.findAll();%0D%0A%09%09for (ThunderStat ts : listThunderStat) %7B%0D%0A%09%09%09Long count = breakerLogDao.findByPathClassMethodNameAndToken(%0D%0A%09%09%09%09%09ts.getPathClassMethodName(), ts.getThunderApp().getToken());%0D%0A%09%09%09ts.setCount(count);%0D%0A%09%09%09thunderStatDao.save(ts);%0D%0A%09%09%09LOG.info(%22save thunderStat %22 + ts.getPathClassMethodName());%0D%0A%09%09%7D%0D%0A%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

change the update, no scheduling -> manual only