Commit db7377fa authored by iwakeh's avatar iwakeh 🌴
Browse files

Use reflection and overloading in order to facilitate testing and

to avoid the use of TimeFactory and Time classes.  This also makes
it more obvious where time is changed for making the tests work.
parent 5527ff0e
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@
package org.torproject.onionoo.docs;

import org.torproject.descriptor.BandwidthHistory;
import org.torproject.onionoo.util.TimeFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -110,18 +109,21 @@ public class BandwidthStatus extends Document {
  }

  public void compressHistory() {
    this.compressHistory(this.writeHistory);
    this.compressHistory(this.readHistory);
    this.compressHistory(System.currentTimeMillis());
  }

  private void compressHistory(SortedMap<Long, long[]> history) {
  public void compressHistory(long now) {
    this.compressHistory(this.writeHistory, now);
    this.compressHistory(this.readHistory, now);
  }

  private void compressHistory(SortedMap<Long, long[]> history, long now) {
    SortedMap<Long, long[]> uncompressedHistory = new TreeMap<>(history);
    history.clear();
    long lastStartMillis = 0L;
    long lastEndMillis = 0L;
    long lastBandwidth = 0L;
    String lastMonthString = "1970-01";
    long now = TimeFactory.getTime().currentTimeMillis();
    for (long[] v : uncompressedHistory.values()) {
      long startMillis = v[0];
      long endMillis = v[1];
+1 −3
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@

package org.torproject.onionoo.docs;

import org.torproject.onionoo.util.TimeFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -76,7 +74,7 @@ public class ClientsStatus extends Document {
    history.clear();
    ClientsHistory lastResponses = null;
    String lastMonthString = "1970-01";
    long now = TimeFactory.getTime().currentTimeMillis();
    long now = System.currentTimeMillis();
    for (ClientsHistory responses : uncompressedHistory) {
      long intervalLengthMillis;
      if (now - responses.getEndMillis()
+1 −9
Original line number Diff line number Diff line
@@ -4,8 +4,6 @@
package org.torproject.onionoo.docs;

import org.torproject.onionoo.util.FormattingUtils;
import org.torproject.onionoo.util.Time;
import org.torproject.onionoo.util.TimeFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -52,12 +50,6 @@ public class DocumentStore {
    this.outDir = outDir;
  }

  private Time time;

  public DocumentStore() {
    this.time = TimeFactory.getTime();
  }

  private long listOperations = 0L;

  private long listedFiles = 0L;
@@ -814,7 +806,7 @@ public class DocumentStore {
      return;
    }
    UpdateStatus updateStatus = new UpdateStatus();
    updateStatus.setUpdatedMillis(this.time.currentTimeMillis());
    updateStatus.setUpdatedMillis(System.currentTimeMillis());
    this.store(updateStatus);
  }

+1 −3
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@

package org.torproject.onionoo.docs;

import org.torproject.onionoo.util.TimeFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -137,7 +135,7 @@ public class WeightsStatus extends Document {
    double[] lastWeights = null;
    String lastMonthString = "1970-01";
    int lastMissingValues = -1;
    long now = TimeFactory.getTime().currentTimeMillis();
    long now = System.currentTimeMillis();
    for (Map.Entry<long[], double[]> e : uncompressedHistory.entrySet()) {
      long startMillis = e.getKey()[0];
      long endMillis = e.getKey()[1];
+11 −7
Original line number Diff line number Diff line
@@ -7,8 +7,6 @@ import org.torproject.onionoo.docs.DocumentStore;
import org.torproject.onionoo.docs.DocumentStoreFactory;
import org.torproject.onionoo.docs.SummaryDocument;
import org.torproject.onionoo.docs.UpdateStatus;
import org.torproject.onionoo.util.Time;
import org.torproject.onionoo.util.TimeFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -125,6 +123,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
    indexerThread.interrupt();
  }

  private long specialTime = -1L;

  private void indexNodeStatuses() {
    long updateStatusMillis = -1L;
    DocumentStore documentStore = DocumentStoreFactory.getDocumentStore();
@@ -173,7 +173,7 @@ public class NodeIndexer implements ServletContextListener, Runnable {
        currentBridges.add(node);
      }
    }
    Time time = TimeFactory.getTime();

    /* This variable can go away once all Onionoo services had their
     * hourly updater write effective families to summary documents at
     * least once.  Remove this code after September 8, 2015. */
@@ -220,7 +220,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
      if (entry.getEffectiveFamily() != null) {
        newRelaysByFamily.put(fingerprint, entry.getEffectiveFamily());
      }
      int daysSinceFirstSeen = (int) ((time.currentTimeMillis()
      int daysSinceFirstSeen = (int) ((
          (specialTime < 0 ? System.currentTimeMillis() : specialTime)
          - entry.getFirstSeenMillis()) / ONE_DAY);
      if (!newRelaysByFirstSeenDays.containsKey(daysSinceFirstSeen)) {
        newRelaysByFirstSeenDays.put(daysSinceFirstSeen,
@@ -229,7 +230,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
      newRelaysByFirstSeenDays.get(daysSinceFirstSeen).add(fingerprint);
      newRelaysByFirstSeenDays.get(daysSinceFirstSeen).add(
          hashedFingerprint);
      int daysSinceLastSeen = (int) ((time.currentTimeMillis()
      int daysSinceLastSeen = (int) ((
          (specialTime < 0 ? System.currentTimeMillis() : specialTime)
          - entry.getLastSeenMillis()) / ONE_DAY);
      if (!newRelaysByLastSeenDays.containsKey(daysSinceLastSeen)) {
        newRelaysByLastSeenDays.put(daysSinceLastSeen,
@@ -277,7 +279,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
        newBridgesByFlag.get(flagLowerCase).add(
            hashedHashedFingerprint);
      }
      int daysSinceFirstSeen = (int) ((time.currentTimeMillis()
      int daysSinceFirstSeen = (int) ((
          (specialTime < 0 ? System.currentTimeMillis() : specialTime)
          - entry.getFirstSeenMillis()) / ONE_DAY);
      if (!newBridgesByFirstSeenDays.containsKey(daysSinceFirstSeen)) {
        newBridgesByFirstSeenDays.put(daysSinceFirstSeen,
@@ -287,7 +290,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
          hashedFingerprint);
      newBridgesByFirstSeenDays.get(daysSinceFirstSeen).add(
          hashedHashedFingerprint);
      int daysSinceLastSeen = (int) ((time.currentTimeMillis()
      int daysSinceLastSeen = (int) ((
          (specialTime < 0 ? System.currentTimeMillis() : specialTime)
          - entry.getLastSeenMillis()) / ONE_DAY);
      if (!newBridgesByLastSeenDays.containsKey(daysSinceLastSeen)) {
        newBridgesByLastSeenDays.put(daysSinceLastSeen,
Loading