commit f4492310fa24fd1be115e07acf0b392a829046e2 Author: Karsten Loesing karsten.loesing@gmx.net Date: Tue Apr 8 23:22:35 2014 +0200
Move date and time constants to single place.
Suggested by SonarQube to avoid magic numbers. --- .../onionoo/BandwidthDocumentWriter.java | 33 +++++++++-------- .../torproject/onionoo/BandwidthStatusUpdater.java | 28 ++++++++------ .../torproject/onionoo/ClientsDocumentWriter.java | 30 +++++++-------- .../torproject/onionoo/ClientsStatusUpdater.java | 24 ++++++------ src/org/torproject/onionoo/DateTimeHelper.java | 19 ++++++++++ src/org/torproject/onionoo/DocumentStore.java | 2 +- src/org/torproject/onionoo/Logger.java | 4 +- .../onionoo/NodeDetailsStatusUpdater.java | 6 ++- src/org/torproject/onionoo/ResponseBuilder.java | 2 +- .../onionoo/ReverseDomainNameResolver.java | 11 +++--- .../torproject/onionoo/UptimeDocumentWriter.java | 39 ++++++++++---------- .../torproject/onionoo/UptimeStatusUpdater.java | 15 ++++---- .../torproject/onionoo/WeightsDocumentWriter.java | 23 ++++++------ .../torproject/onionoo/WeightsStatusUpdater.java | 27 ++++++++------ 14 files changed, 146 insertions(+), 117 deletions(-)
diff --git a/src/org/torproject/onionoo/BandwidthDocumentWriter.java b/src/org/torproject/onionoo/BandwidthDocumentWriter.java index 68912c7..0c7308e 100644 --- a/src/org/torproject/onionoo/BandwidthDocumentWriter.java +++ b/src/org/torproject/onionoo/BandwidthDocumentWriter.java @@ -77,20 +77,20 @@ public class BandwidthDocumentWriter implements FingerprintListener, "5_years" };
private long[] graphIntervals = new long[] { - 72L * 60L * 60L * 1000L, - 7L * 24L * 60L * 60L * 1000L, - 31L * 24L * 60L * 60L * 1000L, - 92L * 24L * 60L * 60L * 1000L, - 366L * 24L * 60L * 60L * 1000L, - 5L * 366L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.THREE_DAYS, + DateTimeHelper.ONE_WEEK, + DateTimeHelper.ROUGHLY_ONE_MONTH, + DateTimeHelper.ROUGHLY_THREE_MONTHS, + DateTimeHelper.ROUGHLY_ONE_YEAR, + DateTimeHelper.ROUGHLY_FIVE_YEARS };
private long[] dataPointIntervals = new long[] { - 15L * 60L * 1000L, - 60L * 60L * 1000L, - 4L * 60L * 60L * 1000L, - 12L * 60L * 60L * 1000L, - 2L * 24L * 60L * 60L * 1000L, - 10L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.FIFTEEN_MINUTES, + DateTimeHelper.ONE_HOUR, + DateTimeHelper.FOUR_HOURS, + DateTimeHelper.TWELVE_HOURS, + DateTimeHelper.TWO_DAYS, + DateTimeHelper.TEN_DAYS };
private String formatHistoryString(SortedMap<Long, long[]> history) { StringBuilder sb = new StringBuilder(); @@ -110,7 +110,8 @@ public class BandwidthDocumentWriter implements FingerprintListener, while ((intervalStartMillis / dataPointInterval) != (endMillis / dataPointInterval)) { dataPoints.add(totalMillis * 5L < dataPointInterval - ? -1L : (totalBandwidth * 1000L) / totalMillis); + ? -1L : (totalBandwidth * DateTimeHelper.ONE_SECOND) + / totalMillis); totalBandwidth = 0L; totalMillis = 0L; intervalStartMillis += dataPointInterval; @@ -119,7 +120,8 @@ public class BandwidthDocumentWriter implements FingerprintListener, totalMillis += (endMillis - startMillis); } dataPoints.add(totalMillis * 5L < dataPointInterval - ? -1L : (totalBandwidth * 1000L) / totalMillis); + ? -1L : (totalBandwidth * DateTimeHelper.ONE_SECOND) + / totalMillis); long maxValue = 1L; int firstNonNullIndex = -1, lastNonNullIndex = -1; for (int j = 0; j < dataPoints.size(); j++) { @@ -157,7 +159,8 @@ public class BandwidthDocumentWriter implements FingerprintListener, + DateTimeHelper.format(firstDataPointMillis) + ""," + ""last":"" + DateTimeHelper.format(lastDataPointMillis) + ""," - +""interval":" + String.valueOf(dataPointInterval / 1000L) + + ""interval":" + String.valueOf(dataPointInterval + / DateTimeHelper.ONE_SECOND) + ","factor":" + String.format(Locale.US, "%.3f", factor) + ","count":" + String.valueOf(count) + ","values":["); int written = 0, previousNonNullIndex = -2; diff --git a/src/org/torproject/onionoo/BandwidthStatusUpdater.java b/src/org/torproject/onionoo/BandwidthStatusUpdater.java index cce56bc..6aff25d 100644 --- a/src/org/torproject/onionoo/BandwidthStatusUpdater.java +++ b/src/org/torproject/onionoo/BandwidthStatusUpdater.java @@ -74,7 +74,8 @@ public class BandwidthStatusUpdater implements DescriptorListener, + "'. Skipping."); return; } - long intervalMillis = Long.parseLong(parts[3].substring(1)) * 1000L; + long intervalMillis = Long.parseLong(parts[3].substring(1)) + * DateTimeHelper.ONE_SECOND; String[] values = parts[5].split(","); for (int i = values.length - 1; i >= 0; i--) { long bandwidthValue = Long.parseLong(values[i]); @@ -96,18 +97,21 @@ public class BandwidthStatusUpdater implements DescriptorListener, for (long[] v : uncompressedHistory.values()) { long startMillis = v[0], endMillis = v[1], bandwidth = v[2]; long intervalLengthMillis; - if (this.now - endMillis <= 72L * 60L * 60L * 1000L) { - intervalLengthMillis = 15L * 60L * 1000L; - } else if (this.now - endMillis <= 7L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 60L * 60L * 1000L; - } else if (this.now - endMillis <= 31L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 4L * 60L * 60L * 1000L; - } else if (this.now - endMillis <= 92L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 12L * 60L * 60L * 1000L; - } else if (this.now - endMillis <= 366L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 2L * 24L * 60L * 60L * 1000L; + if (this.now - endMillis <= DateTimeHelper.THREE_DAYS) { + intervalLengthMillis = DateTimeHelper.FIFTEEN_MINUTES; + } else if (this.now - endMillis <= DateTimeHelper.ONE_WEEK) { + intervalLengthMillis = DateTimeHelper.ONE_HOUR; + } else if (this.now - endMillis <= + DateTimeHelper.ROUGHLY_ONE_MONTH) { + intervalLengthMillis = DateTimeHelper.FOUR_HOURS; + } else if (this.now - endMillis <= + DateTimeHelper.ROUGHLY_THREE_MONTHS) { + intervalLengthMillis = DateTimeHelper.TWELVE_HOURS; + } else if (this.now - endMillis <= + DateTimeHelper.ROUGHLY_ONE_YEAR) { + intervalLengthMillis = DateTimeHelper.TWO_DAYS; } else { - intervalLengthMillis = 10L * 24L * 60L * 60L * 1000L; + intervalLengthMillis = DateTimeHelper.TEN_DAYS; } String monthString = DateTimeHelper.format(startMillis, DateTimeHelper.ISO_YEARMONTH_FORMAT); diff --git a/src/org/torproject/onionoo/ClientsDocumentWriter.java b/src/org/torproject/onionoo/ClientsDocumentWriter.java index fe400d2..d9ff02d 100644 --- a/src/org/torproject/onionoo/ClientsDocumentWriter.java +++ b/src/org/torproject/onionoo/ClientsDocumentWriter.java @@ -92,18 +92,18 @@ public class ClientsDocumentWriter implements FingerprintListener, "5_years" };
private long[] graphIntervals = new long[] { - 7L * 24L * 60L * 60L * 1000L, - 31L * 24L * 60L * 60L * 1000L, - 92L * 24L * 60L * 60L * 1000L, - 366L * 24L * 60L * 60L * 1000L, - 5L * 366L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.ONE_WEEK, + DateTimeHelper.ROUGHLY_ONE_MONTH, + DateTimeHelper.ROUGHLY_THREE_MONTHS, + DateTimeHelper.ROUGHLY_ONE_YEAR, + DateTimeHelper.ROUGHLY_FIVE_YEARS };
private long[] dataPointIntervals = new long[] { - 24L * 60L * 60L * 1000L, - 24L * 60L * 60L * 1000L, - 24L * 60L * 60L * 1000L, - 2L * 24L * 60L * 60L * 1000L, - 10L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.ONE_DAY, + DateTimeHelper.ONE_DAY, + DateTimeHelper.ONE_DAY, + DateTimeHelper.TWO_DAYS, + DateTimeHelper.TEN_DAYS };
private String formatHistoryString(String hashedFingerprint, SortedSet<ClientsHistory> history) { @@ -124,9 +124,6 @@ public class ClientsDocumentWriter implements FingerprintListener, return sb.toString(); }
- private static final long ONE_HOUR_MILLIS = 60L * 60L * 1000L, - ONE_DAY_MILLIS = 24L * ONE_HOUR_MILLIS; - private String formatTimeline(int graphIntervalIndex, SortedSet<ClientsHistory> history) { String graphName = this.graphNames[graphIntervalIndex]; @@ -149,7 +146,7 @@ public class ClientsDocumentWriter implements FingerprintListener, while ((intervalStartMillis / dataPointInterval) != (hist.endMillis / dataPointInterval)) { dataPoints.add(millis * 2L < dataPointInterval - ? -1.0 : responses * ((double) ONE_DAY_MILLIS) + ? -1.0 : responses * ((double) DateTimeHelper.ONE_DAY) / (((double) millis) * 10.0)); responses = 0.0; millis = 0L; @@ -184,7 +181,7 @@ public class ClientsDocumentWriter implements FingerprintListener, millis += (hist.endMillis - hist.startMillis); } dataPoints.add(millis * 2L < dataPointInterval - ? -1.0 : responses * ((double) ONE_DAY_MILLIS) + ? -1.0 : responses * ((double) DateTimeHelper.ONE_DAY) / (((double) millis) * 10.0)); double maxValue = 0.0; int firstNonNullIndex = -1, lastNonNullIndex = -1; @@ -222,7 +219,8 @@ public class ClientsDocumentWriter implements FingerprintListener, sb.append(""" + graphName + "":{" + ""first":"" + DateTimeHelper.format(firstDataPointMillis) + "","last":"" + DateTimeHelper.format(lastDataPointMillis) - + "","interval":" + String.valueOf(dataPointInterval / 1000L) + + "","interval":" + String.valueOf(dataPointInterval + / DateTimeHelper.ONE_SECOND) + ","factor":" + String.format(Locale.US, "%.9f", factor) + ","count":" + String.valueOf(count) + ","values":["); int dataPointsWritten = 0, previousNonNullIndex = -2; diff --git a/src/org/torproject/onionoo/ClientsStatusUpdater.java b/src/org/torproject/onionoo/ClientsStatusUpdater.java index 0ce550f..8a98ccf 100644 --- a/src/org/torproject/onionoo/ClientsStatusUpdater.java +++ b/src/org/torproject/onionoo/ClientsStatusUpdater.java @@ -58,9 +58,6 @@ public class ClientsStatusUpdater implements DescriptorListener, } }
- private static final long ONE_HOUR_MILLIS = 60L * 60L * 1000L, - ONE_DAY_MILLIS = 24L * ONE_HOUR_MILLIS; - private SortedMap<String, SortedSet<ClientsHistory>> newResponses = new TreeMap<String, SortedSet<ClientsHistory>>();
@@ -68,10 +65,11 @@ public class ClientsStatusUpdater implements DescriptorListener, ExtraInfoDescriptor descriptor) { long dirreqStatsEndMillis = descriptor.getDirreqStatsEndMillis(); long dirreqStatsIntervalLengthMillis = - descriptor.getDirreqStatsIntervalLength() * 1000L; + descriptor.getDirreqStatsIntervalLength() + * DateTimeHelper.ONE_SECOND; SortedMap<String, Integer> responses = descriptor.getDirreqV3Resp(); if (dirreqStatsEndMillis < 0L || - dirreqStatsIntervalLengthMillis != ONE_DAY_MILLIS || + dirreqStatsIntervalLengthMillis != DateTimeHelper.ONE_DAY || responses == null || !responses.containsKey("ok")) { return; } @@ -82,8 +80,8 @@ public class ClientsStatusUpdater implements DescriptorListener, String hashedFingerprint = descriptor.getFingerprint().toUpperCase(); long dirreqStatsStartMillis = dirreqStatsEndMillis - dirreqStatsIntervalLengthMillis; - long utcBreakMillis = (dirreqStatsEndMillis / ONE_DAY_MILLIS) - * ONE_DAY_MILLIS; + long utcBreakMillis = (dirreqStatsEndMillis / DateTimeHelper.ONE_DAY) + * DateTimeHelper.ONE_DAY; for (int i = 0; i < 2; i++) { long startMillis = i == 0 ? dirreqStatsStartMillis : utcBreakMillis; long endMillis = i == 0 ? utcBreakMillis : dirreqStatsEndMillis; @@ -92,7 +90,7 @@ public class ClientsStatusUpdater implements DescriptorListener, } double totalResponses = okResponses * ((double) (endMillis - startMillis)) - / ((double) ONE_DAY_MILLIS); + / ((double) DateTimeHelper.ONE_DAY); SortedMap<String, Double> responsesByCountry = this.weightResponsesWithUniqueIps(totalResponses, descriptor.getBridgeIps(), "??"); @@ -179,13 +177,13 @@ public class ClientsStatusUpdater implements DescriptorListener, for (ClientsHistory responses : history) { long intervalLengthMillis; if (this.now - responses.endMillis <= - 92L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 24L * 60L * 60L * 1000L; + DateTimeHelper.ROUGHLY_THREE_MONTHS) { + intervalLengthMillis = DateTimeHelper.ONE_DAY; } else if (this.now - responses.endMillis <= - 366L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 2L * 24L * 60L * 60L * 1000L; + DateTimeHelper.ROUGHLY_ONE_YEAR) { + intervalLengthMillis = DateTimeHelper.TWO_DAYS; } else { - intervalLengthMillis = 10L * 24L * 60L * 60L * 1000L; + intervalLengthMillis = DateTimeHelper.TEN_DAYS; } String monthString = DateTimeHelper.format(responses.startMillis, DateTimeHelper.ISO_YEARMONTH_FORMAT); diff --git a/src/org/torproject/onionoo/DateTimeHelper.java b/src/org/torproject/onionoo/DateTimeHelper.java index efe126b..fb0c00d 100644 --- a/src/org/torproject/onionoo/DateTimeHelper.java +++ b/src/org/torproject/onionoo/DateTimeHelper.java @@ -14,6 +14,25 @@ public class DateTimeHelper { private DateTimeHelper() { }
+ public static final long ONE_SECOND = 1000L, + TEN_SECONDS = 10L * ONE_SECOND, + ONE_MINUTE = 60L * ONE_SECOND, + FIVE_MINUTES = 5L * ONE_MINUTE, + FIFTEEN_MINUTES = 15L * ONE_MINUTE, + ONE_HOUR = 60L * ONE_MINUTE, + FOUR_HOURS = 4L * ONE_HOUR, + SIX_HOURS = 6L * ONE_HOUR, + TWELVE_HOURS = 12L * ONE_HOUR, + ONE_DAY = 24L * ONE_HOUR, + TWO_DAYS = 2L * ONE_DAY, + THREE_DAYS = 3L * ONE_DAY, + ONE_WEEK = 7L * ONE_DAY, + TEN_DAYS = 10L * ONE_DAY, + ROUGHLY_ONE_MONTH = 31L * ONE_DAY, + ROUGHLY_THREE_MONTHS = 92L * ONE_DAY, + ROUGHLY_ONE_YEAR = 366L * ONE_DAY, + ROUGHLY_FIVE_YEARS = 5L * ROUGHLY_ONE_YEAR; + public static final String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String ISO_DATETIME_TAB_FORMAT = diff --git a/src/org/torproject/onionoo/DocumentStore.java b/src/org/torproject/onionoo/DocumentStore.java index a91f302..46b5d98 100644 --- a/src/org/torproject/onionoo/DocumentStore.java +++ b/src/org/torproject/onionoo/DocumentStore.java @@ -484,7 +484,7 @@ public class DocumentStore { maxLastSeenMillis = node.getLastSeenMillis(); } } - cutoff = maxLastSeenMillis - 7L * 24L * 60L * 60L * 1000L; + cutoff = maxLastSeenMillis - DateTimeHelper.ONE_WEEK; } for (Map.Entry<String, NodeStatus> e : this.cachedNodeStatuses.entrySet()) { diff --git a/src/org/torproject/onionoo/Logger.java b/src/org/torproject/onionoo/Logger.java index ccc7962..5ae9be1 100644 --- a/src/org/torproject/onionoo/Logger.java +++ b/src/org/torproject/onionoo/Logger.java @@ -29,7 +29,9 @@ public class Logger {
public static String formatMillis(long millis) { return String.format("%02d:%02d.%03d minutes", - millis / (1000L * 60L), (millis / 1000L) % 60L, millis % 1000L); + millis / DateTimeHelper.ONE_MINUTE, + (millis % DateTimeHelper.ONE_MINUTE) / DateTimeHelper.ONE_SECOND, + millis % DateTimeHelper.ONE_SECOND); }
public static String formatBytes(long bytes) { diff --git a/src/org/torproject/onionoo/NodeDetailsStatusUpdater.java b/src/org/torproject/onionoo/NodeDetailsStatusUpdater.java index aa245dc..d6cd2ec 100644 --- a/src/org/torproject/onionoo/NodeDetailsStatusUpdater.java +++ b/src/org/torproject/onionoo/NodeDetailsStatusUpdater.java @@ -308,7 +308,8 @@ public class NodeDetailsStatusUpdater implements DescriptorListener, } StringBuilder sb = new StringBuilder(); String lastRestartedString = DateTimeHelper.format( - descriptor.getPublishedMillis() - descriptor.getUptime() * 1000L); + descriptor.getPublishedMillis() - descriptor.getUptime() + * DateTimeHelper.ONE_SECOND); int bandwidthRate = descriptor.getBandwidthRate(); int bandwidthBurst = descriptor.getBandwidthBurst(); int observedBandwidth = descriptor.getBandwidthObserved(); @@ -392,7 +393,8 @@ public class NodeDetailsStatusUpdater implements DescriptorListener, } StringBuilder sb = new StringBuilder(); String lastRestartedString = DateTimeHelper.format( - descriptor.getPublishedMillis() - descriptor.getUptime() * 1000L); + descriptor.getPublishedMillis() - descriptor.getUptime() + * DateTimeHelper.ONE_SECOND); int advertisedBandwidth = Math.min(descriptor.getBandwidthRate(), Math.min(descriptor.getBandwidthBurst(), descriptor.getBandwidthObserved())); diff --git a/src/org/torproject/onionoo/ResponseBuilder.java b/src/org/torproject/onionoo/ResponseBuilder.java index 73ce683..bde8961 100644 --- a/src/org/torproject/onionoo/ResponseBuilder.java +++ b/src/org/torproject/onionoo/ResponseBuilder.java @@ -31,7 +31,7 @@ public class ResponseBuilder { private static SortedMap<Integer, Set<String>> relaysByFirstSeenDays = null, bridgesByFirstSeenDays = null, relaysByLastSeenDays = null, bridgesByLastSeenDays = null; - private static final long SUMMARY_MAX_AGE = 6L * 60L * 60L * 1000L; + private static final long SUMMARY_MAX_AGE = DateTimeHelper.SIX_HOURS;
public static void initialize(DocumentStore documentStoreParam, Time timeParam) { diff --git a/src/org/torproject/onionoo/ReverseDomainNameResolver.java b/src/org/torproject/onionoo/ReverseDomainNameResolver.java index 586da35..7f14460 100644 --- a/src/org/torproject/onionoo/ReverseDomainNameResolver.java +++ b/src/org/torproject/onionoo/ReverseDomainNameResolver.java @@ -89,11 +89,12 @@ public class ReverseDomainNameResolver { this.time = time; }
- private static final long RDNS_LOOKUP_MAX_REQUEST_MILLIS = 10L * 1000L; - private static final long RDNS_LOOKUP_MAX_DURATION_MILLIS = 5L * 60L - * 1000L; - private static final long RDNS_LOOKUP_MAX_AGE_MILLIS = 12L * 60L * 60L - * 1000L; + private static final long RDNS_LOOKUP_MAX_REQUEST_MILLIS = + DateTimeHelper.TEN_SECONDS; + private static final long RDNS_LOOKUP_MAX_DURATION_MILLIS = + DateTimeHelper.FIVE_MINUTES; + private static final long RDNS_LOOKUP_MAX_AGE_MILLIS = + DateTimeHelper.TWELVE_HOURS; private static final int RDNS_LOOKUP_WORKERS_NUM = 5;
private Map<String, Long> addressLastLookupTimes; diff --git a/src/org/torproject/onionoo/UptimeDocumentWriter.java b/src/org/torproject/onionoo/UptimeDocumentWriter.java index ea6bd4e..06fde0f 100644 --- a/src/org/torproject/onionoo/UptimeDocumentWriter.java +++ b/src/org/torproject/onionoo/UptimeDocumentWriter.java @@ -32,8 +32,6 @@ public class UptimeDocumentWriter implements FingerprintListener, DescriptorType.BRIDGE_STATUSES); }
- private static final long ONE_HOUR_MILLIS = 60L * 60L * 1000L; - private SortedSet<String> newRelayFingerprints = new TreeSet<String>(), newBridgeFingerprints = new TreeSet<String>();
@@ -96,18 +94,18 @@ public class UptimeDocumentWriter implements FingerprintListener, "5_years" };
private long[] graphIntervals = new long[] { - 7L * 24L * 60L * 60L * 1000L, - 31L * 24L * 60L * 60L * 1000L, - 92L * 24L * 60L * 60L * 1000L, - 366L * 24L * 60L * 60L * 1000L, - 5L * 366L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.ONE_WEEK, + DateTimeHelper.ROUGHLY_ONE_MONTH, + DateTimeHelper.ROUGHLY_THREE_MONTHS, + DateTimeHelper.ROUGHLY_ONE_YEAR, + DateTimeHelper.ROUGHLY_FIVE_YEARS };
private long[] dataPointIntervals = new long[] { - 60L * 60L * 1000L, - 4L * 60L * 60L * 1000L, - 12L * 60L * 60L * 1000L, - 2L * 24L * 60L * 60L * 1000L, - 10L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.ONE_HOUR, + DateTimeHelper.FOUR_HOURS, + DateTimeHelper.TWELVE_HOURS, + DateTimeHelper.TWO_DAYS, + DateTimeHelper.TEN_DAYS };
private String formatHistoryString(boolean relay, String fingerprint, SortedSet<UptimeHistory> history, @@ -138,7 +136,7 @@ public class UptimeDocumentWriter implements FingerprintListener, long dataPointInterval = this.dataPointIntervals[graphIntervalIndex]; int dataPointIntervalHours = (int) (dataPointInterval - / ONE_HOUR_MILLIS); + / DateTimeHelper.ONE_HOUR); List<Integer> statusDataPoints = new ArrayList<Integer>(); long intervalStartMillis = ((this.now - graphInterval) / dataPointInterval) * dataPointInterval; @@ -147,7 +145,7 @@ public class UptimeDocumentWriter implements FingerprintListener, if (hist.relay != relay) { continue; } - long histEndMillis = hist.startMillis + ONE_HOUR_MILLIS + long histEndMillis = hist.startMillis + DateTimeHelper.ONE_HOUR * hist.uptimeHours; if (histEndMillis < intervalStartMillis) { continue; @@ -162,14 +160,14 @@ public class UptimeDocumentWriter implements FingerprintListener, while (histEndMillis >= intervalStartMillis + dataPointInterval) { statusHours += (int) ((intervalStartMillis + dataPointInterval - Math.max(hist.startMillis, intervalStartMillis)) - / ONE_HOUR_MILLIS); + / DateTimeHelper.ONE_HOUR); statusDataPoints.add(statusHours * 5 > dataPointIntervalHours ? statusHours : -1); statusHours = 0; intervalStartMillis += dataPointInterval; } statusHours += (int) ((histEndMillis - Math.max(hist.startMillis, - intervalStartMillis)) / ONE_HOUR_MILLIS); + intervalStartMillis)) / DateTimeHelper.ONE_HOUR); } statusDataPoints.add(statusHours * 5 > dataPointIntervalHours ? statusHours : -1); @@ -185,7 +183,7 @@ public class UptimeDocumentWriter implements FingerprintListener, if (firstStatusStartMillis < 0L) { firstStatusStartMillis = hist.startMillis; } - long histEndMillis = hist.startMillis + ONE_HOUR_MILLIS + long histEndMillis = hist.startMillis + DateTimeHelper.ONE_HOUR * hist.uptimeHours; if (histEndMillis < intervalStartMillis) { continue; @@ -204,13 +202,13 @@ public class UptimeDocumentWriter implements FingerprintListener, while (histEndMillis >= intervalStartMillis + dataPointInterval) { uptimeHours += (int) ((intervalStartMillis + dataPointInterval - Math.max(hist.startMillis, intervalStartMillis)) - / ONE_HOUR_MILLIS); + / DateTimeHelper.ONE_HOUR); uptimeDataPoints.add(uptimeHours); uptimeHours = 0; intervalStartMillis += dataPointInterval; } uptimeHours += (int) ((histEndMillis - Math.max(hist.startMillis, - intervalStartMillis)) / ONE_HOUR_MILLIS); + intervalStartMillis)) / DateTimeHelper.ONE_HOUR); } uptimeDataPoints.add(uptimeHours); List<Double> dataPoints = new ArrayList<Double>(); @@ -258,7 +256,8 @@ public class UptimeDocumentWriter implements FingerprintListener, sb.append(""" + graphName + "":{" + ""first":"" + DateTimeHelper.format(firstDataPointMillis) + "","last":"" + DateTimeHelper.format(lastDataPointMillis) - + "","interval":" + String.valueOf(dataPointInterval / 1000L) + + "","interval":" + String.valueOf(dataPointInterval + / DateTimeHelper.ONE_SECOND) + ","factor":" + String.format(Locale.US, "%.9f", factor) + ","count":" + String.valueOf(count) + ","values":["); int dataPointsWritten = 0, previousNonNullIndex = -2; diff --git a/src/org/torproject/onionoo/UptimeStatusUpdater.java b/src/org/torproject/onionoo/UptimeStatusUpdater.java index 30ab703..7bf20e5 100644 --- a/src/org/torproject/onionoo/UptimeStatusUpdater.java +++ b/src/org/torproject/onionoo/UptimeStatusUpdater.java @@ -50,8 +50,6 @@ public class UptimeStatusUpdater implements DescriptorListener, newRunningRelays = new TreeMap<String, SortedSet<Long>>(), newRunningBridges = new TreeMap<String, SortedSet<Long>>();
- private static final long ONE_HOUR_MILLIS = 60L * 60L * 1000L; - private void processRelayNetworkStatusConsensus( RelayNetworkStatusConsensus consensus) { SortedSet<String> fingerprints = new TreeSet<String>(); @@ -63,7 +61,7 @@ public class UptimeStatusUpdater implements DescriptorListener, } if (!fingerprints.isEmpty()) { long dateHourMillis = (consensus.getValidAfterMillis() - / ONE_HOUR_MILLIS) * ONE_HOUR_MILLIS; + / DateTimeHelper.ONE_HOUR) * DateTimeHelper.ONE_HOUR; for (String fingerprint : fingerprints) { if (!this.newRunningRelays.containsKey(fingerprint)) { this.newRunningRelays.put(fingerprint, new TreeSet<Long>()); @@ -84,7 +82,7 @@ public class UptimeStatusUpdater implements DescriptorListener, } if (!fingerprints.isEmpty()) { long dateHourMillis = (status.getPublishedMillis() - / ONE_HOUR_MILLIS) * ONE_HOUR_MILLIS; + / DateTimeHelper.ONE_HOUR) * DateTimeHelper.ONE_HOUR; for (String fingerprint : fingerprints) { if (!this.newRunningBridges.containsKey(fingerprint)) { this.newRunningBridges.put(fingerprint, new TreeSet<Long>()); @@ -134,15 +132,16 @@ public class UptimeStatusUpdater implements DescriptorListener, if (!history.headSet(interval).isEmpty()) { UptimeHistory prev = history.headSet(interval).last(); if (prev.relay == interval.relay && - prev.startMillis + ONE_HOUR_MILLIS * prev.uptimeHours > - interval.startMillis) { + prev.startMillis + DateTimeHelper.ONE_HOUR + * prev.uptimeHours > interval.startMillis) { continue; } } if (!history.tailSet(interval).isEmpty()) { UptimeHistory next = history.tailSet(interval).first(); if (next.relay == interval.relay && - next.startMillis < interval.startMillis + ONE_HOUR_MILLIS) { + next.startMillis < interval.startMillis + + DateTimeHelper.ONE_HOUR) { continue; } } @@ -157,7 +156,7 @@ public class UptimeStatusUpdater implements DescriptorListener, UptimeHistory lastInterval = null; for (UptimeHistory interval : history) { if (lastInterval != null && - lastInterval.startMillis + ONE_HOUR_MILLIS + lastInterval.startMillis + DateTimeHelper.ONE_HOUR * lastInterval.uptimeHours == interval.startMillis && lastInterval.relay == interval.relay) { lastInterval.addUptime(interval); diff --git a/src/org/torproject/onionoo/WeightsDocumentWriter.java b/src/org/torproject/onionoo/WeightsDocumentWriter.java index 6e225f1..83fe872 100644 --- a/src/org/torproject/onionoo/WeightsDocumentWriter.java +++ b/src/org/torproject/onionoo/WeightsDocumentWriter.java @@ -80,18 +80,18 @@ public class WeightsDocumentWriter implements FingerprintListener, "5_years" };
private long[] graphIntervals = new long[] { - 7L * 24L * 60L * 60L * 1000L, - 31L * 24L * 60L * 60L * 1000L, - 92L * 24L * 60L * 60L * 1000L, - 366L * 24L * 60L * 60L * 1000L, - 5L * 366L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.ONE_WEEK, + DateTimeHelper.ROUGHLY_ONE_MONTH, + DateTimeHelper.ROUGHLY_THREE_MONTHS, + DateTimeHelper.ROUGHLY_ONE_YEAR, + DateTimeHelper.ROUGHLY_FIVE_YEARS };
private long[] dataPointIntervals = new long[] { - 60L * 60L * 1000L, - 4L * 60L * 60L * 1000L, - 12L * 60L * 60L * 1000L, - 2L * 24L * 60L * 60L * 1000L, - 10L * 24L * 60L * 60L * 1000L }; + DateTimeHelper.ONE_HOUR, + DateTimeHelper.FOUR_HOURS, + DateTimeHelper.TWELVE_HOURS, + DateTimeHelper.TWO_DAYS, + DateTimeHelper.TEN_DAYS };
private String formatHistoryString(String fingerprint, SortedMap<long[], double[]> history) { @@ -184,7 +184,8 @@ public class WeightsDocumentWriter implements FingerprintListener, sb.append(""" + graphName + "":{" + ""first":"" + DateTimeHelper.format(firstDataPointMillis) + "","last":"" + DateTimeHelper.format(lastDataPointMillis) - + "","interval":" + String.valueOf(dataPointInterval / 1000L) + + "","interval":" + String.valueOf(dataPointInterval + / DateTimeHelper.ONE_SECOND) + ","factor":" + String.format(Locale.US, "%.9f", factor) + ","count":" + String.valueOf(count) + ","values":["); int dataPointsWritten = 0, previousNonNullIndex = -2; diff --git a/src/org/torproject/onionoo/WeightsStatusUpdater.java b/src/org/torproject/onionoo/WeightsStatusUpdater.java index 3438325..fce1c3e 100644 --- a/src/org/torproject/onionoo/WeightsStatusUpdater.java +++ b/src/org/torproject/onionoo/WeightsStatusUpdater.java @@ -285,16 +285,19 @@ public class WeightsStatusUpdater implements DescriptorListener, long startMillis = e.getKey()[0], endMillis = e.getKey()[1]; double[] weights = e.getValue(); long intervalLengthMillis; - if (this.now - endMillis <= 7L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 60L * 60L * 1000L; - } else if (this.now - endMillis <= 31L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 4L * 60L * 60L * 1000L; - } else if (this.now - endMillis <= 92L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 12L * 60L * 60L * 1000L; - } else if (this.now - endMillis <= 366L * 24L * 60L * 60L * 1000L) { - intervalLengthMillis = 2L * 24L * 60L * 60L * 1000L; + if (this.now - endMillis <= DateTimeHelper.ONE_WEEK) { + intervalLengthMillis = DateTimeHelper.ONE_HOUR; + } else if (this.now - endMillis <= + DateTimeHelper.ROUGHLY_ONE_MONTH) { + intervalLengthMillis = DateTimeHelper.FOUR_HOURS; + } else if (this.now - endMillis <= + DateTimeHelper.ROUGHLY_THREE_MONTHS) { + intervalLengthMillis = DateTimeHelper.TWELVE_HOURS; + } else if (this.now - endMillis <= + DateTimeHelper.ROUGHLY_ONE_YEAR) { + intervalLengthMillis = DateTimeHelper.TWO_DAYS; } else { - intervalLengthMillis = 10L * 24L * 60L * 60L * 1000L; + intervalLengthMillis = DateTimeHelper.TEN_DAYS; } String monthString = DateTimeHelper.format(startMillis, DateTimeHelper.ISO_YEARMONTH_FORMAT); @@ -303,11 +306,11 @@ public class WeightsStatusUpdater implements DescriptorListener, ((endMillis - 1L) / intervalLengthMillis) && lastMonthString.equals(monthString)) { double lastIntervalInHours = (double) ((lastEndMillis - - lastStartMillis) / 60L * 60L * 1000L); + - lastStartMillis) / DateTimeHelper.ONE_HOUR); double currentIntervalInHours = (double) ((endMillis - - startMillis) / 60L * 60L * 1000L); + - startMillis) / DateTimeHelper.ONE_HOUR); double newIntervalInHours = (double) ((endMillis - - lastStartMillis) / 60L * 60L * 1000L); + - lastStartMillis) / DateTimeHelper.ONE_HOUR); for (int i = 0; i < lastWeights.length; i++) { lastWeights[i] *= lastIntervalInHours; lastWeights[i] += weights[i] * currentIntervalInHours;
tor-commits@lists.torproject.org