commit 29109e2b8136234ba36b3c5f6b1e4418e5cc794a Author: Karsten Loesing karsten.loesing@gmx.net Date: Fri Aug 15 10:07:31 2014 +0200
Avoid passing around formatted timestamps if feasible.
Implements part of #12866. --- .../onionoo/docs/ClientsGraphHistory.java | 18 ++++++----- .../torproject/onionoo/docs/DetailsDocument.java | 34 +++++++++++--------- .../org/torproject/onionoo/docs/DetailsStatus.java | 17 +++++----- .../org/torproject/onionoo/docs/GraphHistory.java | 18 ++++++----- .../org/torproject/onionoo/server/NodeIndex.java | 11 ++++--- .../org/torproject/onionoo/server/NodeIndexer.java | 6 ++-- .../onionoo/updater/NodeDetailsStatusUpdater.java | 32 +++++++----------- .../onionoo/writer/BandwidthDocumentWriter.java | 4 +-- .../onionoo/writer/ClientsDocumentWriter.java | 4 +-- .../onionoo/writer/DetailsDocumentWriter.java | 14 +++----- .../onionoo/writer/UptimeDocumentWriter.java | 4 +-- .../onionoo/writer/WeightsDocumentWriter.java | 4 +-- .../onionoo/UptimeDocumentWriterTest.java | 8 ++--- 13 files changed, 85 insertions(+), 89 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/docs/ClientsGraphHistory.java b/src/main/java/org/torproject/onionoo/docs/ClientsGraphHistory.java index e1db663..00d11a0 100644 --- a/src/main/java/org/torproject/onionoo/docs/ClientsGraphHistory.java +++ b/src/main/java/org/torproject/onionoo/docs/ClientsGraphHistory.java @@ -6,22 +6,24 @@ import java.util.ArrayList; import java.util.List; import java.util.SortedMap;
+import org.torproject.onionoo.util.DateTimeHelper; + public class ClientsGraphHistory {
private String first; - public void setFirst(String first) { - this.first = first; + public void setFirst(long first) { + this.first = DateTimeHelper.format(first); } - public String getFirst() { - return this.first; + public long getFirst() { + return DateTimeHelper.parse(this.first); }
private String last; - public void setLast(String last) { - this.last = last; + public void setLast(long last) { + this.last = DateTimeHelper.format(last); } - public String getLast() { - return this.last; + public long getLast() { + return DateTimeHelper.parse(this.last); }
private Integer interval; diff --git a/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java b/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java index 142b591..9196153 100644 --- a/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java +++ b/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map;
import org.apache.commons.lang.StringEscapeUtils; +import org.torproject.onionoo.util.DateTimeHelper;
public class DetailsDocument extends Document {
@@ -75,28 +76,29 @@ public class DetailsDocument extends Document { }
private String last_seen; - public void setLastSeen(String lastSeen) { - this.last_seen = lastSeen; + public void setLastSeen(long lastSeen) { + this.last_seen = DateTimeHelper.format(lastSeen); } - public String getLastSeen() { - return this.last_seen; + public long getLastSeen() { + return DateTimeHelper.parse(this.last_seen); }
private String last_changed_address_or_port; public void setLastChangedAddressOrPort( - String lastChangedAddressOrPort) { - this.last_changed_address_or_port = lastChangedAddressOrPort; + long lastChangedAddressOrPort) { + this.last_changed_address_or_port = DateTimeHelper.format( + lastChangedAddressOrPort); } - public String getLastChangedAddressOrPort() { - return this.last_changed_address_or_port; + public long getLastChangedAddressOrPort() { + return DateTimeHelper.parse(this.last_changed_address_or_port); }
private String first_seen; - public void setFirstSeen(String firstSeen) { - this.first_seen = firstSeen; + public void setFirstSeen(long firstSeen) { + this.first_seen = DateTimeHelper.format(firstSeen); } - public String getFirstSeen() { - return this.first_seen; + public long getFirstSeen() { + return DateTimeHelper.parse(this.first_seen); }
private Boolean running; @@ -196,11 +198,11 @@ public class DetailsDocument extends Document { }
private String last_restarted; - public void setLastRestarted(String lastRestarted) { - this.last_restarted = lastRestarted; + public void setLastRestarted(long lastRestarted) { + this.last_restarted = DateTimeHelper.format(lastRestarted); } - public String getLastRestarted() { - return this.last_restarted; + public long getLastRestarted() { + return DateTimeHelper.parse(this.last_restarted); }
private Integer bandwidth_rate; diff --git a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java index a19b4b9..77ab269 100644 --- a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java +++ b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map;
import org.apache.commons.lang.StringEscapeUtils; +import org.torproject.onionoo.util.DateTimeHelper;
public class DetailsStatus extends Document {
@@ -27,19 +28,19 @@ public class DetailsStatus extends Document { }
private String desc_published; - public void setDescPublished(String descPublished) { - this.desc_published = descPublished; + public void setDescPublished(long descPublished) { + this.desc_published = DateTimeHelper.format(descPublished); } - public String getDescPublished() { - return this.desc_published; + public long getDescPublished() { + return DateTimeHelper.parse(this.desc_published); }
private String last_restarted; - public void setLastRestarted(String lastRestarted) { - this.last_restarted = lastRestarted; + public void setLastRestarted(long lastRestarted) { + this.last_restarted = DateTimeHelper.format(lastRestarted); } - public String getLastRestarted() { - return this.last_restarted; + public long getLastRestarted() { + return DateTimeHelper.parse(this.last_restarted); }
private Integer bandwidth_rate; diff --git a/src/main/java/org/torproject/onionoo/docs/GraphHistory.java b/src/main/java/org/torproject/onionoo/docs/GraphHistory.java index 19ace31..10941db 100644 --- a/src/main/java/org/torproject/onionoo/docs/GraphHistory.java +++ b/src/main/java/org/torproject/onionoo/docs/GraphHistory.java @@ -4,22 +4,24 @@ package org.torproject.onionoo.docs;
import java.util.List;
+import org.torproject.onionoo.util.DateTimeHelper; + public class GraphHistory {
private String first; - public void setFirst(String first) { - this.first = first; + public void setFirst(long first) { + this.first = DateTimeHelper.format(first); } - public String getFirst() { - return this.first; + public long getFirst() { + return DateTimeHelper.parse(this.first); }
private String last; - public void setLast(String last) { - this.last = last; + public void setLast(long last) { + this.last = DateTimeHelper.format(last); } - public String getLast() { - return this.last; + public long getLast() { + return DateTimeHelper.parse(this.last); }
private Integer interval; diff --git a/src/main/java/org/torproject/onionoo/server/NodeIndex.java b/src/main/java/org/torproject/onionoo/server/NodeIndex.java index 7b95d2e..f2859a5 100644 --- a/src/main/java/org/torproject/onionoo/server/NodeIndex.java +++ b/src/main/java/org/torproject/onionoo/server/NodeIndex.java @@ -6,20 +6,23 @@ import java.util.Set; import java.util.SortedMap;
import org.torproject.onionoo.docs.SummaryDocument; +import org.torproject.onionoo.util.DateTimeHelper;
class NodeIndex {
private String relaysPublishedString; - public void setRelaysPublishedString(String relaysPublishedString) { - this.relaysPublishedString = relaysPublishedString; + public void setRelaysPublishedMillis(long relaysPublishedMillis) { + this.relaysPublishedString = + DateTimeHelper.format(relaysPublishedMillis); } public String getRelaysPublishedString() { return relaysPublishedString; }
private String bridgesPublishedString; - public void setBridgesPublishedString(String bridgesPublishedString) { - this.bridgesPublishedString = bridgesPublishedString; + public void setBridgesPublishedMillis(long bridgesPublishedMillis) { + this.bridgesPublishedString = + DateTimeHelper.format(bridgesPublishedMillis); } public String getBridgesPublishedString() { return bridgesPublishedString; diff --git a/src/main/java/org/torproject/onionoo/server/NodeIndexer.java b/src/main/java/org/torproject/onionoo/server/NodeIndexer.java index f72d322..1cbd75e 100644 --- a/src/main/java/org/torproject/onionoo/server/NodeIndexer.java +++ b/src/main/java/org/torproject/onionoo/server/NodeIndexer.java @@ -286,10 +286,8 @@ public class NodeIndexer implements ServletContextListener, Runnable { newNodeIndex.setRelaysByLastSeenDays(newRelaysByLastSeenDays); newNodeIndex.setBridgesByFirstSeenDays(newBridgesByFirstSeenDays); newNodeIndex.setBridgesByLastSeenDays(newBridgesByLastSeenDays); - newNodeIndex.setRelaysPublishedString(DateTimeHelper.format( - relaysLastValidAfterMillis)); - newNodeIndex.setBridgesPublishedString(DateTimeHelper.format( - bridgesLastPublishedMillis)); + newNodeIndex.setRelaysPublishedMillis(relaysLastValidAfterMillis); + newNodeIndex.setBridgesPublishedMillis(bridgesLastPublishedMillis); synchronized (this) { this.lastIndexed = updateStatusMillis; this.latestNodeIndex = newNodeIndex; diff --git a/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java b/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java index 979c49b..827c890 100644 --- a/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java +++ b/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java @@ -105,25 +105,21 @@ public class NodeDetailsStatusUpdater implements DescriptorListener, String fingerprint = descriptor.getFingerprint(); DetailsStatus detailsStatus = this.documentStore.retrieve( DetailsStatus.class, true, fingerprint); - String publishedDateTime = - DateTimeHelper.format(descriptor.getPublishedMillis()); if (detailsStatus == null) { detailsStatus = new DetailsStatus(); - } else if (detailsStatus.getDescPublished() != null && - publishedDateTime.compareTo( - detailsStatus.getDescPublished()) <= 0) { + } else if (descriptor.getPublishedMillis() > + detailsStatus.getDescPublished()) { return; } - String lastRestartedString = DateTimeHelper.format( - descriptor.getPublishedMillis() - descriptor.getUptime() - * DateTimeHelper.ONE_SECOND); + long lastRestartedMillis = descriptor.getPublishedMillis() + - descriptor.getUptime() * DateTimeHelper.ONE_SECOND; int bandwidthRate = descriptor.getBandwidthRate(); int bandwidthBurst = descriptor.getBandwidthBurst(); int observedBandwidth = descriptor.getBandwidthObserved(); int advertisedBandwidth = Math.min(bandwidthRate, Math.min(bandwidthBurst, observedBandwidth)); - detailsStatus.setDescPublished(publishedDateTime); - detailsStatus.setLastRestarted(lastRestartedString); + detailsStatus.setDescPublished(descriptor.getPublishedMillis()); + detailsStatus.setLastRestarted(lastRestartedMillis); detailsStatus.setBandwidthRate(bandwidthRate); detailsStatus.setBandwidthBurst(bandwidthBurst); detailsStatus.setObservedBandwidth(observedBandwidth); @@ -227,23 +223,19 @@ public class NodeDetailsStatusUpdater implements DescriptorListener, String fingerprint = descriptor.getFingerprint(); DetailsStatus detailsStatus = this.documentStore.retrieve( DetailsStatus.class, true, fingerprint); - String publishedDateTime = - DateTimeHelper.format(descriptor.getPublishedMillis()); if (detailsStatus == null) { detailsStatus = new DetailsStatus(); - } else if (detailsStatus.getDescPublished() != null && - publishedDateTime.compareTo( - detailsStatus.getDescPublished()) <= 0) { + } else if (detailsStatus.getDescPublished() > + descriptor.getPublishedMillis()) { return; } - String lastRestartedString = DateTimeHelper.format( - descriptor.getPublishedMillis() - descriptor.getUptime() - * DateTimeHelper.ONE_SECOND); + long lastRestartedMillis = descriptor.getPublishedMillis() + - descriptor.getUptime() * DateTimeHelper.ONE_SECOND; int advertisedBandwidth = Math.min(descriptor.getBandwidthRate(), Math.min(descriptor.getBandwidthBurst(), descriptor.getBandwidthObserved())); - detailsStatus.setDescPublished(publishedDateTime); - detailsStatus.setLastRestarted(lastRestartedString); + detailsStatus.setDescPublished(descriptor.getPublishedMillis()); + detailsStatus.setLastRestarted(lastRestartedMillis); detailsStatus.setAdvertisedBandwidth(advertisedBandwidth); detailsStatus.setPlatform(descriptor.getPlatform()); this.documentStore.store(detailsStatus, fingerprint); diff --git a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java index c5ae22e..5ce467c 100644 --- a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java +++ b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java @@ -168,8 +168,8 @@ public class BandwidthDocumentWriter implements FingerprintListener, double factor = ((double) maxValue) / 999.0; int count = lastNonNullIndex - firstNonNullIndex + 1; GraphHistory graphHistory = new GraphHistory(); - graphHistory.setFirst(DateTimeHelper.format(firstDataPointMillis)); - graphHistory.setLast(DateTimeHelper.format(lastDataPointMillis)); + graphHistory.setFirst(firstDataPointMillis); + graphHistory.setLast(lastDataPointMillis); graphHistory.setInterval((int) (dataPointInterval / DateTimeHelper.ONE_SECOND)); graphHistory.setFactor(factor); diff --git a/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java index ae64f86..6684db7 100644 --- a/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java +++ b/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java @@ -227,8 +227,8 @@ public class ClientsDocumentWriter implements FingerprintListener, double factor = ((double) maxValue) / 999.0; int count = lastNonNullIndex - firstNonNullIndex + 1; ClientsGraphHistory graphHistory = new ClientsGraphHistory(); - graphHistory.setFirst(DateTimeHelper.format(firstDataPointMillis)); - graphHistory.setLast(DateTimeHelper.format(lastDataPointMillis)); + graphHistory.setFirst(firstDataPointMillis); + graphHistory.setLast(lastDataPointMillis); graphHistory.setInterval((int) (dataPointInterval / DateTimeHelper.ONE_SECOND)); graphHistory.setFactor(factor); diff --git a/src/main/java/org/torproject/onionoo/writer/DetailsDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/DetailsDocumentWriter.java index 8e2cb9f..8d69079 100644 --- a/src/main/java/org/torproject/onionoo/writer/DetailsDocumentWriter.java +++ b/src/main/java/org/torproject/onionoo/writer/DetailsDocumentWriter.java @@ -92,12 +92,10 @@ public class DetailsDocumentWriter implements FingerprintListener, detailsDocument.setDirAddress(entry.getAddress() + ":" + entry.getDirPort()); } - detailsDocument.setLastSeen(DateTimeHelper.format( - entry.getLastSeenMillis())); - detailsDocument.setFirstSeen(DateTimeHelper.format( - entry.getFirstSeenMillis())); + detailsDocument.setLastSeen(entry.getLastSeenMillis()); + detailsDocument.setFirstSeen(entry.getFirstSeenMillis()); detailsDocument.setLastChangedAddressOrPort( - DateTimeHelper.format(entry.getLastChangedOrAddress())); + entry.getLastChangedOrAddress()); detailsDocument.setRunning(entry.getRunning()); if (!entry.getRelayFlags().isEmpty()) { detailsDocument.setFlags(new ArrayList<String>( @@ -202,10 +200,8 @@ public class DetailsDocumentWriter implements FingerprintListener, orAddresses.add(orAddress.toLowerCase()); } detailsDocument.setOrAddresses(orAddresses); - detailsDocument.setLastSeen(DateTimeHelper.format( - entry.getLastSeenMillis())); - detailsDocument.setFirstSeen(DateTimeHelper.format( - entry.getFirstSeenMillis())); + detailsDocument.setLastSeen(entry.getLastSeenMillis()); + detailsDocument.setFirstSeen(entry.getFirstSeenMillis()); detailsDocument.setRunning(entry.getRunning()); detailsDocument.setFlags(new ArrayList<String>( entry.getRelayFlags())); diff --git a/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java index f58ec93..71f4d9a 100644 --- a/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java +++ b/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java @@ -267,8 +267,8 @@ public class UptimeDocumentWriter implements FingerprintListener, + (lastNonNullIndex - firstNonNullIndex) * dataPointInterval; int count = lastNonNullIndex - firstNonNullIndex + 1; GraphHistory graphHistory = new GraphHistory(); - graphHistory.setFirst(DateTimeHelper.format(firstDataPointMillis)); - graphHistory.setLast(DateTimeHelper.format(lastDataPointMillis)); + graphHistory.setFirst(firstDataPointMillis); + graphHistory.setLast(lastDataPointMillis); graphHistory.setInterval((int) (dataPointInterval / DateTimeHelper.ONE_SECOND)); graphHistory.setFactor(1.0 / 999.0); diff --git a/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java index e0601c1..cfb410c 100644 --- a/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java +++ b/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java @@ -199,8 +199,8 @@ public class WeightsDocumentWriter implements FingerprintListener, double factor = ((double) maxValue) / 999.0; int count = lastNonNullIndex - firstNonNullIndex + 1; GraphHistory graphHistory = new GraphHistory(); - graphHistory.setFirst(DateTimeHelper.format(firstDataPointMillis)); - graphHistory.setLast(DateTimeHelper.format(lastDataPointMillis)); + graphHistory.setFirst(firstDataPointMillis); + graphHistory.setLast(lastDataPointMillis); graphHistory.setInterval((int) (dataPointInterval / DateTimeHelper.ONE_SECOND)); graphHistory.setFactor(factor); diff --git a/src/test/java/org/torproject/onionoo/UptimeDocumentWriterTest.java b/src/test/java/org/torproject/onionoo/UptimeDocumentWriterTest.java index f430c0b..afae0e8 100644 --- a/src/test/java/org/torproject/onionoo/UptimeDocumentWriterTest.java +++ b/src/test/java/org/torproject/onionoo/UptimeDocumentWriterTest.java @@ -97,10 +97,10 @@ public class UptimeDocumentWriterTest { assertTrue("Should contain a graph for " + graphName + ".", document.getUptime().containsKey(graphName)); GraphHistory history = document.getUptime().get(graphName); - assertEquals("First data point should be " + first + ".", first, - history.getFirst()); - assertEquals("Last data point should be " + last + ".", last, - history.getLast()); + assertEquals("First data point should be " + first + ".", + DateTimeHelper.parse(first), history.getFirst()); + assertEquals("Last data point should be " + last + ".", + DateTimeHelper.parse(last), history.getLast()); assertEquals("Interval should be " + interval + " seconds.", interval, (int) history.getInterval()); assertEquals("Factor should be 1.0 / 999.0.", 1.0 / 999.0,