001    /*
002     * Copyright (C) 2001 Ciaran Treanor <ciaran@codeloop.com>
003     *
004     * Distributable under GPL license.
005     * See terms of license at gnu.org.
006     *
007     * $Id: DataSource.java,v 1.2 2006/12/21 18:02:42 tarus Exp $
008     */
009    package org.jrobin.core.jrrd;
010    
011    import java.io.IOException;
012    import java.io.PrintStream;
013    import java.text.NumberFormat;
014    
015    /**
016     * Instances of this class model a data source in an RRD file.
017     *
018     * @author <a href="mailto:ciaran@codeloop.com">Ciaran Treanor</a>
019     * @version $Revision: 1.2 $
020     */
021    public class DataSource {
022    
023            long offset;
024            long size;
025            String name;
026            DataSourceType type;
027            int minimumHeartbeat;
028            double minimum;
029            double maximum;
030            PDPStatusBlock pdpStatusBlock;
031    
032            DataSource(RRDFile file) throws IOException {
033    
034                    offset = file.getFilePointer();
035                    name = file.readString(Constants.DS_NAM_SIZE);
036                    type = DataSourceType.get(file.readString(Constants.DST_SIZE));
037    
038                    file.align(8);
039    
040                    minimumHeartbeat = file.readInt(true);
041    
042                    file.align(8);
043    
044                    minimum = file.readDouble();
045                    maximum = file.readDouble();
046    
047                    // Skip rest of ds_def_t.par[]
048                    file.align();
049                    file.skipBytes(56);
050    
051                    size = file.getFilePointer() - offset;
052            }
053    
054            void loadPDPStatusBlock(RRDFile file) throws IOException {
055                    pdpStatusBlock = new PDPStatusBlock(file);
056            }
057    
058            /**
059             * Returns the primary data point status block for this data source.
060             *
061             * @return the primary data point status block for this data source.
062             */
063            public PDPStatusBlock getPDPStatusBlock() {
064                    return pdpStatusBlock;
065            }
066    
067            /**
068             * Returns the minimum required heartbeat for this data source.
069             *
070             * @return the minimum required heartbeat for this data source.
071             */
072            public int getMinimumHeartbeat() {
073                    return minimumHeartbeat;
074            }
075    
076            /**
077             * Returns the minimum value input to this data source can have.
078             *
079             * @return the minimum value input to this data source can have.
080             */
081            public double getMinimum() {
082                    return minimum;
083            }
084    
085            /**
086             * Returns the type this data source is.
087             *
088             * @return the type this data source is.
089             * @see DataSourceType
090             */
091            public DataSourceType getType() {
092                    return type;
093            }
094    
095            /**
096             * Returns the maximum value input to this data source can have.
097             *
098             * @return the maximum value input to this data source can have.
099             */
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    }