public void send(Graph graph, OutputStream out) throws IOException { RendererRun runRender = null; try { runRender = rendered.get(graph.hashCode()); } catch (Exception e) { logger.error("Error with probe: " + e); } if(runRender != null && runRender.isReady()) { runRender.send(out); } else { logger.info("No valid precalculated render found for " + graph); // No precalculation found, so we do it right now graph.writePng(out); } }
if(graph.getEnd().after(new Date())) cache = false; boolean allowed = graph.getACL().check(p); logger.trace(jrds.Util.delayedFormatString("Looking if ACL %s allow access to %s", graph.getACL(), this)); if(!allowed) { res.sendError(HttpServletResponse.SC_FORBIDDEN, "Invalid role access"); int graphStep = graph.getNode().getProbe().getStep(); if(p.period.getScale() != 0 || !cache) { res.addDateHeader("Expires", new Date().getTime() + graphStep * 1000); res.addDateHeader("Last-Modified", graph.getEnd().getTime()); res.addHeader("Content-disposition", "inline; filename=" + graph.getPngName()); String eTagBaseString = getServletName() + graph.hashCode(); res.addHeader("ETag", Base64.encodeBytes(eTagBaseString.getBytes())); ServletOutputStream out = res.getOutputStream(); } else { logger.debug(jrds.Util.delayedFormatString("graph %s not found in cache", graph)); graph.writePng(out); int wh = graph.getDimension().height; int rh = graph.getRrdGraph().getRrdGraphInfo().getHeight(); logger.trace("Delta height:" + (rh - wh) + " for " + node.getGraphDesc()); Date finish = new Date();
@Test public void create() throws Exception { @SuppressWarnings("unchecked") Probe<String, Number> p = GenerateProbe.quickProbe(testFolder); p.setPd(Full.getPd()); File rrdFile = new File(p.getMainStore().getPath()); Assert.assertTrue("Failed to create probe " + rrdFile.getAbsolutePath(), p.checkStore()); logger.debug("Created " + p + " stored in " + p.getMainStore().getStoreObject()); long endSec = Full.fill(p); logger.debug("fill time: " + endSec); logger.debug(p.getLastUpdate()); Period pr = Full.getPeriod(p, endSec); Graphics2D g2d = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics(); GraphDesc gd = Full.getGd(); gd.initializeLimits(g2d); GraphNode gn = new GraphNode(p, gd); Graph g = new Graph(gn); g.setPeriod(pr); int h = g.getDimension().height; int w = g.getDimension().width; File outputFile = testFolder.newFile(); OutputStream out = new FileOutputStream(outputFile); g.writePng(out); BufferedImage img = ImageIO.read(outputFile); Assert.assertEquals(h, img.getHeight()); Assert.assertEquals(w, img.getWidth()); Assert.assertTrue(rrdFile.exists()); Assert.assertTrue(rrdFile.length() > 0); }
@Test public void compare() throws IOException, InvocationTargetException { GraphNode gn = new GraphNode(p, GetMoke.getGraphDesc()); Period pr = new Period(); Graph g1 = new Graph(gn); g1.setPeriod(pr); Graph g2 = new Graph(gn); g2.setPeriod(pr); Assert.assertEquals(g1.hashCode(), g2.hashCode()); Assert.assertEquals(g1, g2); }
@Test public void test1() throws IOException, InvocationTargetException { RRDToolProbe p = new RRDToolProbe(); p.setHost(new HostStarter(new HostInfo("toto"))); ProbeDesc<String> pd = new ProbeDesc<String>(); pd.setName("Rrdtool"); pd.setProbeName("rrdtool"); p.setPd(pd); Map<String, String> empty = Collections.emptyMap(); p.setMainStore(new RrdDbStoreFactory(), empty); Assert.assertTrue("rrd native file can't be read", rrdfile.canRead()); Assert.assertTrue("Configuration of the probe failed", p.configure(rrdfile)); Assert.assertTrue("Check of the probe failed", p.checkStore()); GraphDesc gd = new GraphDesc(); gd.setGraphName("rrdtool"); gd.setName("rrdtool"); gd.add("speed", "speed", null, GraphDesc.LINE, Color.BLUE, "speed", GraphDesc.DEFAULTCF, false, null, null, null); gd.add("weight", "weight", null, GraphDesc.LINE, Color.GREEN, "weight", GraphDesc.DEFAULTCF, false, null, null, null); p.addGraph(gd); for(GraphNode gn: p.getGraphList()) { Graph g = gn.getGraph(); g.setEnd(new Date(end * 1000)); g.setStart(new Date(start * 1000)); File outputFile = new File(testFolder.getRoot(), "rrdtool.png"); OutputStream out = new FileOutputStream(outputFile); g.writePng(out); Assert.assertTrue("graph not created", outputFile.canRead()); } }
protected void fillGraphDef(RrdGraphDef graphDef) { GraphDesc gd = getGraphDesc(); try { long startsec = getStartSec(); long endsec = getEndSec(); ExtractInfo ei = ExtractInfo.get().make(start, end); graphDef.setStartTime(startsec); graphDef.setEndTime(endsec); PlottableMap customData = node.getCustomData(); if(customData != null) { long step = Math.max((endsec - startsec) / gd.getWidth(), 1); customData.configure(startsec, endsec, step); } setGraphDefData(graphDef, node.getProbe(), ei, customData); if(gd.withLegend()) addlegend(graphDef); } catch (RuntimeException e) { Util.log(this, logger, Level.ERROR, e, "Impossible to create graph definition: %s", e.getMessage()); } }
OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile)); long middletime = System.currentTimeMillis(); graph.writePng(out); if(logger.isTraceEnabled()) { long endtime = System.currentTimeMillis(); long duration1 = (middletime - starttime); long duration2 = (endtime - middletime); logger.trace("Graph " + graph.getQualifiedName() + " renderding ran for (ms) " + duration1 + ":" + duration2); String message; try { String graphName = graph.getQualifiedName(); message = String.format("Error rendering graph %s: %s", graphName, e.getMessage()); } catch (Exception e1) { String graphName = graph.getNode().getProbe().getName() + "/" + graph.getNode().getGraphDesc().getGraphName(); message = String.format("Error rendering incomplete graph %s: %s", graphName, e.getMessage());
public RendererRun(Graph graph) throws IOException { this.graph = graph; destFile = new File(tmpDir, Integer.toHexString(graph.hashCode()) + ".png"); }
private void doGraph(GraphNode gn, Renderer r, ParamsBean params, JrdsJSONWriter w) throws IOException { jrds.Graph graph = gn.getGraph(); if (graph == null) { return; } params.configureGraph(graph); Map<String, Object> imgProps = new HashMap<String, Object>(); r.render(graph); Probe<?, ?> p = gn.getProbe(); imgProps.put("probename", p.getName()); imgProps.put("qualifiedname", graph.getQualifiedName()); Dimension d = graph.getDimension(); if(d != null) { imgProps.put("height", d.height); imgProps.put("width", d.width); } imgProps.put("graph", params.doArgsMap(graph, true)); imgProps.put("history", params.doArgsMap(graph, false)); imgProps.put("probe", params.doArgsMap(p, true)); imgProps.put("graphnode", params.doArgsMap(gn, true)); doTree(w, graph.getQualifiedName(), gn.hashCode(), "graph", null, imgProps); }
logger.debug("New graph: " + gn.getGraphTitle()); Graph g = gn.getGraph(); g.setPeriod(p); logger.debug("Found graph for probe " + gn.getProbe()); try {
public void render(Graph graph) throws IOException { if(!rendered.containsKey(graph.hashCode())) { synchronized (rendered) { if(!rendered.containsKey(graph.hashCode())) { RendererRun runRender = new RendererRun(graph); // Create graphics object rendered.put(graph.hashCode(), runRender); try { tpool.execute(runRender); } catch (RejectedExecutionException ex) { logger.warn("Render thread dropped for graph " + graph); } logger.debug("wants to render " + runRender); } } } }
public boolean isReady(Graph graph) { RendererRun runRender = rendered.get(graph.hashCode()); if(runRender == null) { try { render(graph); runRender = rendered.get(graph.hashCode()); } // If cannot launch render, will always be false catch (IOException e) { logger.error("graph " + graph + " will not be calculated:" + e); runRender = null; } } return (runRender != null) && runRender.isReady(); }
public FileChannel sendInfo(Graph graph) { RendererRun runRender = null; try { runRender = rendered.get(graph.hashCode()); } catch (Exception e) { logger.error("Error with probe: " + e); } if(runRender != null && runRender.isReady()) { try { return FileChannel.open(runRender.destFile.toPath(), StandardOpenOption.READ); } catch (IOException e) { logger.error("Can't read graph cache file: " + e.getMessage()); return null; } } else { return null; } }