[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [onionoo/master] Use exact periods for graphs covering months/years.



commit 5cefee7b9de2a746ccaff42c6a0adb27cac8578a
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Mon Jan 15 15:20:02 2018 +0100

    Use exact periods for graphs covering months/years.
---
 .../onionoo/writer/BandwidthDocumentWriter.java    | 32 ++++++++++++++--------
 .../onionoo/writer/ClientsDocumentWriter.java      | 29 ++++++++++++--------
 .../onionoo/writer/UptimeDocumentWriter.java       | 29 ++++++++++++--------
 .../onionoo/writer/WeightsDocumentWriter.java      | 29 ++++++++++++--------
 4 files changed, 74 insertions(+), 45 deletions(-)

diff --git a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java
index f1c5041..2f27271 100644
--- a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java
@@ -15,6 +15,9 @@ import org.torproject.onionoo.docs.UpdateStatus;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.time.LocalDateTime;
+import java.time.Period;
+import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -79,13 +82,13 @@ public class BandwidthDocumentWriter implements DocumentWriter {
       "1_year",
       "5_years" };
 
-  private long[] graphIntervals = new long[] {
-      DateTimeHelper.THREE_DAYS,
-      DateTimeHelper.ONE_WEEK,
-      DateTimeHelper.ROUGHLY_ONE_MONTH,
-      DateTimeHelper.ROUGHLY_THREE_MONTHS,
-      DateTimeHelper.ROUGHLY_ONE_YEAR,
-      DateTimeHelper.ROUGHLY_FIVE_YEARS };
+  private Period[] graphIntervals = new Period[] {
+      Period.ofDays(3),
+      Period.ofWeeks(1),
+      Period.ofMonths(1),
+      Period.ofMonths(3),
+      Period.ofYears(1),
+      Period.ofYears(5) };
 
   private long[] dataPointIntervals = new long[] {
       DateTimeHelper.FIFTEEN_MINUTES,
@@ -100,12 +103,15 @@ public class BandwidthDocumentWriter implements DocumentWriter {
     Map<String, GraphHistory> graphs = new LinkedHashMap<>();
     for (int i = 0; i < this.graphIntervals.length; i++) {
       String graphName = this.graphNames[i];
-      long graphInterval = this.graphIntervals[i];
+      Period graphInterval = this.graphIntervals[i];
       long dataPointInterval = this.dataPointIntervals[i];
       List<Long> dataPoints = new ArrayList<>();
       long graphEndMillis = ((lastSeenMillis + DateTimeHelper.ONE_HOUR)
           / dataPointInterval) * dataPointInterval;
-      long graphStartMillis = graphEndMillis - graphInterval;
+      long graphStartMillis = LocalDateTime
+          .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+          .minus(graphInterval)
+          .toEpochSecond(ZoneOffset.UTC) * 1000L;
       long intervalStartMillis = graphStartMillis;
       long totalMillis = 0L;
       long totalBandwidth = 0L;
@@ -159,9 +165,11 @@ public class BandwidthDocumentWriter implements DocumentWriter {
       }
       long firstDataPointMillis = graphStartMillis + firstNonNullIndex
           * dataPointInterval + dataPointInterval / 2L;
-      if (i > 0 && !graphs.isEmpty() && firstDataPointMillis >=
-          ((lastSeenMillis + DateTimeHelper.ONE_HOUR) / dataPointInterval)
-          * dataPointInterval - graphIntervals[i - 1]) {
+      if (i > 0 && !graphs.isEmpty() && firstDataPointMillis >= LocalDateTime
+          .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+          .minus(graphIntervals[i - 1])
+          .toEpochSecond(ZoneOffset.UTC) * 1000L) {
+
         /* Skip bandwidth history object, because it doesn't contain
          * anything new that wasn't already contained in the last
          * bandwidth history object(s).  Unless we did not include any of
diff --git a/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java
index 81168f5..4eca33a 100644
--- a/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java
@@ -17,6 +17,9 @@ import org.torproject.onionoo.util.FormattingUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.time.LocalDateTime;
+import java.time.Period;
+import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -94,12 +97,12 @@ public class ClientsDocumentWriter implements DocumentWriter {
       "1_year",
       "5_years" };
 
-  private long[] graphIntervals = new long[] {
-      DateTimeHelper.ONE_WEEK,
-      DateTimeHelper.ROUGHLY_ONE_MONTH,
-      DateTimeHelper.ROUGHLY_THREE_MONTHS,
-      DateTimeHelper.ROUGHLY_ONE_YEAR,
-      DateTimeHelper.ROUGHLY_FIVE_YEARS };
+  private Period[] graphIntervals = new Period[] {
+      Period.ofWeeks(1),
+      Period.ofMonths(1),
+      Period.ofMonths(3),
+      Period.ofYears(1),
+      Period.ofYears(5) };
 
   private long[] dataPointIntervals = new long[] {
       DateTimeHelper.ONE_DAY,
@@ -129,13 +132,16 @@ public class ClientsDocumentWriter implements DocumentWriter {
   private GraphHistory compileClientsHistory(
       int graphIntervalIndex, SortedSet<ClientsHistory> history,
       long lastSeenMillis) {
-    long graphInterval = this.graphIntervals[graphIntervalIndex];
+    Period graphInterval = this.graphIntervals[graphIntervalIndex];
     long dataPointInterval =
         this.dataPointIntervals[graphIntervalIndex];
     List<Double> dataPoints = new ArrayList<>();
     long graphEndMillis = ((lastSeenMillis + DateTimeHelper.ONE_HOUR)
         / dataPointInterval) * dataPointInterval;
-    long graphStartMillis = graphEndMillis - graphInterval;
+    long graphStartMillis = LocalDateTime
+        .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+        .minus(graphInterval)
+        .toEpochSecond(ZoneOffset.UTC) * 1000L;
     long intervalStartMillis = graphStartMillis;
     long millis = 0L;
     double responses = 0.0;
@@ -182,9 +188,10 @@ public class ClientsDocumentWriter implements DocumentWriter {
     }
     long firstDataPointMillis = graphStartMillis + firstNonNullIndex
         * dataPointInterval + dataPointInterval / 2L;
-    if (graphIntervalIndex > 0 && firstDataPointMillis >=
-        ((lastSeenMillis + DateTimeHelper.ONE_HOUR) / dataPointInterval)
-        * dataPointInterval - graphIntervals[graphIntervalIndex - 1]) {
+    if (graphIntervalIndex > 0 && firstDataPointMillis >= LocalDateTime
+        .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+        .minus(graphIntervals[graphIntervalIndex - 1])
+        .toEpochSecond(ZoneOffset.UTC) * 1000L) {
       /* Skip clients history object, because it doesn't contain
        * anything new that wasn't already contained in the last
        * clients history object(s). */
diff --git a/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java
index 2aee7a1..d1e2003 100644
--- a/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java
@@ -17,6 +17,9 @@ import org.torproject.onionoo.util.FormattingUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.time.LocalDateTime;
+import java.time.Period;
+import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -88,12 +91,12 @@ public class UptimeDocumentWriter implements DocumentWriter {
       "1_year",
       "5_years" };
 
-  private long[] graphIntervals = new long[] {
-      DateTimeHelper.ONE_WEEK,
-      DateTimeHelper.ROUGHLY_ONE_MONTH,
-      DateTimeHelper.ROUGHLY_THREE_MONTHS,
-      DateTimeHelper.ROUGHLY_ONE_YEAR,
-      DateTimeHelper.ROUGHLY_FIVE_YEARS };
+  private Period[] graphIntervals = new Period[] {
+      Period.ofWeeks(1),
+      Period.ofMonths(1),
+      Period.ofMonths(3),
+      Period.ofYears(1),
+      Period.ofYears(5) };
 
   private long[] dataPointIntervals = new long[] {
       DateTimeHelper.ONE_HOUR,
@@ -152,7 +155,7 @@ public class UptimeDocumentWriter implements DocumentWriter {
       boolean relay, SortedSet<UptimeHistory> history,
       SortedSet<UptimeHistory> knownStatuses, long lastSeenMillis,
       String flag) {
-    long graphInterval = this.graphIntervals[graphIntervalIndex];
+    Period graphInterval = this.graphIntervals[graphIntervalIndex];
     long dataPointInterval =
         this.dataPointIntervals[graphIntervalIndex];
     int dataPointIntervalHours = (int) (dataPointInterval
@@ -160,7 +163,10 @@ public class UptimeDocumentWriter implements DocumentWriter {
     List<Integer> uptimeDataPoints = new ArrayList<>();
     long graphEndMillis = ((lastSeenMillis + DateTimeHelper.ONE_HOUR)
         / dataPointInterval) * dataPointInterval;
-    long graphStartMillis = graphEndMillis - graphInterval;
+    long graphStartMillis = LocalDateTime
+        .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+        .minus(graphInterval)
+        .toEpochSecond(ZoneOffset.UTC) * 1000L;
     long intervalStartMillis = graphStartMillis;
     int uptimeHours = 0;
     long firstStatusStartMillis = -1L;
@@ -285,9 +291,10 @@ public class UptimeDocumentWriter implements DocumentWriter {
     }
     long firstDataPointMillis = graphStartMillis + firstNonNullIndex
         * dataPointInterval + dataPointInterval / 2L;
-    if (graphIntervalIndex > 0 && firstDataPointMillis >=
-        ((lastSeenMillis + DateTimeHelper.ONE_HOUR) / dataPointInterval)
-        * dataPointInterval - graphIntervals[graphIntervalIndex - 1]) {
+    if (graphIntervalIndex > 0 && firstDataPointMillis >= LocalDateTime
+        .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+        .minus(graphIntervals[graphIntervalIndex - 1])
+        .toEpochSecond(ZoneOffset.UTC) * 1000L) {
       /* Skip uptime history object, because it doesn't contain
        * anything new that wasn't already contained in the last
        * uptime history object(s). */
diff --git a/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java
index 10d8a94..f4e2c3a 100644
--- a/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java
@@ -15,6 +15,9 @@ import org.torproject.onionoo.docs.WeightsStatus;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.time.LocalDateTime;
+import java.time.Period;
+import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -68,12 +71,12 @@ public class WeightsDocumentWriter implements DocumentWriter {
       "1_year",
       "5_years" };
 
-  private long[] graphIntervals = new long[] {
-      DateTimeHelper.ONE_WEEK,
-      DateTimeHelper.ROUGHLY_ONE_MONTH,
-      DateTimeHelper.ROUGHLY_THREE_MONTHS,
-      DateTimeHelper.ROUGHLY_ONE_YEAR,
-      DateTimeHelper.ROUGHLY_FIVE_YEARS };
+  private Period[] graphIntervals = new Period[] {
+      Period.ofWeeks(1),
+      Period.ofMonths(1),
+      Period.ofMonths(3),
+      Period.ofYears(1),
+      Period.ofYears(5) };
 
   private long[] dataPointIntervals = new long[] {
       DateTimeHelper.ONE_HOUR,
@@ -118,13 +121,16 @@ public class WeightsDocumentWriter implements DocumentWriter {
   private GraphHistory compileWeightsHistory(int graphTypeIndex,
       int graphIntervalIndex, SortedMap<long[], double[]> history,
       long lastSeenMillis) {
-    long graphInterval = this.graphIntervals[graphIntervalIndex];
+    Period graphInterval = this.graphIntervals[graphIntervalIndex];
     long dataPointInterval =
         this.dataPointIntervals[graphIntervalIndex];
     List<Double> dataPoints = new ArrayList<>();
     long graphEndMillis = ((lastSeenMillis + DateTimeHelper.ONE_HOUR)
         / dataPointInterval) * dataPointInterval;
-    long graphStartMillis = graphEndMillis - graphInterval;
+    long graphStartMillis = LocalDateTime
+        .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+        .minus(graphInterval)
+        .toEpochSecond(ZoneOffset.UTC) * 1000L;
     long intervalStartMillis = graphStartMillis;
     long totalMillis = 0L;
     double totalWeightTimesMillis = 0.0;
@@ -175,9 +181,10 @@ public class WeightsDocumentWriter implements DocumentWriter {
     }
     long firstDataPointMillis = graphStartMillis + firstNonNullIndex
         * dataPointInterval + dataPointInterval / 2L;
-    if (graphIntervalIndex > 0 && firstDataPointMillis >=
-        ((lastSeenMillis + DateTimeHelper.ONE_HOUR) / dataPointInterval)
-        * dataPointInterval - graphIntervals[graphIntervalIndex - 1]) {
+    if (graphIntervalIndex > 0 && firstDataPointMillis >= LocalDateTime
+        .ofEpochSecond(graphEndMillis / 1000L, 0, ZoneOffset.UTC)
+        .minus(graphIntervals[graphIntervalIndex - 1])
+        .toEpochSecond(ZoneOffset.UTC) * 1000L) {
       /* Skip weights history object, because it doesn't contain
        * anything new that wasn't already contained in the last
        * weights history object(s). */



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits