1 package org.jrobin.graph; 2 3 import java.util.HashMap ; 4 5 import org.jrobin.core.RrdException; 6 import org.jrobin.core.XmlWriter; 7 8 15 class Sdef extends Cdef 16 { 17 private int defIndex = -1; 21 22 private String defName = ""; 23 private String consolFunc = "AVERAGE"; 24 private int aggregate = Source.AGG_AVERAGE; 25 26 private boolean calculated = false; 27 private double[] value = null; 28 29 30 42 Sdef( String name, String defName, String consolFunc ) throws RrdException 43 { 44 super( name ); 45 46 this.defName = defName; 47 this.consolFunc = consolFunc; 48 49 if ( consolFunc.equalsIgnoreCase("AVERAGE") || consolFunc.equalsIgnoreCase("AVG") ) 51 aggregate = Source.AGG_AVERAGE; 52 else if ( consolFunc.equalsIgnoreCase("MAX") || consolFunc.equalsIgnoreCase("MAXIMUM") ) 53 aggregate = Source.AGG_MAXIMUM; 54 else if ( consolFunc.equalsIgnoreCase("MIN") || consolFunc.equalsIgnoreCase("MINIMUM") ) 55 aggregate = Source.AGG_MINIMUM; 56 else if ( consolFunc.equalsIgnoreCase("LAST") ) 57 aggregate = Source.AGG_LAST; 58 else if ( consolFunc.equalsIgnoreCase("FIRST") ) 59 aggregate = Source.AGG_FIRST; 60 else if ( consolFunc.equalsIgnoreCase("TOTAL") ) 61 aggregate = Source.AGG_TOTAL; 62 else 63 throw new RrdException( "Invalid consolidation function specified." ); 64 } 65 66 77 void prepare( HashMap sourceIndex, int numPoints, int aggregatePoints ) throws RrdException 78 { 79 if ( sourceIndex.containsKey( defName ) ) 80 defIndex = ( (Integer ) sourceIndex.get( defName ) ).intValue(); 81 else 82 throw new RrdException( "Datasource not found: " + defName ); 83 84 values = new double[ numPoints ]; 85 this.aggregatePoints = aggregatePoints; 86 calculated = false; 87 } 88 89 98 int calculateLevel( int[] levels ) 99 { 100 return levels[defIndex] + 1; 102 } 103 104 111 double get( int pos ) 112 { 113 return values[0]; 114 } 115 116 121 void set( Source[] sources ) 122 { 123 if ( calculated ) return; 124 125 double value = sources[ defIndex ].getAggregate( aggregate ); 126 for ( int i = 0; i < values.length; i++ ) 127 values[i] = value; 128 129 calculated = true; 130 } 131 132 146 double getAggregate( int aggType ) 147 { 148 switch ( aggType ) 149 { 150 case AGG_MINIMUM: 151 case AGG_MAXIMUM: 152 case AGG_AVERAGE: 153 case AGG_FIRST: 154 case AGG_LAST: 155 return values[0]; 156 157 case AGG_TOTAL: 158 return (values[0] * values.length) ; 159 } 160 161 return Double.NaN; 162 } 163 164 165 void exportXml(XmlWriter xml) { 166 xml.startTag( "def" ); 167 xml.writeTag( "name", getName() ); 168 xml.writeTag( "datasource", defName ); 169 xml.writeTag( "cf", consolFunc ); 170 xml.closeTag(); } 172 } 173 | Popular Tags |