KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > demo > TemplatesDemo


1 package org.jrobin.demo;
2
3 /* ============================================================
4  * JRobin : Pure java implementation of RRDTool's functionality
5  * ============================================================
6  *
7  * Project Info: http://www.jrobin.org
8  * Project Lead: Sasa Markovic (saxon@jrobin.org);
9  *
10  * (C) Copyright 2003, by Sasa Markovic.
11  *
12  * This library is free software; you can redistribute it and/or modify it under the terms
13  * of the GNU Lesser General Public License as published by the Free Software Foundation;
14  * either version 2.1 of the License, or (at your option) any later version.
15  *
16  * Developers: Sasa Markovic (saxon@jrobin.org)
17  * Arne Vandamme (cobralord@jrobin.org)
18  *
19  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  * See the GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License along with this
24  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */

27
28 import org.jrobin.core.*;
29 import org.jrobin.graph.RrdGraph;
30 import org.jrobin.graph.RrdGraphDef;
31 import org.jrobin.graph.RrdGraphDefTemplate;
32
33 import java.io.BufferedOutputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37 import java.util.Random JavaDoc;
38
39 class TemplatesDemo {
40     static final String JavaDoc RRD_TEMPLATE =
41         "<rrd_def> " +
42         " <path>${path}</path> " +
43         " <start>${start}</start> " +
44         " <step>300</step> " +
45         " <datasource> " +
46         " <name>sun</name> " +
47         " <type>GAUGE</type> " +
48         " <heartbeat>600</heartbeat> " +
49         " <min>0</min> " +
50         " <max>U</max> " +
51         " </datasource> " +
52         " <datasource> " +
53         " <name>shade</name> " +
54         " <type>GAUGE</type> " +
55         " <heartbeat>600</heartbeat> " +
56         " <min>0</min> " +
57         " <max>U</max> " +
58         " </datasource> " +
59         " <archive> " +
60         " <cf>AVERAGE</cf> " +
61         " <xff>0.5</xff> " +
62         " <steps>1</steps> " +
63         " <rows>600</rows> " +
64         " </archive> " +
65         " <archive> " +
66         " <cf>AVERAGE</cf> " +
67         " <xff>0.5</xff> " +
68         " <steps>6</steps> " +
69         " <rows>700</rows> " +
70         " </archive> " +
71         " <archive> " +
72         " <cf>AVERAGE</cf> " +
73         " <xff>0.5</xff> " +
74         " <steps>24</steps> " +
75         " <rows>775</rows> " +
76         " </archive> " +
77         " <archive> " +
78         " <cf>AVERAGE</cf> " +
79         " <xff>0.5</xff> " +
80         " <steps>288</steps> " +
81         " <rows>797</rows> " +
82         " </archive> " +
83         " <archive> " +
84         " <cf>MAX</cf> " +
85         " <xff>0.5</xff> " +
86         " <steps>1</steps> " +
87         " <rows>600</rows> " +
88         " </archive> " +
89         " <archive> " +
90         " <cf>MAX</cf> " +
91         " <xff>0.5</xff> " +
92         " <steps>6</steps> " +
93         " <rows>700</rows> " +
94         " </archive> " +
95         " <archive> " +
96         " <cf>MAX</cf> " +
97         " <xff>0.5</xff> " +
98         " <steps>24</steps> " +
99         " <rows>775</rows> " +
100         " </archive> " +
101         " <archive> " +
102         " <cf>MAX</cf> " +
103         " <xff>0.5</xff> " +
104         " <steps>288</steps> " +
105         " <rows>797</rows> " +
106         " </archive> " +
107         "</rrd_def> " ;
108
109     static final String JavaDoc GRAPH_TEMPLATE =
110         "<rrd_graph_def> " +
111         " <span> " +
112         " <start>${start}</start> " +
113         " <end>${end}</end> " +
114         " </span> " +
115         " <options> " +
116         " <title>${title}</title> " +
117         " <vertical_label>temperature</vertical_label> " +
118         " </options> " +
119         " <datasources> " +
120         " <def> " +
121         " <name>sun</name> " +
122         " <rrd>${rrd}</rrd> " +
123         " <source>sun</source> " +
124         " <cf>AVERAGE</cf> " +
125         " </def> " +
126         " <def> " +
127         " <name>shade</name> " +
128         " <rrd>${rrd}</rrd> " +
129         " <source>shade</source> " +
130         " <cf>AVERAGE</cf> " +
131         " </def> " +
132         " <def> " +
133         " <name>median</name> " +
134         " <rpn>sun,shade,+,2,/</rpn> " +
135         " </def> " +
136         " <def> " +
137         " <name>diff</name> " +
138         " <rpn>sun,shade,-,ABS,-1,*</rpn> " +
139         " </def> " +
140         " <def> " +
141         " <name>sine</name> " +
142         " <rpn>${sine}</rpn> " +
143         " </def> " +
144         " </datasources> " +
145         " <graph> " +
146         " <line> " +
147         " <datasource>sun</datasource> " +
148         " <color>#00FF00</color> " +
149         " <legend>sun temp</legend> " +
150         " </line> " +
151         " <line> " +
152         " <datasource>shade</datasource> " +
153         " <color>#0000FF</color> " +
154         " <legend>shade temp</legend> " +
155         " </line> " +
156         " <line> " +
157         " <datasource>median</datasource> " +
158         " <color>#FF00FF</color> " +
159         " <legend>median value@L</legend> " +
160         " </line> " +
161         " <area> " +
162         " <datasource>diff</datasource> " +
163         " <color>#FFFF00</color> " +
164         " <legend>difference@r</legend> " +
165         " </area> " +
166         " <line> " +
167         " <datasource>diff</datasource> " +
168         " <color>#FF0000</color> " +
169         " <legend/> " +
170         " </line> " +
171         " <line> " +
172         " <datasource>sine</datasource> " +
173         " <color>#00FFFF</color> " +
174         " <legend>sine function demo@L</legend> " +
175         " </line> " +
176         " <gprint> " +
177         " <datasource>sun</datasource> " +
178         " <cf>MAX</cf> " +
179         " <format>maxSun = @3@s</format> " +
180         " </gprint> " +
181         " <gprint> " +
182         " <datasource>sun</datasource> " +
183         " <cf>AVERAGE</cf> " +
184         " <format>avgSun = @3@S@r</format> " +
185         " </gprint> " +
186         " <gprint> " +
187         " <datasource>shade</datasource> " +
188         " <cf>MAX</cf> " +
189         " <format>maxShade = @3@S</format> " +
190         " </gprint> " +
191         " <gprint> " +
192         " <datasource>shade</datasource> " +
193         " <cf>AVERAGE</cf> " +
194         " <format>avgShade = @3@S@r</format> " +
195         " </gprint> " +
196         " </graph> " +
197         "</rrd_graph_def> " ;
198
199     static final long SEED = 1909752002L;
200
201     static final Random JavaDoc RANDOM = new Random JavaDoc(SEED);
202     static final String JavaDoc FILE = "templates_demo";
203
204     static final long START = Util.getTimestamp(2003, 4, 1);
205     static final long END = Util.getTimestamp(2003, 5, 1);
206
207     static final int MAX_STEP = 300;
208
209     public static void main(String JavaDoc[] args) throws RrdException, IOException JavaDoc {
210         // setup
211
println("== Starting demo");
212         RrdDb.setLockMode(RrdDb.NO_LOCKS);
213
214         long startMillis = System.currentTimeMillis();
215         long start = START;
216         long end = END;
217         String JavaDoc rrdPath = Util.getJRobinDemoPath(FILE + ".rrd");
218         String JavaDoc xmlPath = Util.getJRobinDemoPath(FILE + ".xml");
219         String JavaDoc rrdRestoredPath = Util.getJRobinDemoPath(FILE + "_restored.rrd");
220         String JavaDoc pngPath = Util.getJRobinDemoPath(FILE + ".png");
221         String JavaDoc jpegPath = Util.getJRobinDemoPath(FILE + ".jpeg");
222         String JavaDoc gifPath = Util.getJRobinDemoPath(FILE + ".gif");
223         String JavaDoc logPath = Util.getJRobinDemoPath(FILE + ".log");
224         PrintWriter JavaDoc log = new PrintWriter JavaDoc(
225             new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(logPath, false))
226         );
227
228         // creation from the template
229
println("== Creating RRD file " + rrdPath);
230         RrdDefTemplate defTemplate = new RrdDefTemplate(RRD_TEMPLATE);
231         defTemplate.setVariable("path", rrdPath);
232         defTemplate.setVariable("start", start - 1);
233         RrdDef rrdDef = defTemplate.getRrdDef();
234
235         println(rrdDef.dump());
236         log.println(rrdDef.dump());
237         RrdDb rrdDb = new RrdDb(rrdDef);
238         rrdDb.close();
239         println("== RRD file created and closed.");
240
241         // update database
242
GaugeSource sunSource = new GaugeSource(1200, 20);
243         GaugeSource shadeSource = new GaugeSource(300, 10);
244         println("== Simulating one month of RRD file updates with step not larger than " +
245             MAX_STEP + " seconds (* denotes 1000 updates)");
246         long t = start; int n = 0;
247         rrdDb = new RrdDb(rrdPath);
248         Sample sample = rrdDb.createSample();
249         while(t <= end + 86400L) {
250             sample.setTime(t);
251             sample.setValue("sun", sunSource.getValue());
252             sample.setValue("shade", shadeSource.getValue());
253             log.println(sample.dump());
254             sample.update();
255
256             t += RANDOM.nextDouble() * MAX_STEP + 1;
257             if(((++n) % 1000) == 0) {
258                 System.out.print("*");
259             };
260         }
261         System.out.println("");
262         println("== Finished. RRD file updated " + n + " times");
263         println("== Last update time was: " + rrdDb.getLastUpdateTime());
264
265         // fetch data
266
println("== Fetching data for the whole month");
267         FetchRequest request = rrdDb.createFetchRequest("AVERAGE", start, end);
268         println(request.dump());
269         log.println(request.dump());
270         FetchData fetchData = request.fetchData();
271         println("== Data fetched. " + fetchData.getRowCount() + " points obtained");
272         for(int i = 0; i < fetchData.getRowCount(); i++) {
273             println(fetchData.getRow(i).dump());
274         }
275         println("== Dumping fetch data to XML format");
276         println(fetchData.exportXml());
277         println("== Fetch completed");
278
279         // dump to XML file
280
println("== Dumping RRD file to XML file " + xmlPath + " (can be restored with RRDTool)");
281         rrdDb.exportXml(xmlPath);
282         println("== Creating RRD file " + rrdRestoredPath + " from XML file " + xmlPath);
283         RrdDb rrdRestoredDb = new RrdDb(rrdRestoredPath, xmlPath);
284
285         // close files
286
println("== Closing both RRD files");
287         rrdDb.close();
288         rrdRestoredDb.close();
289
290         // create graph
291
println("== Creating graph from the second file");
292         RrdGraphDefTemplate graphTemplate = new RrdGraphDefTemplate(GRAPH_TEMPLATE);
293         graphTemplate.setVariable("start", start);
294         graphTemplate.setVariable("end", end);
295         graphTemplate.setVariable("title", "Temperatures in May 2003");
296         graphTemplate.setVariable("rrd", rrdRestoredPath);
297         // RPN expressions can be created at runtime and put into a template
298
graphTemplate.setVariable("sine", "TIME," + start + ",-," + (end - start) +
299             ",/,2,PI,*,*,SIN,1000,*");
300         // create graph finally
301
RrdGraphDef gDef = graphTemplate.getRrdGraphDef();
302         RrdGraph graph = new RrdGraph(gDef);
303         println("== Graph created");
304         println("== Saving graph as PNG file " + pngPath);
305         graph.saveAsPNG(pngPath, 400, 250);
306         println("== Saving graph as JPEG file " + jpegPath);
307         graph.saveAsJPEG(jpegPath, 400, 250, 0.5F);
308         println("== Saving graph as GIF file " + gifPath);
309         graph.saveAsGIF(gifPath, 400, 250);
310
311         // demo ends
312
log.close();
313         println("== Demo completed in " +
314             ((System.currentTimeMillis() - startMillis) / 1000.0) + " sec");
315     }
316
317     static void println(String JavaDoc msg) {
318         System.out.println(msg);
319     }
320
321     static class GaugeSource {
322         private double value;
323         private double step;
324
325         GaugeSource(double value, double step) {
326             this.value = value;
327             this.step = step;
328         }
329
330         long getValue() {
331             double oldValue = value;
332             double increment = RANDOM.nextDouble() * step;
333             if (RANDOM.nextDouble() > 0.5) {
334                 increment *= -1;
335             }
336             value += increment;
337             if (value <= 0) {
338                 value = 0;
339             }
340             return Math.round(oldValue);
341         }
342     }
343 }
344
345
346
347
348
Popular Tags