[tor-commits] [onionoo/master] Make UptimeStatus smarter about itself.

karsten at torproject.org karsten at torproject.org
Mon Apr 14 13:29:25 UTC 2014


commit 87514a284d57f02091aab8d27ddb5731ffa237e1
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Tue Apr 8 23:39:19 2014 +0200

    Make UptimeStatus smarter about itself.
---
 src/org/torproject/onionoo/UptimeStatus.java       |   46 +++++++++++++
 .../torproject/onionoo/UptimeStatusUpdater.java    |   68 ++------------------
 2 files changed, 51 insertions(+), 63 deletions(-)

diff --git a/src/org/torproject/onionoo/UptimeStatus.java b/src/org/torproject/onionoo/UptimeStatus.java
index 2e688b8..61b4fe0 100644
--- a/src/org/torproject/onionoo/UptimeStatus.java
+++ b/src/org/torproject/onionoo/UptimeStatus.java
@@ -118,6 +118,52 @@ class UptimeStatus extends Document {
     s.close();
   }
 
+  public void addToHistory(boolean relay, SortedSet<Long> newIntervals) {
+    for (long startMillis : newIntervals) {
+      UptimeHistory interval = new UptimeHistory(relay, startMillis, 1);
+      if (!this.history.headSet(interval).isEmpty()) {
+        UptimeHistory prev = this.history.headSet(interval).last();
+        if (prev.isRelay() == interval.isRelay() &&
+            prev.getStartMillis() + DateTimeHelper.ONE_HOUR
+            * prev.getUptimeHours() > interval.getStartMillis()) {
+          continue;
+        }
+      }
+      if (!this.history.tailSet(interval).isEmpty()) {
+        UptimeHistory next = this.history.tailSet(interval).first();
+        if (next.isRelay() == interval.isRelay() &&
+            next.getStartMillis() < interval.getStartMillis()
+            + DateTimeHelper.ONE_HOUR) {
+          continue;
+        }
+      }
+      this.history.add(interval);
+    }
+  }
+
+  public void compressHistory() {
+    SortedSet<UptimeHistory> compressedHistory =
+        new TreeSet<UptimeHistory>();
+    UptimeHistory lastInterval = null;
+    for (UptimeHistory interval : history) {
+      if (lastInterval != null &&
+          lastInterval.getStartMillis() + DateTimeHelper.ONE_HOUR
+          * lastInterval.getUptimeHours() == interval.getStartMillis() &&
+          lastInterval.isRelay() == interval.isRelay()) {
+        lastInterval.addUptime(interval);
+      } else {
+        if (lastInterval != null) {
+          compressedHistory.add(lastInterval);
+        }
+        lastInterval = interval;
+      }
+    }
+    if (lastInterval != null) {
+      compressedHistory.add(lastInterval);
+    }
+    this.history = compressedHistory;
+  }
+
   public String toDocumentString() {
     StringBuilder sb = new StringBuilder();
     for (UptimeHistory interval : this.history) {
diff --git a/src/org/torproject/onionoo/UptimeStatusUpdater.java b/src/org/torproject/onionoo/UptimeStatusUpdater.java
index f524ea3..97a6be1 100644
--- a/src/org/torproject/onionoo/UptimeStatusUpdater.java
+++ b/src/org/torproject/onionoo/UptimeStatusUpdater.java
@@ -109,72 +109,14 @@ public class UptimeStatusUpdater implements DescriptorListener,
 
   private void updateStatus(boolean relay, String fingerprint,
       SortedSet<Long> newUptimeHours) {
-    UptimeStatus uptimeStatus = this.readHistory(fingerprint);
-    if (uptimeStatus == null) {
-      uptimeStatus = new UptimeStatus();
-    }
-    this.addToHistory(uptimeStatus, relay, newUptimeHours);
-    this.compressHistory(uptimeStatus);
-    this.writeHistory(fingerprint, uptimeStatus);
-  }
-
-  private UptimeStatus readHistory(String fingerprint) {
-    return fingerprint == null ?
+    UptimeStatus uptimeStatus = fingerprint == null ?
         documentStore.retrieve(UptimeStatus.class, true) :
         documentStore.retrieve(UptimeStatus.class, true, fingerprint);
-  }
-
-  private void addToHistory(UptimeStatus uptimeStatus, boolean relay,
-      SortedSet<Long> newIntervals) {
-    SortedSet<UptimeHistory> history = uptimeStatus.getHistory();
-    for (long startMillis : newIntervals) {
-      UptimeHistory interval = new UptimeHistory(relay, startMillis, 1);
-      if (!history.headSet(interval).isEmpty()) {
-        UptimeHistory prev = history.headSet(interval).last();
-        if (prev.isRelay() == interval.isRelay() &&
-            prev.getStartMillis() + DateTimeHelper.ONE_HOUR
-            * prev.getUptimeHours() > interval.getStartMillis()) {
-          continue;
-        }
-      }
-      if (!history.tailSet(interval).isEmpty()) {
-        UptimeHistory next = history.tailSet(interval).first();
-        if (next.isRelay() == interval.isRelay() &&
-            next.getStartMillis() < interval.getStartMillis()
-            + DateTimeHelper.ONE_HOUR) {
-          continue;
-        }
-      }
-      history.add(interval);
-    }
-  }
-
-  private void compressHistory(UptimeStatus uptimeStatus) {
-    SortedSet<UptimeHistory> history = uptimeStatus.getHistory();
-    SortedSet<UptimeHistory> compressedHistory =
-        new TreeSet<UptimeHistory>();
-    UptimeHistory lastInterval = null;
-    for (UptimeHistory interval : history) {
-      if (lastInterval != null &&
-          lastInterval.getStartMillis() + DateTimeHelper.ONE_HOUR
-          * lastInterval.getUptimeHours() == interval.getStartMillis() &&
-          lastInterval.isRelay() == interval.isRelay()) {
-        lastInterval.addUptime(interval);
-      } else {
-        if (lastInterval != null) {
-          compressedHistory.add(lastInterval);
-        }
-        lastInterval = interval;
-      }
-    }
-    if (lastInterval != null) {
-      compressedHistory.add(lastInterval);
+    if (uptimeStatus == null) {
+      uptimeStatus = new UptimeStatus();
     }
-    uptimeStatus.setHistory(compressedHistory);
-  }
-
-  private void writeHistory(String fingerprint,
-      UptimeStatus uptimeStatus) {
+    uptimeStatus.addToHistory(relay, newUptimeHours);
+    uptimeStatus.compressHistory();
     if (fingerprint == null) {
       this.documentStore.store(uptimeStatus);
     } else {





More information about the tor-commits mailing list