Exemplo n.º 1
0
  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 "";
    }
  }
Exemplo n.º 2
0
  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());
  }
Exemplo n.º 3
0
  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));
  }