1 package org.jrobin.demo; 2 3 27 28 import org.jrobin.core.*; 29 import org.jrobin.graph.RrdGraph; 30 import org.jrobin.graph.RrdGraphDef; 31 32 import java.awt.*; 33 import java.io.BufferedOutputStream ; 34 import java.io.FileOutputStream ; 35 import java.io.IOException ; 36 import java.io.PrintWriter ; 37 import java.util.Random ; 38 39 class Demo { 40 42 static final long SEED = 1909752002L; 43 static final Random RANDOM = new Random (SEED); 44 static final String FILE = "demo"; 45 46 static final long START = Util.getTimestamp(2003, 4, 1); 47 static final long END = Util.getTimestamp(2003, 5, 1); 48 static final int MAX_STEP = 300; 49 50 public static void main(String [] args) throws RrdException, IOException { 51 println("== Starting demo"); 54 RrdDb.setLockMode(RrdDb.NO_LOCKS); 55 long startMillis = System.currentTimeMillis(); 56 long start = START; 57 long end = END; 58 String rrdPath = Util.getJRobinDemoPath(FILE + ".rrd"); 59 String xmlPath = Util.getJRobinDemoPath(FILE + ".xml"); 60 String rrdRestoredPath = Util.getJRobinDemoPath(FILE + "_restored.rrd"); 61 String pngPath = Util.getJRobinDemoPath(FILE + ".png"); 62 String jpegPath = Util.getJRobinDemoPath(FILE + ".jpeg"); 63 String gifPath = Util.getJRobinDemoPath(FILE + ".gif"); 64 String logPath = Util.getJRobinDemoPath(FILE + ".log"); 65 PrintWriter log = new PrintWriter ( 66 new BufferedOutputStream (new FileOutputStream (logPath, false)) 67 ); 68 69 println("== Creating RRD file " + rrdPath); 71 RrdDef rrdDef = new RrdDef(rrdPath, start - 1, 300); 72 rrdDef.addDatasource("sun", "GAUGE", 600, 0, Double.NaN); 73 rrdDef.addDatasource("shade", "GAUGE", 600, 0, Double.NaN); 74 rrdDef.addArchive("AVERAGE", 0.5, 1, 600); 75 rrdDef.addArchive("AVERAGE", 0.5, 6, 700); 76 rrdDef.addArchive("AVERAGE", 0.5, 24, 775); 77 rrdDef.addArchive("AVERAGE", 0.5, 288, 797); 78 rrdDef.addArchive("MAX", 0.5, 1, 600); 79 rrdDef.addArchive("MAX", 0.5, 6, 700); 80 rrdDef.addArchive("MAX", 0.5, 24, 775); 81 rrdDef.addArchive("MAX", 0.5, 288, 797); 82 println(rrdDef.dump()); 83 log.println(rrdDef.dump()); 84 println("Estimated file size: " + rrdDef.getEstimatedSize()); 85 RrdDb rrdDb = new RrdDb(rrdDef); 86 println("== RRD file created."); 87 if(rrdDb.getRrdDef().equals(rrdDef)) { 88 println("Checking RRD file structure... OK"); 89 } 90 else { 91 println("Invalid RRD file created. This is a serious bug, bailing out"); 92 return; 93 } 94 rrdDb.close(); 95 println("== RRD file closed."); 96 97 GaugeSource sunSource = new GaugeSource(1200, 20); 99 GaugeSource shadeSource = new GaugeSource(300, 10); 100 println("== Simulating one month of RRD file updates with step not larger than " + 101 MAX_STEP + " seconds (* denotes 1000 updates)"); 102 long t = start; int n = 0; 103 rrdDb = new RrdDb(rrdPath); 104 Sample sample = rrdDb.createSample(); 105 while(t <= end + 86400L) { 106 sample.setTime(t); 109 sample.setValue("sun", sunSource.getValue()); 110 sample.setValue("shade", shadeSource.getValue()); 111 log.println(sample.dump()); 112 sample.update(); 113 t += RANDOM.nextDouble() * MAX_STEP + 1; 114 if(((++n) % 1000) == 0) { 115 System.out.print("*"); 116 }; 117 } 119 println(""); 120 println("== Finished. RRD file updated " + n + " times"); 121 println("== Last update time was: " + rrdDb.getLastUpdateTime()); 122 rrdDb.close(); 123 124 rrdDb = new RrdDb(rrdPath, true); 126 println("File reopen in read-only mode"); 127 128 println("== Fetching data for the whole month"); 130 FetchRequest request = rrdDb.createFetchRequest("AVERAGE", start, end); 131 println(request.dump()); 132 log.println(request.dump()); 133 FetchData fetchData = request.fetchData(); 134 println("== Data fetched. " + fetchData.getRowCount() + " points obtained"); 135 for(int i = 0; i < fetchData.getRowCount(); i++) { 136 println(fetchData.getRow(i).dump()); 137 } 138 println("== Dumping fetch data to XML format"); 139 println(fetchData.exportXml()); 140 println("== Fetch completed"); 141 142 println("== Dumping RRD file to XML file " + xmlPath + " (can be restored with RRDTool)"); 144 rrdDb.exportXml(xmlPath); 145 println("== Creating RRD file " + rrdRestoredPath + " from XML file " + xmlPath); 146 RrdDb rrdRestoredDb = new RrdDb(rrdRestoredPath, xmlPath); 147 148 println("== Closing both RRD files"); 150 rrdDb.close(); 151 println("== First file closed"); 152 rrdRestoredDb.close(); 153 println("== Second file closed"); 154 155 println("== Creating graph from the second file"); 157 RrdGraphDef gDef = new RrdGraphDef(); 158 gDef.setTimePeriod(start, end); 159 gDef.setTitle("Temperatures in May 2003"); 160 gDef.setVerticalLabel("temperature"); 161 gDef.datasource("sun", rrdRestoredPath, "sun", "AVERAGE"); 162 gDef.datasource("shade", rrdRestoredPath, "shade", "AVERAGE"); 163 gDef.datasource("median", "sun,shade,+,2,/"); 164 gDef.datasource("diff", "sun,shade,-,ABS,-1,*"); 165 gDef.datasource("sine", "TIME," + start + ",-," + (end - start) + 166 ",/,2,PI,*,*,SIN,1000,*"); 167 gDef.line("sun", Color.GREEN, "sun temp"); 168 gDef.line("shade", Color.BLUE, "shade temp"); 169 gDef.line("median", Color.MAGENTA, "median value@L"); 170 gDef.area("diff", Color.YELLOW, "difference@r"); 171 gDef.line("diff", Color.RED, null); 172 gDef.line("sine", Color.CYAN, "sine function demo@L"); 173 gDef.gprint("sun", "MAX", "maxSun = @3@s"); 174 gDef.gprint("sun", "AVERAGE", "avgSun = @3@S@r"); 175 gDef.gprint("shade", "MAX", "maxShade = @3@S"); 176 gDef.gprint("shade", "AVERAGE", "avgShade = @3@S@r"); 177 RrdGraph graph = new RrdGraph(gDef); 179 println("== Graph created"); 180 graph.saveAsPNG(pngPath, 400, 250); 181 println("== Graph saved as a PNG file " + pngPath); 182 graph.saveAsJPEG(jpegPath, 400, 250, 0.5F); 183 println("== Graph saved as a JPEG file " + jpegPath); 184 graph.saveAsGIF(gifPath, 400, 250); 185 println("== Graph saved as a GIF file " + gifPath); 186 187 log.close(); 189 println("== Demo completed in " + 190 ((System.currentTimeMillis() - startMillis) / 1000.0) + " sec"); 191 } 192 193 static void println(String msg) { 194 System.out.println(msg); 196 } 197 198 static void print(String msg) { 199 System.out.print(msg); 200 } 201 202 static class GaugeSource { 203 private double value; 204 private double step; 205 206 GaugeSource(double value, double step) { 207 this.value = value; 208 this.step = step; 209 } 210 211 long getValue() { 212 double oldValue = value; 213 double increment = RANDOM.nextDouble() * step; 214 if(RANDOM.nextDouble() > 0.5) { 215 increment *= -1; 216 } 217 value += increment; 218 if(value <= 0) { 219 value = 0; 220 } 221 return Math.round(oldValue); 222 } 223 } 224 } 225 226 227 228 229 | Popular Tags |