package com.dianping.cat.system.alarm;

import java.util.List;

import org.unidal.helper.Threads.Task;
import org.unidal.lookup.annotation.Inject;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.system.alarm.connector.Connector;
import com.dianping.cat.system.alarm.threshold.ThresholdDataEntity;
import com.dianping.cat.system.alarm.threshold.ThresholdRule;
import com.dianping.cat.system.alarm.threshold.ThresholdRuleManager;
import com.dianping.cat.system.alarm.threshold.event.ExceptionDataEvent;
import com.dianping.cat.system.alarm.threshold.event.ServiceDataEvent;
import com.dianping.cat.system.event.EventDispatcher;

public class AlarmTask implements Task {

	@Inject
	private Connector m_connector;

	@Inject
	private EventDispatcher m_dispatcher;

	@Inject
	private ThresholdRuleManager m_manager;

	@Override
	public String getName() {
		return "Exception-Service-Alarm";
	}

	@Override
	public void run() {
		boolean active = true;
		try {
			Thread.sleep(1000 * 10);
		} catch (InterruptedException e) {
			active = false;
		}

		while (active) {
			long time = System.currentTimeMillis();

			try {
				processExceptionRule();
				processServiceRule();
			} catch (Throwable e) {
				Cat.logError(e);
			}

			long duration = System.currentTimeMillis() - time;

			if (duration < 29 * 1000) {
				try {
					Thread.sleep(29 * 1000 - duration);
				} catch (InterruptedException e) {
					active = false;
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				active = false;
			}
		}
	}

	private void processServiceRule() {
		List<ThresholdRule> rules = m_manager.getAllServiceRules();
		Transaction t = Cat.newTransaction("Alarm", "ProcessServiceRule");

		for (ThresholdRule rule : rules) {
			try {
				String connectUrl = rule.getConnectUrl();
				ThresholdDataEntity entity = m_connector.fetchAlarmData(connectUrl);

				if (entity != null) {
					String domain = rule.getDomain();

					entity.setDomain(domain);
					Cat.getProducer().logEvent("AlarmRule", domain + "[" + rule.getRuleId() + "]", Event.SUCCESS,
					      entity.toString());

					ServiceDataEvent event = new ServiceDataEvent(entity);
					m_dispatcher.dispatch(event);
				}
			} catch (Exception e) {
				t.setStatus(e);
				Cat.logError(e);
			}
		}
		t.setStatus(Transaction.SUCCESS);
		t.complete();
	}

	private void processExceptionRule() {
		List<ThresholdRule> rules = m_manager.getAllExceptionRules();
		Transaction t = Cat.newTransaction("Alarm", "ProcessExceptionRule");

		for (ThresholdRule rule : rules) {
			try {
				String connectUrl = rule.getConnectUrl();
				ThresholdDataEntity entity = m_connector.fetchAlarmData(connectUrl);

				if (entity != null) {
					entity.setDomain(rule.getDomain());
					Cat.getProducer().logEvent("AlarmUrl", connectUrl, Event.SUCCESS, entity.toString());

					ExceptionDataEvent event = new ExceptionDataEvent(entity);

					m_dispatcher.dispatch(event);
				}
			} catch (Exception e) {
				t.setStatus(e);
				Cat.logError(e);
			}
		}
		t.setStatus(Transaction.SUCCESS);
		t.complete();
	}

	@Override
	public void shutdown() {
	}

}

@@ -53,24 +53,114 @@
 util.List;%0A%0A
+import org.codehaus.plexus.logging.LogEnabled;%0Aimport org.codehaus.plexus.logging.Logger;%0A
 import org.u
@@ -264,47 +264,8 @@
 at;%0A
-import com.dianping.cat.message.Event;%0A
 impo
@@ -799,16 +799,28 @@
 nts Task
+, LogEnabled
  %7B%0A%0A%09@In
@@ -957,16 +957,96 @@
 nager;%0A%0A
+%09private Logger m_logger;%0A%0A%09private static final int MAX_DURATION = 29 * 1000;%0A%0A
 %09@Overri
@@ -1349,55 +1349,167 @@
 %09%09%09%09
-processExceptionRule();%0A%09%09%09%09processServiceRule(
+m_logger.info(%22Exception-Service-Alarm Starting.%22);%0A%09%09%09%09processExceptionRule();%0A%09%09%09%09processServiceRule();%0A%09%09%09%09m_logger.info(%22Exception-Service-Alarm Finished.%22
 );%0A%09
@@ -1634,25 +1634,28 @@
 ation %3C 
-29 * 1000
+MAX_DURATION
 ) %7B%0A%09%09%09%09
@@ -1678,25 +1678,28 @@
 d.sleep(
-29 * 1000
+MAX_DURATION
  - durat
@@ -2249,235 +2249,71 @@
 %09%09%09%09
-String domain = rule.getDomain();%0A%0A%09%09%09%09%09entity.setDomain(domain);%0A%09%09%09%09%09Cat.getProducer().logEvent(%22AlarmRule%22, domain + %22%5B%22 + rule.getRuleId() + %22%5D%22, Event.SUCCESS,%0A%09%09%09%09%09      entity.toString
+entity.setDomain(rule.getDomain
 ());%0A
-%0A
 %09%09%09%09%09
-ServiceDataEvent event = 
+m_dispatcher.dispatch(
 new 
@@ -2340,42 +2340,8 @@
 ity)
-;%0A%09%09%09%09%09m_dispatcher.dispatch(event
 );%0A%09
@@ -2895,192 +2895,60 @@
 %09%09%09%09
-Cat.getProducer().logEvent(%22AlarmUrl%22, connectUrl, Event.SUCCESS, entity.toString());%0A%0A%09%09%09%09%09ExceptionDataEvent event = new ExceptionDataEvent(entity);%0A%0A%09%09%09%09%09m_dispatcher.dispatch(event
+m_dispatcher.dispatch(new ExceptionDataEvent(entity)
 );%0A%09
@@ -3126,10 +3126,90 @@
 ) %7B%0A%09%7D%0A%0A
+%09@Override%0A%09public void enableLogging(Logger logger) %7B%0A%09%09m_logger = logger;%0A%09%7D%0A%0A
 %7D%0A

add alarmTask log