All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anysoft.rrm.RRModel Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.anysoft.rrm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.alogic.metrics.Fragment;
import com.alogic.metrics.stream.MetricsCollector;
import com.alogic.metrics.stream.MetricsHandlerFactory;
import com.anysoft.stream.Handler;
import com.anysoft.util.BaseException;
import com.anysoft.util.Configurable;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.Reportable;
import com.anysoft.util.XMLConfigurable;
import com.anysoft.util.XmlElementProperties;

/**
 * Round Robin Model
 * @author duanyy
 * @version 1.6.4.42 [duanyy 20160407] 
* - 对接指标处理器
* * @version 1.6.6.13 [20170109 duanyy]
* - 采用新的指标接口 */ public class RRModel implements XMLConfigurable,Configurable,Reportable,MetricsCollector{ /** * id */ private String id; /** * 模型下的多个RRA */ private Map> rras = new HashMap>(); /** * 指标处理器 */ private Handler metricsHandler = null; /** * 是否向指标处理器输出指标 */ private boolean metricsOutput = true; /** * 获取id * @return id */ public String getId(){ return id; } public RRModel(String _id){ id = _id; } /** * 获取指定id的rra * @param id id * @return rra */ public RRArchive getRRA(String id){ return rras.get(id); } /** * 更新数据 * @param timestamp * @param fragment */ public void update(long timestamp,data fragment){ Iterator> iterator = rras.values().iterator(); while (iterator.hasNext()){ RRArchive rra = iterator.next(); rra.update(timestamp, fragment); } if (metricsOutput){ fragment.report(this); } } @Override public void report(Element xml) { if (xml != null){ xml.setAttribute("id", id); String cycle = xml.getAttribute("cycle"); String current = xml.getAttribute("current"); Document doc = xml.getOwnerDocument(); if (cycle != null && cycle.length() > 0){ RRArchive found = rras.get(cycle); if (found != null){ Element _rra = doc.createElement("rra"); if (current == null || !current.equals("true")){ _rra.setAttribute("hist", "true"); } found.report(_rra); xml.appendChild(_rra); } }else{ Iterator> iterator = rras.values().iterator(); while (iterator.hasNext()){ RRArchive rra = iterator.next(); Element _rra = doc.createElement("rra"); rra.report(_rra); xml.appendChild(_rra); } } } } @Override public void report(Map json) { if (json != null){ json.put("id", id); String cycle = JsonTools.getString(json, "cycle", ""); boolean current = JsonTools.getBoolean(json, "current", false); if (cycle != null && cycle.length() > 0){ RRArchive found = rras.get(cycle); if (found != null){ Map _rra = new HashMap(); if (!current){ _rra.put("hist", true); } found.report(_rra); json.put("rra", _rra); } }else{ List _rras = new ArrayList(); Iterator> iterator = rras.values().iterator(); while (iterator.hasNext()){ RRArchive rra = iterator.next(); Map _rra = new HashMap(); rra.report(_rra); _rras.add(_rra); } json.put("rra", _rras); } } } @Override public void configure(Element _e, Properties _properties) throws BaseException { XmlElementProperties p = new XmlElementProperties(_e,_properties); configure(p); } @Override public void configure(Properties p) throws BaseException { String[] _rras = PropertiesConstants.getString(p,"rrm.rras","minute,halfhour,hour").split(","); for (int i = 0 ;i < _rras.length ; i ++){ String id = _rras[i]; if (id != null && id.length() > 0){ RRArchive newRRA = new RRArchive(id); newRRA.configure(p); rras.put(id, newRRA); } } metricsOutput = PropertiesConstants.getBoolean(p,getId() + ".output",metricsOutput); metricsHandler = MetricsHandlerFactory.getClientInstance(); } @Override public void metricsIncr(Fragment fragment) { if (metricsHandler != null){ metricsHandler.handle(fragment,System.currentTimeMillis()); } } }