Coverage Report - net.admin4j.timer.RollingTimePeriodDataMeasure
 
Classes in this File Line Coverage Branch Coverage Complexity
RollingTimePeriodDataMeasure
90%
40/44
93%
15/16
1.571
RollingTimePeriodDataMeasure$TimedObservation
100%
6/6
N/A
1.571
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.timer;
 15  
 
 16  
 import java.util.ArrayList;
 17  
 import java.util.Date;
 18  
 import java.util.HashSet;
 19  
 import java.util.List;
 20  
 import java.util.Set;
 21  
 
 22  
 import net.admin4j.deps.commons.lang3.Validate;
 23  
 import net.admin4j.deps.commons.lang3.time.DurationFormatUtils;
 24  
 import net.admin4j.vo.DataMeasurementSummaryVO;
 25  
 
 26  
 /**
 27  
  * Will keep observations for the last specified time period (e.g. all observations 
 28  
  * from the last 10 minutes)
 29  
  * @author D. Ashmore
 30  
  *
 31  
  */
 32  
 public class RollingTimePeriodDataMeasure implements DataMeasure {
 33  
     
 34  
     private static final long serialVersionUID = -4023496364533544416L;
 35  3
     public static final Long DEFAULT_TIMER_PERIOD_IN_MILLIS=60000*60L; // 1 hr
 36  
     private Long timePeriodInMillis;
 37  6
     private Set<TimedObservation> observationSet = new HashSet<TimedObservation>();
 38  
     
 39  
     public RollingTimePeriodDataMeasure() {
 40  3
         this(DEFAULT_TIMER_PERIOD_IN_MILLIS);
 41  3
     }
 42  
     
 43  6
     public RollingTimePeriodDataMeasure(long timePeriodInMillis) {
 44  6
         this.timePeriodInMillis = timePeriodInMillis;
 45  6
     }
 46  
 
 47  
     /* (non-Javadoc)
 48  
      * @see net.admin4j.timer.DataMeasure#addNumber()
 49  
      */
 50  
     public synchronized void addNumber(Number number) {
 51  
         Validate.notNull(number, "Null number not allowed.");
 52  30
         observationSet.add(new TimedObservation(number));
 53  30
     }
 54  
 
 55  
     /* (non-Javadoc)
 56  
      * @see net.admin4j.timer.DataMeasure#reset()
 57  
      */
 58  
     public synchronized void reset() {
 59  3
         this.observationSet.clear();
 60  
 
 61  3
     }
 62  
 
 63  
     /* (non-Javadoc)
 64  
      * @see net.admin4j.timer.DataMeasure#purgeObsoleteObservations()
 65  
      */
 66  
     public synchronized void purgeObsoleteObservations() {
 67  6
         long earliestTime = getEarliestObservationTime();
 68  6
         Set<TimedObservation> removedObservations = new HashSet<TimedObservation>();
 69  6
         for (TimedObservation obs: this.observationSet) {
 70  30
             if (obs.getTime() < earliestTime) {
 71  15
                 removedObservations.add(obs);
 72  
             }
 73  
         }
 74  
         
 75  6
         this.observationSet.removeAll(removedObservations);        
 76  6
     }
 77  
 
 78  
     private long getEarliestObservationTime() {
 79  15
         long earliestTime = System.currentTimeMillis() - this.timePeriodInMillis;
 80  15
         return earliestTime;
 81  
     }
 82  
     
 83  
     private static class TimedObservation {
 84  
         private long time;
 85  
         private Number value;
 86  
         
 87  30
         public TimedObservation(Number value) {
 88  30
             this.value = value.doubleValue();
 89  30
             this.time = System.currentTimeMillis();
 90  30
         }
 91  
         public long getTime() {
 92  81
             return time;
 93  
         }
 94  
         public Number getValue() {
 95  15
             return value;
 96  
         }
 97  
     }
 98  
 
 99  
     /* (non-Javadoc)
 100  
      * @see net.admin4j.timer.DataMeasure#getDataMeasurementSummary()
 101  
      */
 102  
     public synchronized DataMeasurementSummaryVO getDataMeasurementSummary() {
 103  9
         List<Number> valueList = new ArrayList<Number>();
 104  9
         long firstObsTime = Long.MAX_VALUE;
 105  9
         long lastObsTime = Long.MIN_VALUE;
 106  
         
 107  9
         long earliestTime = getEarliestObservationTime();
 108  9
         for (TimedObservation obs: this.observationSet) {
 109  15
             if (obs.getTime() >= earliestTime) {
 110  15
                 valueList.add(obs.getValue());
 111  15
                 if (obs.getTime() < firstObsTime) {
 112  3
                     firstObsTime = obs.getTime();
 113  
                 }
 114  15
                 if (obs.getTime() > lastObsTime) {
 115  3
                     lastObsTime = obs.getTime();
 116  
                 }
 117  
             }
 118  
         }
 119  9
         DataMeasurementSummaryVO summary = DataMeasurementSummaryVO.fromValueList(valueList);
 120  
         summary.setLabel("Observations since " + DurationFormatUtils.formatDuration(this.timePeriodInMillis, "H hours, mm min."));
 121  9
         summary.setSummaryType(DataMeasurementSummaryVO.SummaryType.ROLLING_TIME);
 122  9
         if (firstObsTime < Long.MAX_VALUE) {
 123  3
             summary.setFirstObservationDate(new Date(firstObsTime));
 124  
         }
 125  9
         if (lastObsTime > Long.MIN_VALUE) {
 126  3
             summary.setLastObservationDate(new Date(lastObsTime));
 127  
         }
 128  9
         return summary;
 129  
     }
 130  
 
 131  
     public Long getTimePeriodInMillis() {
 132  3
         return timePeriodInMillis;
 133  
     }
 134  
 
 135  
     protected void setTimePeriodInMillis(Long timePeriodInMillis) {
 136  0
         this.timePeriodInMillis = timePeriodInMillis;
 137  0
     }
 138  
 
 139  
     public Set<TimedObservation> getObservationSet() {
 140  9
         return observationSet;
 141  
     }
 142  
 
 143  
     public void setObservationSet(Set<TimedObservation> observationSet) {
 144  0
         this.observationSet = observationSet;
 145  0
     }
 146  
 
 147  
 }