package com.dianping.cat.report.service.impl;

import java.util.Date;

import org.unidal.dal.jdbc.DalException;
import org.unidal.dal.jdbc.DalNotFoundException;

import com.dianping.cat.Cat;
import com.dianping.cat.Constants;
import com.dianping.cat.core.dal.DailyReport;
import com.dianping.cat.core.dal.DailyReportEntity;
import com.dianping.cat.home.dal.report.DailyReportContent;
import com.dianping.cat.home.dal.report.DailyReportContentEntity;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.transform.DefaultNativeParser;
import com.dianping.cat.report.service.AbstractReportService;

public class RouterConfigService extends AbstractReportService<RouterConfig> {

	@Override
	public RouterConfig makeReport(String domain, Date start, Date end) {
		RouterConfig report = new RouterConfig(domain);

		report.setStartTime(start);
		report.setEndTime(end);
		return report;
	}

	@Override
	public RouterConfig queryDailyReport(String domain, Date start, Date end) {
		String name = Constants.REPORT_ROUTER;

		try {
			DailyReport report = m_dailyReportDao.findByDomainNamePeriod(domain, name, start,
			      DailyReportEntity.READSET_FULL);
			RouterConfig config = queryFromDailyBinary(report.getId(), domain);

			return config;
		} catch (DalNotFoundException e) {
			// ignore
		} catch (Exception e) {
			Cat.logError(e);
		}
		return null;
	}

	private RouterConfig queryFromDailyBinary(int id, String domain) throws DalException {
		DailyReportContent content = m_dailyReportContentDao.findByPK(id, DailyReportContentEntity.READSET_FULL);

		if (content != null) {
			return DefaultNativeParser.parse(content.getContent());
		} else {
			return null;
		}
	}

	@Override
	public RouterConfig queryHourlyReport(String domain, Date start, Date end) {
		throw new RuntimeException("router report don't support hourly report");
	}

	@Override
	public RouterConfig queryMonthlyReport(String domain, Date start) {
		throw new RuntimeException("router report don't support monthly report");
	}

	@Override
	public RouterConfig queryWeeklyReport(String domain, Date start) {
		throw new RuntimeException("router report don't support weekly report");
	}

}

@@ -62,16 +62,70 @@
 il.Date;
+%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;
 %0A%0Aimport
@@ -764,16 +764,103 @@
 fig%3E %7B%0A%0A
+%09private Map%3CLong, RouterConfig%3E m_configs = new LinkedHashMap%3CLong, RouterConfig%3E();%0A%0A
 %09@Overri
@@ -1149,16 +1149,118 @@
  end) %7B%0A
+%09%09long time = start.getTime();%0A%09%09RouterConfig config = m_configs.get(time);%0A%0A%09%09if (config == null) %7B%0A%09
 %09%09String
@@ -1299,14 +1299,16 @@
 %0A%0A%09%09
+%09
 try %7B%0A
+%09
 %09%09%09D
@@ -1391,16 +1391,17 @@
 art,%0A%09%09%09
+%09
       Da
@@ -1434,29 +1434,17 @@
 LL);%0A%09%09%09
-RouterConfig 
+%09
 config =
@@ -1491,16 +1491,50 @@
 main);%0A%0A
+%09%09%09%09m_configs.put(time, config);%0A%09
 %09%09%09retur
@@ -1543,16 +1543,17 @@
 config;%0A
+%09
 %09%09%7D catc
@@ -1584,16 +1584,17 @@
 e) %7B%0A%09%09%09
+%09
 // ignor
@@ -1595,16 +1595,17 @@
  ignore%0A
+%09
 %09%09%7D catc
@@ -1625,16 +1625,17 @@
 e) %7B%0A%09%09%09
+%09
 Cat.logE
@@ -1645,18 +1645,20 @@
 r(e);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09return
@@ -1664,16 +1664,49 @@
 n null;%0A
+%09%09%7D else %7B%0A%09%09%09return config;%0A%09%09%7D%0A
 %09%7D%0A%0A%09pri

modify the cache