

package scouter.lang.pack;

import java.io.IOException;

import scouter.io.DataInputX;
import scouter.io.DataOutputX;
import scouter.lang.TimeTypeEnum;
import scouter.lang.value.MapValue;
import scouter.lang.value.NumberValue;
import scouter.lang.value.Value;
import scouter.util.DateUtil;

/**
 * Object that contains multiple counter information
 */
public class PerfCounterPack implements Pack {

	/**
	 * Counter time
	 */
	public long time;
	/**
	 * Object name
	 */
	public String objName;
	/**
	 * Time type. 1:Real-time, 2:OneMin, 3:FiveMin, 4:TenMin, 5:Hour, 6:Day
	 */
	public byte timetype;
	/**
	 * Multiple counter data. Key is counter name. ref.)scouter.lang.value.MapValue
	 */
	public MapValue data = new MapValue();

	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("PerfCounter ").append(DateUtil.timestamp(time));
		buf.append(" ").append(objName);
		buf.append(" ").append(TimeTypeEnum.getString(timetype));
		buf.append(" ").append(data);
		return buf.toString();
	}

	public byte getPackType() {
		return PackEnum.PERF_COUNTER;
	}

	public void write(DataOutputX dout) throws IOException {
		dout.writeLong(time);
		dout.writeText(objName);
		dout.writeByte(timetype);
		dout.writeValue(data);
	}

	public Pack read(DataInputX din) throws IOException {
		this.time = din.readLong();
		this.objName = din.readText();
		this.timetype = din.readByte();
		this.data = (MapValue) din.readValue();
		return this;
	}

	public void put(String key, Value value) {
		this.data.put(key, value);
	}

	public void add(String key, NumberValue value) {
		Value old = this.data.get(key);
		if (old == null) {
			this.data.put(key, value);
		} else if (old instanceof NumberValue) {
			((NumberValue) old).add(value);
		}
	}
}