KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > ValueExtractor


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  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
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 package org.jrobin.graph;
26
27 import org.jrobin.core.FetchData;
28 import org.jrobin.core.RrdException;
29 import org.jrobin.core.Util;
30
31 /**
32  * <p>Class used to extract specific time-based values out of a number of fetched datasources.</p>
33  *
34  * @author Arne Vandamme (cobralord@jrobin.org)
35  */

36 class ValueExtractor
37 {
38     // ================================================================
39
// -- Members
40
// ================================================================
41
private String JavaDoc[] varNames; // Name of the variable, NOT it's dsName in the file
42

43     private int reduceFactor = 1;
44
45     private int[] tPos;
46     private long[] steps;
47     private long[][] timestamps;
48
49     private double[][][] dsValues;
50
51
52     // ================================================================
53
// -- Constructors
54
// ================================================================
55
/**
56      * Constructs a ValueExtractor object used to extract fetched datapoints for specific points in time.
57      * @param names Array containing the datasource names in the graph definition.
58      * @param values Array of FetchData objects holding all fetched datasources for a specific RRD file.
59      */

60     ValueExtractor( String JavaDoc[] names, FetchData[] values, int reduceFactor )
61     {
62         this.varNames = names;
63         this.reduceFactor = reduceFactor;
64
65         // Set timestamps
66
tPos = new int[values.length];
67         steps = new long[values.length];
68         timestamps = new long[values.length][];
69
70         dsValues = new double[values.length][][];
71
72         for (int i = 0; i < timestamps.length; i++)
73         {
74             if ( values[i] != null )
75             {
76                 timestamps[i] = values[i].getTimestamps();
77                 dsValues[i] = values[i].getValues();
78
79                 if ( timestamps[i].length >= 2 )
80                     steps[i] = (timestamps[i][1] - timestamps[i][0]);
81             }
82         }
83     }
84
85
86     // ================================================================
87
// -- Protected methods
88
// ================================================================
89
int prepareSources( Source[] sources, int offset )
90     {
91         int tblPos = offset;
92
93         for ( int i = 0; i < dsValues.length; i++ )
94         {
95             if ( dsValues[i] != null )
96             {
97                 for (int x = 0; x < dsValues[i].length; x++)
98                 {
99                     sources[tblPos++].setFetchedStep( steps[i] );
100                 }
101             }
102         }
103
104         return tblPos;
105     }
106
107     /**
108      * Extracts a number of values out of the fetched values, and approximates them
109      * to a specific timestamp, to store them in the complete Source array for the graph definition.
110      * @param timestamp Timestamp to which a fetched value should be approximated.
111      * @param sources Array containing all datasources.
112      * @param row Row index in the Source table where the values should stored.
113      * @param offset Offset in the Source table of where to start storing the values.
114      * @return Table position offset for the next datasource.
115      * @throws RrdException Thrown in case of a JRobin specific error.
116      */

117     int extract( long timestamp, Source[] sources, int row, int offset ) throws RrdException
118     {
119         int tblPos = offset;
120
121         for ( int i = 0; i < dsValues.length; i++ )
122         {
123             if ( dsValues[i] == null )
124                 continue;
125
126             int tIndex = tPos[i];
127
128             double[] nValue = new double[ dsValues[i].length ];
129             int[] vValue = new int[ nValue.length ]; // Counts the actual valid values
130

131             for ( int j = 0; j < nValue.length; j++ )
132                 nValue[j] = Double.NaN;
133
134             // Combine the rows
135
int j;
136
137             for ( j = 0; j < reduceFactor && timestamps[i][tIndex] <= timestamp; j++ )
138             {
139                 for (int x = 0; x < dsValues[i].length; x++)
140                 {
141                     if ( Double.isNaN(dsValues[i][x][tIndex]) )
142                         continue;
143
144                     vValue[x]++;
145
146                     if ( Double.isNaN(nValue[x]) )
147                         nValue[x] = dsValues[i][x][tIndex];
148                     else
149                     {
150                         switch ( i )
151                         {
152                             case FetchSource.AVG:
153                                 nValue[x] += dsValues[i][x][tIndex];
154                                 break;
155                             case FetchSource.MAX:
156                                 nValue[x] = Util.max( nValue[x], dsValues[i][x][tIndex] );
157                                 break;
158                             case FetchSource.MIN:
159                                 nValue[x] = Util.min( nValue[x], dsValues[i][x][tIndex] );
160                                 break;
161                             case FetchSource.LAST:
162                                 nValue[x] = dsValues[i][x][tIndex];
163                                 break;
164                         }
165                     }
166                 }
167
168                 tIndex++;
169             }
170
171             // See if we are using a stretched timespan
172
if ( j == 0 && row > 0 )
173             {
174                 sources[tblPos++].set( row, timestamp, Double.POSITIVE_INFINITY );
175             }
176             else
177             {
178                 // Finalize
179
for (int x = 0; x < dsValues[i].length; x++)
180                 {
181                     if ( i == FetchSource.AVG )
182                         nValue[x] /= vValue[x];
183
184                     sources[tblPos++].set( row, timestamp, nValue[x] );
185                 }
186             }
187
188             tPos[i] = tIndex;
189         }
190
191         return tblPos;
192     }
193
194     String JavaDoc[] getNames() {
195         return varNames;
196     }
197 }
198
Popular Tags