/*******************************************************************************
* Copyright 2014 Miami-Dade County
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.sharegov.cirm.stats;
import java.util.Map;
import java.util.TreeMap;
/**
* Utilities for calculations on CirmStatistics Objects.
*
* @author Thomas Hilpold
*/
public class CirmStatisticsUtils {
/**
* Generates a new CirmStatistics, which holds speed per hour as values (all rounded values).
*
* @param stats
* @param statsToMinusFromTimeMs
* @return
*/
public CirmStatistics calculatePerHour(CirmStatistics stats, long statsToMinusFromTimeMs) {
CirmStatistics speedStats = new CirmStatistics();
double factorToHourly = 60 * 1000.0 / statsToMinusFromTimeMs;
TreeMap<CirmStatistics.StatsKey, CirmStatistics.StatsValue> statsMap = stats.getStatistics();
for (Map.Entry<CirmStatistics.StatsKey, CirmStatistics.StatsValue> toEntry : statsMap.entrySet()) {
CirmStatistics.StatsKey k = toEntry.getKey();
CirmStatistics.StatsValue v = toEntry.getValue();
CirmStatistics.StatsValue speedVal = speedStats.getEntry(k.getComponent(), k.getAction(), k.gettype());
speedVal.aggregate(v);
speedVal.setFailureCount(Math.round(v.getFailureCount() * factorToHourly));
speedVal.setSuccessCount(Math.round(v.getSuccessCount() * factorToHourly));
}
return speedStats;
}
public CirmStatistics calculateDiff(CirmStatistics from, CirmStatistics to) {
CirmStatistics diff = new CirmStatistics();
TreeMap<CirmStatistics.StatsKey, CirmStatistics.StatsValue> fromStats = from.getStatistics();
for (Map.Entry<CirmStatistics.StatsKey, CirmStatistics.StatsValue> toEntry : to.getStatistics().entrySet()) {
CirmStatistics.StatsKey toKey = toEntry.getKey();
CirmStatistics.StatsValue toVal = toEntry.getValue();
CirmStatistics.StatsValue fromVal = null;
if (fromStats.containsKey(toKey)) {
fromVal = fromStats.get(toKey);
}
CirmStatistics.StatsValue diffValue = getDiff(fromVal, toVal);
diff.getEntry(toKey.getComponent(), toKey.getAction(), toKey.gettype()).aggregate(diffValue);
}
//TODO check if any keys were removed between from and to...
//So far we assume that to has all keys ever and there is no from key that's not in to.
//NOT YET IMPLEMENTED
return diff;
}
CirmStatistics.StatsValue getDiff(CirmStatistics.StatsValue from, CirmStatistics.StatsValue to) {
CirmStatistics.StatsValue diff = new CirmStatistics.StatsValue();
if (from == null) {
diff.aggregate(to);
} else if (to == null) {
diff.aggregate(from);
diff.setFailureCount(-from.getFailureCount());
diff.setSuccessCount(-from.getSuccessCount());
} else {
diff.aggregate(to);
diff.setFailureCount(to.getFailureCount()-from.getFailureCount());
diff.setSuccessCount(to.getSuccessCount()-from.getSuccessCount());
}
return diff;
}
}