[tor-commits] [onionoo/master] Make ClientsStatus smarter about itself.
karsten at torproject.org
karsten at torproject.org
Sun Aug 17 07:06:46 UTC 2014
commit 934eff6921bb5e166a551276fb1d698b66507b16
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Fri Aug 15 10:54:24 2014 +0200
Make ClientsStatus smarter about itself.
Removes another dependency on DateTimeHelper, which gets us closer to
implementing #12866.
---
.../org/torproject/onionoo/docs/ClientsStatus.java | 65 +++++++++++++++++++
.../onionoo/updater/ClientsStatusUpdater.java | 66 ++------------------
2 files changed, 70 insertions(+), 61 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java b/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java
index 2bd2168..2e7e2ee 100644
--- a/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java
@@ -6,8 +6,19 @@ import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
+import org.torproject.onionoo.util.DateTimeHelper;
+import org.torproject.onionoo.util.TimeFactory;
+
public class ClientsStatus extends Document {
+ private transient boolean isDirty = false;
+ public boolean isDirty() {
+ return this.isDirty;
+ }
+ public void clearDirty() {
+ this.isDirty = false;
+ }
+
private SortedSet<ClientsHistory> history =
new TreeSet<ClientsHistory>();
public void setHistory(SortedSet<ClientsHistory> history) {
@@ -32,6 +43,60 @@ public class ClientsStatus extends Document {
s.close();
}
+ public void addToHistory(SortedSet<ClientsHistory> newIntervals) {
+ for (ClientsHistory interval : newIntervals) {
+ if ((this.history.headSet(interval).isEmpty() ||
+ this.history.headSet(interval).last().getEndMillis() <=
+ interval.getStartMillis()) &&
+ (this.history.tailSet(interval).isEmpty() ||
+ this.history.tailSet(interval).first().getStartMillis() >=
+ interval.getEndMillis())) {
+ this.history.add(interval);
+ this.isDirty = true;
+ }
+ }
+ }
+
+ public void compressHistory() {
+ SortedSet<ClientsHistory> uncompressedHistory =
+ new TreeSet<ClientsHistory>(this.history);
+ history.clear();
+ ClientsHistory lastResponses = null;
+ String lastMonthString = "1970-01";
+ long now = TimeFactory.getTime().currentTimeMillis();
+ for (ClientsHistory responses : uncompressedHistory) {
+ long intervalLengthMillis;
+ if (now - responses.getEndMillis() <=
+ DateTimeHelper.ROUGHLY_THREE_MONTHS) {
+ intervalLengthMillis = DateTimeHelper.ONE_DAY;
+ } else if (now - responses.getEndMillis() <=
+ DateTimeHelper.ROUGHLY_ONE_YEAR) {
+ intervalLengthMillis = DateTimeHelper.TWO_DAYS;
+ } else {
+ intervalLengthMillis = DateTimeHelper.TEN_DAYS;
+ }
+ String monthString = DateTimeHelper.format(
+ responses.getStartMillis(),
+ DateTimeHelper.ISO_YEARMONTH_FORMAT);
+ if (lastResponses != null &&
+ lastResponses.getEndMillis() == responses.getStartMillis() &&
+ ((lastResponses.getEndMillis() - 1L) / intervalLengthMillis) ==
+ ((responses.getEndMillis() - 1L) / intervalLengthMillis) &&
+ lastMonthString.equals(monthString)) {
+ lastResponses.addResponses(responses);
+ } else {
+ if (lastResponses != null) {
+ this.history.add(lastResponses);
+ }
+ lastResponses = responses;
+ }
+ lastMonthString = monthString;
+ }
+ if (lastResponses != null) {
+ this.history.add(lastResponses);
+ }
+ }
+
public String toDocumentString() {
StringBuilder sb = new StringBuilder();
for (ClientsHistory interval : this.history) {
diff --git a/src/main/java/org/torproject/onionoo/updater/ClientsStatusUpdater.java b/src/main/java/org/torproject/onionoo/updater/ClientsStatusUpdater.java
index 72d5142..a8797ba 100644
--- a/src/main/java/org/torproject/onionoo/updater/ClientsStatusUpdater.java
+++ b/src/main/java/org/torproject/onionoo/updater/ClientsStatusUpdater.java
@@ -16,7 +16,6 @@ import org.torproject.onionoo.docs.DocumentStore;
import org.torproject.onionoo.docs.DocumentStoreFactory;
import org.torproject.onionoo.util.DateTimeHelper;
import org.torproject.onionoo.util.Logger;
-import org.torproject.onionoo.util.TimeFactory;
/*
* Example extra-info descriptor used as input:
@@ -43,12 +42,9 @@ public class ClientsStatusUpdater implements DescriptorListener,
private DocumentStore documentStore;
- private long now;
-
public ClientsStatusUpdater() {
this.descriptorSource = DescriptorSourceFactory.getDescriptorSource();
this.documentStore = DocumentStoreFactory.getDocumentStore();
- this.now = TimeFactory.getTime().currentTimeMillis();
this.registerDescriptorListeners();
}
@@ -152,67 +148,15 @@ public class ClientsStatusUpdater implements DescriptorListener,
if (clientsStatus == null) {
clientsStatus = new ClientsStatus();
}
- this.addToHistory(clientsStatus, e.getValue());
- this.compressHistory(clientsStatus);
- this.documentStore.store(clientsStatus, hashedFingerprint);
- }
- }
-
- private void addToHistory(ClientsStatus clientsStatus,
- SortedSet<ClientsHistory> newIntervals) {
- SortedSet<ClientsHistory> history = clientsStatus.getHistory();
- for (ClientsHistory interval : newIntervals) {
- if ((history.headSet(interval).isEmpty() ||
- history.headSet(interval).last().getEndMillis() <=
- interval.getStartMillis()) &&
- (history.tailSet(interval).isEmpty() ||
- history.tailSet(interval).first().getStartMillis() >=
- interval.getEndMillis())) {
- history.add(interval);
+ clientsStatus.addToHistory(e.getValue());
+ if (clientsStatus.isDirty()) {
+ clientsStatus.compressHistory();
+ this.documentStore.store(clientsStatus, hashedFingerprint);
+ clientsStatus.clearDirty();
}
}
}
- private void compressHistory(ClientsStatus clientsStatus) {
- SortedSet<ClientsHistory> history = clientsStatus.getHistory();
- SortedSet<ClientsHistory> compressedHistory =
- new TreeSet<ClientsHistory>();
- ClientsHistory lastResponses = null;
- String lastMonthString = "1970-01";
- for (ClientsHistory responses : history) {
- long intervalLengthMillis;
- if (this.now - responses.getEndMillis() <=
- DateTimeHelper.ROUGHLY_THREE_MONTHS) {
- intervalLengthMillis = DateTimeHelper.ONE_DAY;
- } else if (this.now - responses.getEndMillis() <=
- DateTimeHelper.ROUGHLY_ONE_YEAR) {
- intervalLengthMillis = DateTimeHelper.TWO_DAYS;
- } else {
- intervalLengthMillis = DateTimeHelper.TEN_DAYS;
- }
- String monthString = DateTimeHelper.format(
- responses.getStartMillis(),
- DateTimeHelper.ISO_YEARMONTH_FORMAT);
- if (lastResponses != null &&
- lastResponses.getEndMillis() == responses.getStartMillis() &&
- ((lastResponses.getEndMillis() - 1L) / intervalLengthMillis) ==
- ((responses.getEndMillis() - 1L) / intervalLengthMillis) &&
- lastMonthString.equals(monthString)) {
- lastResponses.addResponses(responses);
- } else {
- if (lastResponses != null) {
- compressedHistory.add(lastResponses);
- }
- lastResponses = responses;
- }
- lastMonthString = monthString;
- }
- if (lastResponses != null) {
- compressedHistory.add(lastResponses);
- }
- clientsStatus.setHistory(compressedHistory);
- }
-
public String getStatsString() {
int newIntervals = 0;
for (SortedSet<ClientsHistory> hist : this.newResponses.values()) {
More information about the tor-commits
mailing list