Skip to content

Commit

Permalink
Adding a builder to graphdesc
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacchella committed Mar 18, 2019
1 parent b67a3b7 commit 22b3e6b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 44 deletions.
74 changes: 39 additions & 35 deletions jrds-core/src/main/java/jrds/GraphDesc.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import java.awt.font.LineMetrics;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -43,7 +45,7 @@
*
* @author Fabrice Bacchella
*/
public class GraphDesc implements Cloneable, WithACL {
public class GraphDesc implements WithACL {
static final private Logger logger = Logger.getLogger(GraphDesc.class);

static public final ConsolFun DEFAULTCF = ConsolFun.AVERAGE;
Expand Down Expand Up @@ -702,7 +704,7 @@ public String toString() {
name,
(dspath == null ? "" : dspath.host),
(dspath == null ? "" : dspath.probe),
dsName,
(dsName == null ? "" : dsName),
(rpn == null ? "" : rpn),
graphType,
colorString,
Expand Down Expand Up @@ -785,6 +787,27 @@ public Builder emptyDs() {
return this;
}

public Builder emptyTrees() {
trees = new HashMap<String, List<?>>();
return this;
}

public Builder addTree(String name, List<?> hierarchy) {
if(trees == null) {
emptyTrees();
}
trees.put(name, hierarchy);
return this;
}

public Builder addTree(String name, String... hierarchy) {
if(trees == null) {
emptyTrees();
}
trees.put(name, Arrays.asList(hierarchy));
return this;
}

public Builder addDsSecBuilder(DsDescBuilder builder) {
if(descbuilders == null) {
descbuilders = new ArrayList<>();
Expand Down Expand Up @@ -925,6 +948,10 @@ public void add(DsDescBuilder builder) {
String bprobe = builder.dspath != null ? builder.dspath.probe : null;
String bname = builder.name;
String bdsname = builder.dsName;
// dsName unknown, try to extract from name
if (bdsname == null && bgt.datasource() && builder.rpn == null && bname != null) {
bdsname = bname;
}
// Unknown name, where to get it from ?
if (bname == null) {
// If the name is missing, generate one ?
Expand All @@ -933,26 +960,21 @@ public void add(DsDescBuilder builder) {
} else if (bdsname != null){
bname = bdsname;
} else {
bname = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
bname = Integer.toHexString(ThreadLocalRandom.current().nextInt());
}
}
// dsName unknown, try to extract from name
if (bdsname == null && bgt.datasource() && builder.rpn == null && bname != null) {
bdsname = bname;
}
String dlegend = null;
String blegend = builder.legend;
// Auto generated legend
if(builder.legend == null && bgt.legend()) {
dlegend = bname;
if(blegend == null && bgt.legend()) {
blegend = bname;
}

if(builder.reversed) {
String revRpn = "0, " + name + ", -";
String revRpn = "0, " + bname + ", -";
allds.add(new DsDesc(bname, bdsname, builder.rpn, GraphType.NONE, null, null, builder.cf, bhost, bprobe));
allds.add(new DsDesc("rev_" + name, revRpn, bgt, bcolor, null));
allds.add(new DsDesc(bname, GraphType.LEGEND, dlegend, builder.cf));
allds.add(new DsDesc("rev_" + bname, revRpn, bgt, bcolor, null));
allds.add(new DsDesc(bname, GraphType.LEGEND, blegend, builder.cf));
} else {
allds.add(new DsDesc(bname, bdsname, builder.rpn, bgt, builder.color, dlegend, builder.cf, bhost, bprobe));
allds.add(new DsDesc(bname, bdsname, builder.rpn, bgt, bcolor, blegend, builder.cf, bhost, bprobe));
}
if(builder.percentile != null) {
String percentileName = "percentile" + builder.percentile + "_" + name;
Expand All @@ -969,8 +991,8 @@ public void add(DsDescBuilder builder) {
allds.add(new DsDesc(percentileName, GraphType.PERCENTILELEGEND, percentileLegend, builder.cf));
maxLengthLegend = Math.max(maxLengthLegend, percentileLegend.length());
}
if(dlegend != null) {
maxLengthLegend = Math.max(maxLengthLegend, dlegend.length());
if(blegend != null) {
maxLengthLegend = Math.max(maxLengthLegend, blegend.length());
}
}

Expand Down Expand Up @@ -1371,24 +1393,6 @@ public ACL getACL() {
return acl;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
@Override
public Object clone() throws CloneNotSupportedException {
GraphDesc newgd = (GraphDesc) super.clone();
newgd.allds.addAll(allds);
newgd.trees = new HashMap<String, List<?>>(trees.size());
for(Map.Entry<String, List<?>> e: trees.entrySet()) {
List<Object> tree = new ArrayList<Object>(e.getValue().size());
tree.addAll(e.getValue());
newgd.trees.put(e.getKey(), tree);
}
return newgd;
}

public Document dumpAsXml() throws ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Expand Down
2 changes: 1 addition & 1 deletion jrds-core/src/main/java/jrds/ProbeDesc.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public void setUptimefactor(float uptimefactor) {
/**
* @return Returns the list of graph names.
*/
public Collection<String> getGraphClasses() {
public Collection<String> getGraphs() {
return graphesList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ else if(unitElem.getElementbyName("SI") != null)
builder.setRpn(value);
break;
case "reversed":
builder.setReversed(value != null && ! value.isEmpty());
builder.setReversed(! "false".equalsIgnoreCase(value));
break;
case "dsName":
builder.setDsName(value);
Expand Down
4 changes: 2 additions & 2 deletions jrds-core/src/main/java/jrds/factories/ProbeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public boolean configure(Probe<?, ?> p, List<?> constArgs) {
if(name == null)
name = jrds.Util.parseTemplate(p.getPd().getProbeName(), p);
p.setName(name);
for(String graphName: p.getPd().getGraphClasses()) {
for(String graphName: p.getPd().getGraphs()) {
GraphDesc gd = graphDescMap.get(graphName);
if(gd != null) {
p.addGraph(new GraphNode(p, gd));
p.addGraph(gd);
} else {
logger.warn(Util.delayedFormatString("Unknown graph %s for probe %s", graphName, p));
}
Expand Down
2 changes: 1 addition & 1 deletion jrds-core/src/test/java/jrds/AllProbeCreationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void makeProbe() throws ParserConfigurationException, IOException, URISyn
}
}
p.configureStarters(pm);
for(String graphName: p.getPd().getGraphClasses()) {
for(String graphName: p.getPd().getGraphs()) {
try {
GraphDesc gd = graphDescMap.get(graphName);
p.addGraph(gd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public void testGraphDesc() throws Exception {
dsMap.set("dsName", "add3").set("dsType", DsType.COUNTER);
pd.add(dsMap);

dsMap.clear();
dsMap.set("dsName", "add4").set("dsType", DsType.COUNTER);
pd.add(dsMap);

dsMap.clear();
dsMap.set("dsName", "add5").set("dsType", DsType.COUNTER);
pd.add(dsMap);

p.checkStore();

if(logger.isTraceEnabled()) {
Expand Down Expand Up @@ -129,18 +137,16 @@ public void testGraphDesc() throws Exception {
RrdGraphDef def = gd.getGraphDef(p, ei, empty);
RrdGraphInfo gi = new RrdGraph(def, iw, iwp).getRrdGraphInfo();

logger.debug(Arrays.asList(gi.getPrintLines()));

Assert.assertEquals("graph name failed", "graphName", gd.getGraphName());
Assert.assertEquals("graph title failed", "graphTitle", gd.getGraphTitle());
Assert.assertEquals("graph name failed", "graphdesctest", gd.getName());
Assert.assertEquals("legend count failed", 5, gd.getLegendLines());
Assert.assertEquals("legend count failed", 6, gd.getLegendLines());
Assert.assertFalse("Graph unit should be binary", gd.isSiUnit());
Assert.assertEquals("Graph unit scale should be fixed", 0, gd.getUnitExponent().intValue());

Assert.assertTrue("graph height invalid", 206 < gi.getHeight());
Assert.assertTrue("graph width invalid", 578 < gi.getWidth());
Assert.assertEquals("graph byte count invalid", 626550, gi.getByteCount());
Assert.assertEquals("graph byte count invalid", 678758, gi.getByteCount());

for(String treename: new String[] { PropertiesManager.HOSTSTAB, PropertiesManager.VIEWSTAB, "tab" }) {
List<String> tree = gd.getTree(new GraphNode(p, gd), treename);
Expand Down

0 comments on commit 22b3e6b

Please sign in to comment.