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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.anysoft.util.Configurable;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Pair;
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]
* - 采用新的指标接口
*
* @version 1.6.12.29 [20190409]
* - 优化指标输出模型,对接Prometheus;
*
* @version 1.6.12.32 [20190429]
* - 优化RRM模型
*/
public class RRModel implements XMLConfigurable,Configurable,Reportable{
protected static final Logger LOG = LoggerFactory.getLogger(RRModel.class);
/**
* id
*/
private String id;
/**
* 模型下的多个RRA
*/
private Map> rras = new HashMap>();
/**
* 属性
*/
private List> attrs = null;
/**
* 获取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){
if (attrs == null){
synchronized(this){
attrs = new ArrayList>();
fragment.listAttrs(attrs);
}
}
Iterator> iterator = rras.values().iterator();
while (iterator.hasNext()){
RRArchive rra = iterator.next();
rra.update(timestamp, fragment);
}
}
@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 (!attrs.isEmpty()){
for (Pair pair:attrs){
Element attrElem = doc.createElement("attr");
attrElem.setAttribute("id", pair.key());
attrElem.setAttribute("value",pair.value());
xml.appendChild(attrElem);
}
}
if (StringUtils.isNotEmpty(cycle)){
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);
if (!attrs.isEmpty()){
Map attr = new HashMap();
for (Pair pair:attrs){
attr.put(pair.key(), pair.value());
}
json.put("attr",attr);
}
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