com.anysoft.rrm.RRModel Maven / Gradle / Ivy
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.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{
protected static final Logger LOG = LoggerFactory.getLogger(RRModel.class);
/**
* 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