package com.dianping.cat.report.page.state;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.unidal.lookup.annotation.Inject;

import com.dianping.cat.Constants;
import com.dianping.cat.consumer.state.StateAnalyzer;
import com.dianping.cat.consumer.state.model.entity.Machine;
import com.dianping.cat.consumer.state.model.entity.StateReport;
import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.report.service.ModelService;
import com.dianping.cat.service.ModelRequest;
import com.dianping.cat.service.ModelResponse;
import com.dianping.cat.system.config.RouterConfigManager;

public class StateBuilder {

	@Inject
	private RouterConfigManager m_routerManager;

	@Inject(type = ModelService.class, value = StateAnalyzer.ID)
	private ModelService<StateReport> m_stateService;
	
	private static final int COUNT = 500 * 10000;

	private List<String> queryAllServers() {
		List<String> strs = new ArrayList<String>();
		String backUpServer = m_routerManager.getRouterConfig().getBackupServer();
		List<DefaultServer> servers = m_routerManager.getRouterConfig().getDefaultServers();

		for (DefaultServer server : servers) {
			strs.add(server.getId());
		}
		strs.add(backUpServer);
		return strs;
	}

	public String buildStateMessage(long date, String ip) {
		StateReport report = queryHourlyReport(date, ip);

		int realSize = report.getMachines().size();
		List<String> servers = queryAllServers();
		int excepeted = servers.size();
		Set<String> errorServers = new HashSet<String>();

		if (realSize != excepeted) {
			for (String serverIp : servers) {
				if (report.getMachines().get(serverIp) == null) {
					errorServers.add(serverIp);
				}
			}
		}
		for (Machine machine : report.getMachines().values()) {
			if (machine.getTotalLoss() > COUNT) {
				errorServers.add(machine.getIp());
			}
		}

		if (errorServers.size() > 0) {
			return errorServers.toString();
		} else {
			return null;
		}
	}

	private StateReport queryHourlyReport(long date, String ip) {
		String domain = Constants.CAT;
		ModelRequest request = new ModelRequest(domain, date) //
		      .setProperty("ip", ip);

		if (m_stateService.isEligable(request)) {
			ModelResponse<StateReport> response = m_stateService.invoke(request);

			return response.getModel();
		} else {
			throw new RuntimeException("Internal error: no eligable sql service registered for " + request + "!");
		}
	}
}

@@ -860,17 +860,16 @@
 ervice;%0A
-%09
 %0A%09privat
@@ -1392,16 +1392,41 @@
 , ip);%0A%0A
+%09%09if (report != null) %7B%0A%09
 %09%09int re
@@ -1459,24 +1459,25 @@
 s().size();%0A
+%09
 %09%09List%3CStrin
@@ -1510,16 +1510,17 @@
 rs();%0A%09%09
+%09
 int exce
@@ -1543,16 +1543,17 @@
 size();%0A
+%09
 %09%09Set%3CSt
@@ -1593,24 +1593,25 @@
 String%3E();%0A%0A
+%09
 %09%09if (realSi
@@ -1632,16 +1632,17 @@
 d) %7B%0A%09%09%09
+%09
 for (Str
@@ -1663,24 +1663,25 @@
  servers) %7B%0A
+%09
 %09%09%09%09if (repo
@@ -1723,24 +1723,25 @@
 ull) %7B%0A%09%09%09%09%09
+%09
 errorServers
@@ -1756,16 +1756,17 @@
 verIp);%0A
+%09
 %09%09%09%09%7D%0A%09%09
@@ -1766,22 +1766,25 @@
 %09%09%09%7D%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A
+%09
 %09%09for (M
@@ -1832,24 +1832,25 @@
 ues()) %7B%0A%09%09%09
+%09
 if (machine.
@@ -1871,24 +1871,25 @@
  %3E COUNT) %7B%0A
+%09
 %09%09%09%09errorSer
@@ -1922,17 +1922,20 @@
 %0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A%0A%09%09
+%09
 if (
@@ -1961,16 +1961,17 @@
  %3E 0) %7B%0A
+%09
 %09%09%09retur
@@ -1997,28 +1997,25 @@
 ring();%0A
+%09
 %09%09%7D
- else %7B%0A%09
+%0A%09%09%7D%0A
 %09%09return
@@ -2021,20 +2021,16 @@
 n null;%0A
-%09%09%7D%0A
 %09%7D%0A%0A%09pri

fix npe