@Override public void initialize() { // Increment the session counter int sessions = ApplicationSettings.SESSIONS.getValue(); ApplicationSettings.SESSIONS.setValue(sessions + 1); // Record the time between sessions long lastShutdown = ApplicationSettings.LAST_SHUTDOWN_TIME.getValue(); long downtime; if (lastShutdown == 0) downtime = DEFAULT_DOWNTIME; else downtime = Math.max(0, (clock.now() - lastShutdown) / 1000); // If the number of downtimes is greater that the number of uptimes, // the last session must have ended without recording the uptime or // shutdown time. To avoid double-counting the downtime we should // overwrite the last downtime instead of appending. String[] downtimes = ApplicationSettings.DOWNTIME_HISTORY.get(); String[] uptimes = ApplicationSettings.UPTIME_HISTORY.get(); if (downtimes.length > uptimes.length) downtimes = updateHistory(downtimes, Long.toString(downtime)); else downtimes = appendToHistory(downtimes, Long.toString(downtime)); ApplicationSettings.DOWNTIME_HISTORY.set(downtimes); // Measure the time between refreshes lastUpdateTime = clock.now(); }
/** Refreshes the uptime statistics. Package access for testing. */ void refreshStats() { long now = clock.now(); long elapsed = (now - lastUpdateTime) / 1000; if (elapsed > 0) { currentUptime += elapsed; updateUptimeHistory(currentUptime); long totalUptime = ApplicationSettings.TOTAL_UPTIME.getValue() + elapsed; ApplicationSettings.TOTAL_UPTIME.setValue(totalUptime); int sessions = ApplicationSettings.SESSIONS.getValue(); if (sessions > 0) { ApplicationSettings.AVERAGE_UPTIME.setValue(totalUptime / sessions); } ApplicationSettings.FRACTIONAL_UPTIME.setValue(stats.calculateFractionalUptime()); ApplicationSettings.LAST_SHUTDOWN_TIME.setValue(now); // Pessimistic } lastUpdateTime = now; }