package com.dianping.cat.statistic;

import com.dianping.cat.statistic.ServerStatistic.Statistic;

public class ServerStatisticManager {

	public ServerStatistic m_serverState = new ServerStatistic();

	private Statistic m_currentStatistic = null;

	private long m_currentMunite = -1;

	public void addBlockTotal(long total) {
		getCurrentStatistic().addBlockTotal(total);
	}

	public void addBlockLoss(long total) {
		getCurrentStatistic().addBlockLoss(total);
	}

	public void addBlockTime(long total) {
		getCurrentStatistic().addBlockTime(total);
	}

	public void addPigeonTimeError(long total) {
		getCurrentStatistic().addPigeonTimeError(total);
	}

	public void addMessageDump(long total) {
		getCurrentStatistic().addMessageDump(total);
	}

	public void addNetworkTimeError(long total) {
		getCurrentStatistic().addNetworkTimeError(total);
	}

	public void addMessageDumpLoss(long total) {
		getCurrentStatistic().addMessageDumpLoss(total);
	}

	public void addMessageSize(String domain, double size) {
		getCurrentStatistic().addMessageSize(domain, size);
	}

	public void addMessageSize(double size) {
		getCurrentStatistic().addMessageSize(size);
	}

	public void addMessageTotal(String domain, long total) {
		getCurrentStatistic().addMessageTotal(domain, total);
	}

	public void addMessageTotal(long total) {
		getCurrentStatistic().addMessageTotal(total);
	}

	public void addMessageTotalLoss(String domain, long total) {
		getCurrentStatistic().addMessageTotalLoss(domain, total);
	}

	public void addMessageTotalLoss(long total) {
		getCurrentStatistic().addMessageTotalLoss(total);
	}

	public void addProcessDelay(double delay) {
		getCurrentStatistic().addProcessDelay(delay);
	}

	public Statistic findState(long time) {
		return m_serverState.findOrCreate(time);
	}

	private Statistic getCurrentStatistic() {
		long time = System.currentTimeMillis();
		
		time = time - time % (60 * 1000);
		
		if (time != m_currentMunite) {
			m_currentMunite = time;
			m_currentStatistic = m_serverState.findOrCreate(time);
		}
		
		return m_currentStatistic;
	}

	public void removeState(long time) {
		m_serverState.remove(time);
	}
}

@@ -1909,16 +1909,39 @@
 00);%0A%09%09%0A
+%09%09synchronized(this)%7B%0A%09
 %09%09if (ti
@@ -1965,16 +1965,50 @@
 nite) %7B%0A
+%09%09%09%09System.out.println(%22hello%22);%0A%09
 %09%09%09m_cur
@@ -2026,16 +2026,17 @@
 = time;%0A
+%09
 %09%09%09m_cur
@@ -2085,16 +2085,21 @@
 (time);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09%0A%09

fix bug