package com.dianping.cat.consumer.dump;

import java.util.concurrent.TimeUnit;

import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.unidal.cat.message.storage.MessageDumper;
import org.unidal.cat.message.storage.MessageDumperManager;
import org.unidal.cat.message.storage.MessageFinderManager;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;

import com.dianping.cat.analysis.AbstractMessageAnalyzer;
import com.dianping.cat.analysis.MessageAnalyzer;
import com.dianping.cat.helper.TimeHelper;
import com.dianping.cat.message.internal.MessageId;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.report.ReportManager;
import com.dianping.cat.statistic.ServerStatisticManager;

@Named(type = MessageAnalyzer.class, value = DumpAnalyzer.ID, instantiationStrategy = Named.PER_LOOKUP)
public class DumpAnalyzer extends AbstractMessageAnalyzer<Object> implements LogEnabled {
	public static final String ID = "dump";

	@Inject
	private ServerStatisticManager m_serverStateManager;

	@Inject
	private MessageDumperManager m_dumperManager;

	@Inject
	private MessageFinderManager m_finderManager;

	private MessageDumper m_dumper;

	private Logger m_logger;

	@Override
	public synchronized void doCheckpoint(boolean atEnd) {
		try {
			int hour = (int) TimeUnit.MILLISECONDS.toHours(m_startTime);

			m_dumperManager.close(hour);
			m_finderManager.close(hour);
		} catch (Exception e) {
			m_logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void enableLogging(Logger logger) {
		m_logger = logger;
	}

	@Override
	public int getAnanlyzerCount() {
		return 2;
	}

	@Override
	public Object getReport(String domain) {
		throw new UnsupportedOperationException("This should not be called!");
	}

	@Override
	public ReportManager<?> getReportManager() {
		return null;
	}

	@Override
	public void initialize(long startTime, long duration, long extraTime) {
		int hour = (int) TimeUnit.MILLISECONDS.toHours(startTime);

		m_extraTime = extraTime;
		m_startTime = startTime;
		m_duration = duration;
		m_dumper = m_dumperManager.findOrCreate(hour);
	}

	@Override
	protected void loadReports() {
		// do nothing
	}

	@Override
	public void process(MessageTree tree) {
		String domain = tree.getDomain();

		if ("PhoenixAgent".equals(domain)) {
			return;
		} else {
			MessageId messageId = MessageId.parse(tree.getMessageId());

			long time = tree.getMessage().getTimestamp();
			long fixedTime = time - time % (TimeHelper.ONE_HOUR);
			long idTime = messageId.getTimestamp();
			long duration = fixedTime - idTime;

			if (duration == 0 || duration == ONE_HOUR || duration == -ONE_HOUR) {
				m_dumper.process(tree);
			} else {
				m_serverStateManager.addPigeonTimeError(1);
			}
		}
	}

	public void setServerStateManager(ServerStatisticManager serverStateManager) {
		m_serverStateManager = serverStateManager;
	}
}

@@ -383,51 +383,8 @@
 ect;
-%0Aimport org.unidal.lookup.annotation.Named;
 %0A%0Aim
@@ -443,101 +443,8 @@
 er;%0A
-import com.dianping.cat.analysis.MessageAnalyzer;%0Aimport com.dianping.cat.helper.TimeHelper;%0A
 impo
@@ -649,112 +649,8 @@
 r;%0A%0A
-@Named(type = MessageAnalyzer.class, value = DumpAnalyzer.ID, instantiationStrategy = Named.PER_LOOKUP)%0A
 publ
@@ -959,42 +959,8 @@
 r;%0A%0A
-%09private MessageDumper m_dumper;%0A%0A
 %09pri
@@ -1342,69 +1342,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public int getAnanlyzerCount() %7B%0A%09%09return 2;%0A%09%7D%0A%0A
 %09@Ov
@@ -1548,286 +1548,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public void initialize(long startTime, long duration, long extraTime) %7B%0A%09%09int hour = (int) TimeUnit.MILLISECONDS.toHours(startTime);%0A%0A%09%09m_extraTime = extraTime;%0A%09%09m_startTime = startTime;%0A%09%09m_duration = duration;%0A%09%09m_dumper = m_dumperManager.findOrCreate(hour);%0A%09%7D%0A%0A
 %09@Ov
@@ -1824,275 +1824,127 @@
 ));%0A
-%0A
 %09%09%09
-long time = tree.getM
+int hour = m
 essage
-()
+Id
 .get
-Timestamp();%0A%09%09%09long fixedTime = time - time %25 (TimeHelper.ONE_HOUR);%0A%09%09%09long idTime = messageId.getTimestamp();%0A%09%09%09long duration = fixedTime - idTime;%0A%0A%09%09%09if (duration == 0 %7C%7C duration == ONE_HOUR %7C%7C duration == -ONE_HOUR
+Hour();%0A%09%09%09MessageDumper dumper = m_dumperManager.find(hour);%0A%0A%09%09%09if (dumper != null
 ) %7B%0A%09%09%09%09
 m_du
@@ -1939,18 +1939,16 @@
 ) %7B%0A%09%09%09%09
-m_
 dumper.p
@@ -2162,10 +2162,251 @@
 ger;%0A%09%7D%0A
+%0A%09@Override%0A%09public void initialize(long startTime, long duration, long extraTime) %7B%0A%09%09super.initialize(startTime, duration, extraTime);%0A%0A%09%09int hour = (int) TimeUnit.MILLISECONDS.toHours(startTime);%0A%09%09m_dumperManager.findOrCreate(hour);%0A%09%7D%0A%0A
 %7D%0A

refactor dump analyzer