KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > FetchRequest


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

25
26 package org.jrobin.core;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Class to represent fetch request. For the complete explanation of all
32  * fetch parameters consult RRDTool's
33  * <a HREF="../../../../man/rrdfetch.html" target="man">rrdfetch man page</a>.
34  *
35  * You cannot create <code>FetchRequest</code> directly (no public constructor
36  * is provided). Use {@link org.jrobin.core.RrdDb#createFetchRequest(java.lang.String, long, long, long)
37  * createFetchRequest()} method of your {@link org.jrobin.core.RrdDb RrdDb} object.
38  *
39  * @author <a HREF="mailto:saxon@jrobin.org">Sasa Markovic</a>
40  */

41 public class FetchRequest {
42     private RrdDb parentDb;
43     private String JavaDoc consolFun;
44     private long fetchStart;
45     private long fetchEnd;
46     private long resolution;
47     private String JavaDoc[] filter;
48
49     FetchRequest(RrdDb parentDb, String JavaDoc consolFun, long fetchStart, long fetchEnd,
50         long resolution) throws RrdException {
51         this.parentDb = parentDb;
52         this.consolFun = consolFun;
53         this.fetchStart = fetchStart;
54         this.fetchEnd = fetchEnd;
55         this.resolution = resolution;
56         validate();
57     }
58
59     /**
60      * Sets request filter in order to fetch data only for
61      * the specified array of datasources (datasource names).
62      * If not set (or set to null), fetched data will
63      * containt values of all datasources defined in the corresponding RRD.
64      * To fetch data only from selected
65      * datasources, specify an array of datasource names as method argument.
66      * @param filter Array of datsources (datsource names) to fetch data from.
67      */

68     public void setFilter(String JavaDoc[] filter) {
69         this.filter = filter;
70     }
71
72     /**
73      * Sets request filter in order to fetch data only for
74      * a single datasource (datasource name).
75      * If not set (or set to null), fetched data will
76      * containt values of all datasources defined in the corresponding RRD.
77      * To fetch data for a single datasource only,
78      * specify an array of datasource names as method argument.
79      * @param filter Array of datsources (datsource names) to fetch data from.
80      */

81     public void setFilter(String JavaDoc filter) {
82         this.filter = (filter == null)? null: (new String JavaDoc[] { filter });
83     }
84
85     /**
86      * Returns request filter. See {@link #setFilter(String[]) setFilter()} for
87      * complete explanation.
88      * @return Request filter (array of datasource names), null if not set.
89      */

90     public String JavaDoc[] getFilter() {
91         return filter;
92     }
93
94     /**
95      * Returns consolitation function to be used during the fetch process.
96      * @return Consolidation function.
97      */

98     public String JavaDoc getConsolFun() {
99         return consolFun;
100     }
101
102     /**
103      * Returns starting timestamp to be used for the fetch request.
104      * @return Starting timstamp in seconds.
105      */

106     public long getFetchStart() {
107         return fetchStart;
108     }
109
110     /**
111      * Returns ending timestamp to be used for the fetch request.
112      * @return Ending timestamp in seconds.
113      */

114     public long getFetchEnd() {
115         return fetchEnd;
116     }
117
118     /**
119      * Returns fetch resolution to be used for the fetch request.
120      * @return Fetch resolution in seconds.
121      */

122     public long getResolution() {
123         return resolution;
124     }
125
126     private void validate() throws RrdException {
127         if(!ArcDef.isValidConsolFun(consolFun)) {
128             throw new RrdException("Invalid consolidation function in fetch request: " + consolFun);
129         }
130         if(fetchStart < 0) {
131             throw new RrdException("Invalid start time in fetch request: " + fetchStart);
132         }
133         if(fetchEnd < 0) {
134             throw new RrdException("Invalid end time in fetch request: " + fetchEnd);
135         }
136         if(fetchStart > fetchEnd) {
137             throw new RrdException("Invalid start/end time in fetch request: " + fetchStart +
138                 " > " + fetchEnd);
139         }
140         if(resolution <= 0) {
141             throw new RrdException("Invalid resolution in fetch request: " + resolution);
142         }
143     }
144
145     /**
146      * Dumps the content of fetch request using the syntax of RRDTool's fetch command.
147      * @return Fetch request dump.
148      */

149     public String JavaDoc dump() {
150         return "fetch \"" + parentDb.getRrdBackend().getPath() +
151             "\" " + consolFun + " --start " + fetchStart + " --end " + fetchEnd +
152             (resolution > 1? " --resolution " + resolution: "");
153     }
154
155     String JavaDoc getRrdToolCommand() {
156         return dump();
157     }
158
159     /**
160      * Returns data from the underlying RRD as an array of
161      * {@link org.jrobin.core.FetchPoint FetchPoint} objects. Each fetch point object represents
162      * RRD datasource values for the specific timestamp. Timestamp difference between
163      * consecutive fecth points is guaranteed to be constant.
164      * @return Array of fetch points.
165      * @throws RrdException Thrown in case of JRobin specific error.
166      * @throws IOException Thrown in case of I/O error.
167      * @deprecated As of version 1.2.0 replaced with {@link #fetchData() fetchData()}.
168      */

169     public FetchPoint[] fetch() throws RrdException, IOException JavaDoc {
170         return parentDb.fetch(this);
171     }
172
173     /**
174      * Returns data from the underlying RRD and puts it in a single
175      * {@link org.jrobin.core.FetchData FetchData} object. Use this method instead of
176      * deprecated {@link #fetch() fetch()} method.
177      * @return FetchPoint object filled with timestamps and datasource values.
178      * @throws RrdException Thrown in case of JRobin specific error.
179      * @throws IOException Thrown in case of I/O error.
180      */

181     public FetchData fetchData() throws RrdException, IOException JavaDoc {
182         return parentDb.fetchData(this);
183     }
184
185     /**
186      * Returns the underlying RrdDb object.
187      * @return RrdDb object used to create this FetchRequest object.
188      */

189     public RrdDb getParentDb() {
190         return parentDb;
191     }
192
193 }
194
Popular Tags