@Override public void storeIncomingStatistics(List<LiveStatistics> liveStatisticsList) { for (LiveStatistics ls : liveStatisticsList) { long hoursSince1970 = ls.getTimeperiod() / 240; int fifteenSecondPeriodsSinceStartOfHour = LiveStatisticsUtil.getFifteensecondTimeperiodsSinceStartOfHour(ls.getTimeperiod() * 15); Double calculatedValue = LiveStatisticsUtil.calculateValueBasedOnUnitType( ls.getValue(), UnitType.fromValue(ls.getUnitType())); BasicMetricHour mhToStore = metricHoursToStoreHash.get( ls.getAccountName() + ";" + ls.getGuiPath() + ";" + hoursSince1970); boolean wasInStoreHash = mhToStore != null; if (mhToStore == null) { // Not in store-hash chech in metricHourCache mhToStore = metricHourCache.getIfPresent( ls.getAccountName() + ";" + ls.getGuiPath() + ";" + hoursSince1970); } if (mhToStore == null) { // Not in metricHourCache, fetch from Riak mhToStore = getMetricHour(ls.getAccountName(), ls.getGuiPath(), hoursSince1970); } if (mhToStore == null) { // Not in Riak, create mhToStore = new BasicMetricHour( ls.getGuiPath(), ls.getAccountName(), hoursSince1970, ls.getValueType(), ls.getUnitType()); } mhToStore.getMetrics()[fifteenSecondPeriodsSinceStartOfHour] = LiveStatisticsUtil.calculateValueBasedOnValueType( ls, calculatedValue, ValueType.fromValue(ls.getValueType())); if (!wasInStoreHash) { metricHoursToStoreHash.put( ls.getAccountName() + ";" + ls.getGuiPath() + ";" + hoursSince1970, mhToStore); } } persistRecentMetricHours(); }
@Override public void storeIncomingStatistics( String guiPath, String accountName, Long timeperiod, String value, ValueType valueType, UnitType unitType) { Double valueDouble = LiveStatisticsUtil.parseDouble(value); Double calculatedValue = LiveStatisticsUtil.calculateValueBasedOnUnitType(valueDouble, unitType); long hoursSince1970 = timeperiod / 240; int fifteenSecondPeriodsSinceStartOfHour = LiveStatisticsUtil.getFifteensecondTimeperiodsSinceStartOfHour(timeperiod * 15); Bucket myBucket = null; try { myBucket = riakClient.fetchBucket(accountName + ";" + hoursSince1970).execute(); BasicMetricHour storedMetricHour = myBucket.fetch("" + guiPath, BasicMetricHour.class).execute(); if (storedMetricHour == null) { storedMetricHour = new BasicMetricHour( guiPath, accountName, hoursSince1970, valueType.toString(), unitType.toString()); } Double prevValue = storedMetricHour.getMetrics()[fifteenSecondPeriodsSinceStartOfHour]; if (prevValue == null) { storedMetricHour.getMetrics()[fifteenSecondPeriodsSinceStartOfHour] = calculatedValue; } else { storedMetricHour.getMetrics()[fifteenSecondPeriodsSinceStartOfHour] = LiveStatisticsUtil.calculateValueBasedOnValueType( prevValue, calculatedValue, valueType); } myBucket.store("" + guiPath, storedMetricHour).execute(); } catch (RiakRetryFailedException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } }
@Override public List<LiveStatistics> getLiveStatistics( String guiPath, String accountName, Long minTimeperiod, Long maxTimeperiod) { Long fromHoursSince1970 = minTimeperiod / 240; Long toHoursSince1970 = maxTimeperiod / 240; List<LiveStatistics> retList = new ArrayList<LiveStatistics>(); for (Long index = fromHoursSince1970; index <= toHoursSince1970; index++) { BasicMetricHour metricHour = getMetricHour(accountName, guiPath, index); if (metricHour == null) { metricHour = new BasicMetricHour( guiPath, accountName, index, ValueType.VALUE.value(), UnitType.N.value()); } // If this is the first hour, start from the correct 15-second timeslot within the hour Integer minTimeperiodWithinTheHour = 0; if (index.longValue() == fromHoursSince1970.longValue()) { minTimeperiodWithinTheHour = LiveStatisticsUtil.getFifteensecondTimeperiodsSinceStartOfHour(minTimeperiod * 15); } // If this is the last hour, end with the correct 15-second timeslot within the hour Integer maxTimeperiodWithinTheHour = null; if (index.longValue() == toHoursSince1970.longValue()) { maxTimeperiodWithinTheHour = LiveStatisticsUtil.getFifteensecondTimeperiodsSinceStartOfHour(maxTimeperiod * 15); } retList.addAll( createLivestatisticsFromMetricHour( metricHour, minTimeperiodWithinTheHour, maxTimeperiodWithinTheHour, index)); } return retList; }