public String getStatistic(final Statistic statistic) { switch (statistic.getType()) { case INCREMENTOR: return valueMap.containsKey(statistic) ? valueMap.get(statistic) : "0"; case AVERAGE: final String avgStrValue = valueMap.get(statistic); AverageBean avgBean = new AverageBean(); if (avgStrValue != null && avgStrValue.length() > 0) { try { avgBean = JsonUtil.deserialize(avgStrValue, AverageBean.class); } catch (Exception e) { LOGGER.trace( "unable to parse statistics value for stat " + statistic.toString() + ", value=" + avgStrValue); } } return avgBean.getAverage().toString(); default: return ""; } }
public static StatisticsBundle input(final String inputString) { final Map<Statistic, String> srcMap = new HashMap<>(); final Map<String, String> loadedMap = JsonUtil.deserializeStringMap(inputString); for (final String key : loadedMap.keySet()) { try { srcMap.put(Statistic.valueOf(key), loadedMap.get(key)); } catch (IllegalArgumentException e) { LOGGER.error("error parsing statistic key '" + key + "', reason: " + e.getMessage()); } } final StatisticsBundle bundle = new StatisticsBundle(); for (final Statistic loopStat : Statistic.values()) { final String value = srcMap.get(loopStat); if (value != null && !value.equals("")) { bundle.valueMap.put(loopStat, value); } } return bundle; }
public synchronized void updateAverageValue(final Statistic statistic, final long timeDuration) { if (Statistic.Type.AVERAGE != statistic.getType()) { LOGGER.error("attempt to update average value of non-average stat " + statistic); return; } final String avgStrValue = valueMap.get(statistic); AverageBean avgBean = new AverageBean(); if (avgStrValue != null && avgStrValue.length() > 0) { try { avgBean = JsonUtil.deserialize(avgStrValue, AverageBean.class); } catch (Exception e) { LOGGER.trace( "unable to parse statistics value for stat " + statistic.toString() + ", value=" + avgStrValue); } } avgBean.appendValue(timeDuration); valueMap.put(statistic, JsonUtil.serialize(avgBean)); }
public synchronized void incrementValue(final Statistic statistic) { if (Statistic.Type.INCREMENTOR != statistic.getType()) { LOGGER.error("attempt to increment non-counter/incremental stat " + statistic); return; } BigInteger currentValue = BigInteger.ZERO; try { if (valueMap.containsKey(statistic)) { currentValue = new BigInteger(valueMap.get(statistic)); } else { currentValue = BigInteger.ZERO; } } catch (NumberFormatException e) { LOGGER.error("error reading counter/incremental stat " + statistic); } final BigInteger newValue = currentValue.add(BigInteger.ONE); valueMap.put(statistic, newValue.toString()); }