| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 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 | |
|
| 28 | |
|
| 29 | |
|
| 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; |
| 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 | |
|
| 48 | |
|
| 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 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public synchronized void reset() { |
| 59 | 3 | this.observationSet.clear(); |
| 60 | |
|
| 61 | 3 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 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 | |
|
| 100 | |
|
| 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 | |
} |