package com.dianping.cat.system.alarm;

import java.util.List;

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;
import org.unidal.helper.Threads.Task;
import org.unidal.lookup.annotation.Inject;

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;

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

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

			long duration = System.currentTimeMillis() - time;

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

			try {
				Thread.sleep(3 * 1000 - duration);
			} 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() {
	}

}

@@ -1264,26 +1264,16 @@
  time;%0A%0A
-%09%09%09try %7B%0A%09
 %09%09%09if (d
@@ -1282,18 +1282,18 @@
 ation %3C 
-30
+29
  * 1000)
@@ -1291,24 +1291,34 @@
 9 * 1000) %7B%0A
+%09%09%09%09try %7B%0A
 %09%09%09%09%09Thread.
@@ -1327,10 +1327,10 @@
 eep(
-30
+29
  * 1
@@ -1343,37 +1343,32 @@
 duration);%0A%09%09%09%09%7D
-%0A%09%09%09%7D
  catch (Interrup
@@ -1381,32 +1381,33 @@
 eption e) %7B%0A%09%09%09%09
+%09
 active = false;%0A
@@ -1406,22 +1406,27 @@
  false;%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A
-%0A
 %09%09%09try %7B
@@ -1447,27 +1447,12 @@
 eep(
-3 * 1000 - duration
+1000
 );%0A%09
@@ -1517,12 +1517,8 @@
 %09%09%7D%0A
-%09%09%09%0A
 %09%09%7D%0A

modify the alarm thread time