View Javadoc

1   /*
2    * Copyright (C) 2001 Ciaran Treanor <ciaran@codeloop.com>
3    *
4    * Distributable under GPL license.
5    * See terms of license at gnu.org.
6    *
7    * $Id: DataSource.java,v 1.2 2006/12/21 18:02:42 tarus Exp $
8    */
9   package org.jrobin.core.jrrd;
10  
11  import java.io.IOException;
12  import java.io.PrintStream;
13  import java.text.NumberFormat;
14  
15  /**
16   * Instances of this class model a data source in an RRD file.
17   *
18   * @author <a href="mailto:ciaran@codeloop.com">Ciaran Treanor</a>
19   * @version $Revision: 1.2 $
20   */
21  public class DataSource {
22  
23  	long offset;
24  	long size;
25  	String name;
26  	DataSourceType type;
27  	int minimumHeartbeat;
28  	double minimum;
29  	double maximum;
30  	PDPStatusBlock pdpStatusBlock;
31  
32  	DataSource(RRDFile file) throws IOException {
33  
34  		offset = file.getFilePointer();
35  		name = file.readString(Constants.DS_NAM_SIZE);
36  		type = DataSourceType.get(file.readString(Constants.DST_SIZE));
37  
38  		file.align(8);
39  
40  		minimumHeartbeat = file.readInt(true);
41  
42  		file.align(8);
43  
44  		minimum = file.readDouble();
45  		maximum = file.readDouble();
46  
47  		// Skip rest of ds_def_t.par[]
48  		file.align();
49  		file.skipBytes(56);
50  
51  		size = file.getFilePointer() - offset;
52  	}
53  
54  	void loadPDPStatusBlock(RRDFile file) throws IOException {
55  		pdpStatusBlock = new PDPStatusBlock(file);
56  	}
57  
58  	/**
59  	 * Returns the primary data point status block for this data source.
60  	 *
61  	 * @return the primary data point status block for this data source.
62  	 */
63  	public PDPStatusBlock getPDPStatusBlock() {
64  		return pdpStatusBlock;
65  	}
66  
67  	/**
68  	 * Returns the minimum required heartbeat for this data source.
69  	 *
70  	 * @return the minimum required heartbeat for this data source.
71  	 */
72  	public int getMinimumHeartbeat() {
73  		return minimumHeartbeat;
74  	}
75  
76  	/**
77  	 * Returns the minimum value input to this data source can have.
78  	 *
79  	 * @return the minimum value input to this data source can have.
80  	 */
81  	public double getMinimum() {
82  		return minimum;
83  	}
84  
85  	/**
86  	 * Returns the type this data source is.
87  	 *
88  	 * @return the type this data source is.
89  	 * @see DataSourceType
90  	 */
91  	public DataSourceType getType() {
92  		return type;
93  	}
94  
95  	/**
96  	 * Returns the maximum value input to this data source can have.
97  	 *
98  	 * @return the maximum value input to this data source can have.
99  	 */
100 	public double getMaximum() {
101 		return maximum;
102 	}
103 
104 	/**
105 	 * Returns the name of this data source.
106 	 *
107 	 * @return the name of this data source.
108 	 */
109 	public String getName() {
110 		return name;
111 	}
112 
113 	void printInfo(PrintStream s, NumberFormat numberFormat) {
114 
115 		StringBuffer sb = new StringBuffer("ds[");
116 
117 		sb.append(name);
118 		s.print(sb);
119 		s.print("].type = \"");
120 		s.print(type);
121 		s.println("\"");
122 		s.print(sb);
123 		s.print("].minimal_heartbeat = ");
124 		s.println(minimumHeartbeat);
125 		s.print(sb);
126 		s.print("].min = ");
127 		s.println(Double.isNaN(minimum)
128 				? "NaN"
129 				: numberFormat.format(minimum));
130 		s.print(sb);
131 		s.print("].max = ");
132 		s.println(Double.isNaN(maximum)
133 				? "NaN"
134 				: numberFormat.format(maximum));
135 		s.print(sb);
136 		s.print("].last_ds = ");
137 		s.println(pdpStatusBlock.lastReading);
138 		s.print(sb);
139 		s.print("].value = ");
140 
141 		double value = pdpStatusBlock.value;
142 
143 		s.println(Double.isNaN(value)
144 				? "NaN"
145 				: numberFormat.format(value));
146 		s.print(sb);
147 		s.print("].unknown_sec = ");
148 		s.println(pdpStatusBlock.unknownSeconds);
149 	}
150 
151 	void toXml(PrintStream s) {
152 
153 		s.println("\t<ds>");
154 		s.print("\t\t<name> ");
155 		s.print(name);
156 		s.println(" </name>");
157 		s.print("\t\t<type> ");
158 		s.print(type);
159 		s.println(" </type>");
160 		s.print("\t\t<minimal_heartbeat> ");
161 		s.print(minimumHeartbeat);
162 		s.println(" </minimal_heartbeat>");
163 		s.print("\t\t<min> ");
164 		s.print(minimum);
165 		s.println(" </min>");
166 		s.print("\t\t<max> ");
167 		s.print(maximum);
168 		s.println(" </max>");
169 		s.println();
170 		s.println("\t\t<!-- PDP Status -->");
171 		s.print("\t\t<last_ds> ");
172 		s.print(pdpStatusBlock.lastReading);
173 		s.println(" </last_ds>");
174 		s.print("\t\t<value> ");
175 		s.print(pdpStatusBlock.value);
176 		s.println(" </value>");
177 		s.print("\t\t<unknown_sec> ");
178 		s.print(pdpStatusBlock.unknownSeconds);
179 		s.println(" </unknown_sec>");
180 		s.println("\t</ds>");
181 		s.println();
182 	}
183 
184 	/**
185 	 * Returns a summary the contents of this data source.
186 	 *
187 	 * @return a summary of the information contained in this data source.
188 	 */
189 	public String toString() {
190 
191 		StringBuffer sb = new StringBuffer("[DataSource: OFFSET=0x");
192 
193 		sb.append(Long.toHexString(offset));
194 		sb.append(", SIZE=0x");
195 		sb.append(Long.toHexString(size));
196 		sb.append(", name=");
197 		sb.append(name);
198 		sb.append(", type=");
199 		sb.append(type.toString());
200 		sb.append(", minHeartbeat=");
201 		sb.append(minimumHeartbeat);
202 		sb.append(", min=");
203 		sb.append(minimum);
204 		sb.append(", max=");
205 		sb.append(maximum);
206 		sb.append("]");
207 		sb.append("\n\t\t");
208 		sb.append(pdpStatusBlock.toString());
209 
210 		return sb.toString();
211 	}
212 }