KickJava   Java API By Example, From Geeks To Geeks.

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


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
32 import java.awt.*;
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 Demo {
40     // static final String FACTORY_NAME = "NIO";
41

42     static final long SEED = 1909752002L;
43     static final Random JavaDoc RANDOM = new Random JavaDoc(SEED);
44     static final String JavaDoc 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 JavaDoc[] args) throws RrdException, IOException JavaDoc {
51         // RrdDb.setDefaultFactory(FACTORY_NAME);
52
// setup
53
println("== Starting demo");
54         RrdDb.setLockMode(RrdDb.NO_LOCKS);
55         long startMillis = System.currentTimeMillis();
56         long start = START;
57         long end = END;
58         String JavaDoc rrdPath = Util.getJRobinDemoPath(FILE + ".rrd");
59         String JavaDoc xmlPath = Util.getJRobinDemoPath(FILE + ".xml");
60         String JavaDoc rrdRestoredPath = Util.getJRobinDemoPath(FILE + "_restored.rrd");
61         String JavaDoc pngPath = Util.getJRobinDemoPath(FILE + ".png");
62         String JavaDoc jpegPath = Util.getJRobinDemoPath(FILE + ".jpeg");
63         String JavaDoc gifPath = Util.getJRobinDemoPath(FILE + ".gif");
64         String JavaDoc logPath = Util.getJRobinDemoPath(FILE + ".log");
65         PrintWriter JavaDoc log = new PrintWriter JavaDoc(
66             new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(logPath, false))
67         );
68
69         // creation
70
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         // update database
98
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             //rrdDb = new RrdDb(rrdPath);
107
//Sample sample = rrdDb.createSample();
108
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             //rrdDb.close();
118
}
119         println("");
120         println("== Finished. RRD file updated " + n + " times");
121         println("== Last update time was: " + rrdDb.getLastUpdateTime());
122         rrdDb.close();
123
124         // test read-only access!
125
rrdDb = new RrdDb(rrdPath, true);
126         println("File reopen in read-only mode");
127
128         // fetch data
129
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         // dump to XML file
143
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         // close files
149
println("== Closing both RRD files");
150         rrdDb.close();
151         println("== First file closed");
152         rrdRestoredDb.close();
153         println("== Second file closed");
154
155         // create graph
156
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         // create graph finally
178
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         // demo ends
188
log.close();
189         println("== Demo completed in " +
190             ((System.currentTimeMillis() - startMillis) / 1000.0) + " sec");
191     }
192
193     static void println(String JavaDoc msg) {
194         //System.out.println(msg + " " + Util.getLapTime());
195
System.out.println(msg);
196     }
197
198     static void print(String JavaDoc 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