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

[or-cvs] [metrics-web/master 2/3] Get rid of unused DateRanges class and move ErnieProperties class.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Tue, 5 Oct 2010 13:16:59 +0200
Subject: Get rid of unused DateRanges class and move ErnieProperties class.
Commit: fe9cef83ff4f9dcd9f3ee139d708ff5c39241f97

---
 src/org/torproject/ernie/util/DateRanges.java      |  129 --------------------
 src/org/torproject/ernie/util/ErnieProperties.java |   31 -----
 .../torproject/ernie/web/DescriptorServlet.java    |    2 -
 src/org/torproject/ernie/web/ErnieProperties.java  |   31 +++++
 .../ernie/web/ExtraInfoDescriptorServlet.java      |    1 -
 src/org/torproject/ernie/web/GraphController.java  |   36 +++++-
 .../torproject/ernie/web/RelaySearchServlet.java   |    2 -
 src/org/torproject/ernie/web/RelayServlet.java     |    2 -
 .../ernie/web/ServerDescriptorServlet.java         |    1 -
 9 files changed, 63 insertions(+), 172 deletions(-)
 delete mode 100644 src/org/torproject/ernie/util/DateRanges.java
 delete mode 100644 src/org/torproject/ernie/util/ErnieProperties.java
 create mode 100644 src/org/torproject/ernie/web/ErnieProperties.java

diff --git a/src/org/torproject/ernie/util/DateRanges.java b/src/org/torproject/ernie/util/DateRanges.java
deleted file mode 100644
index 9204492..0000000
--- a/src/org/torproject/ernie/util/DateRanges.java
+++ /dev/null
@@ -1,129 +0,0 @@
-package org.torproject.ernie.util;
-
-import org.torproject.ernie.util.ErnieProperties;
-import org.apache.log4j.Logger;
-import java.util.*;
-import java.text.*;
-import java.sql.*;
-import java.util.Date; /* Use java date instead of sql.*/
-
-public class DateRanges {
-
-  private static final Logger log;
-  private static SimpleDateFormat simpledf;
-  private final static String jdbcURL;
-  private static final ErnieProperties props;
-
-  private Connection conn;
-  private PreparedStatement psYearsRange;
-  private PreparedStatement psAllRange;
-
-  static {
-    log = Logger.getLogger(DateRanges.class.toString());
-    props = new ErnieProperties();
-    simpledf = new SimpleDateFormat("yyyy-MM-dd");
-    simpledf.setTimeZone(TimeZone.getTimeZone("UTC"));
-    jdbcURL = props.getProperty("jdbc.url");
-  }
-
-  public DateRanges() {
-    try {
-      this.conn = DriverManager.getConnection(jdbcURL);
-
-      /* Its much faster to get the year from the aggregate tables instead
-       * of the large statusentry or descriptor tables. TODO do this more
-       * robustly? */
-      this.psYearsRange = conn.prepareStatement(
-          "select min(extract('year' from date(date))) as min, " +
-          "max(extract('year' from date(date))) as max " +
-          "from network_size");
-      this.psAllRange = conn.prepareStatement(
-          "select min(date(date)) as min, " +
-          "max(date(date)) as max " +
-          "from network_size");
-    } catch (SQLException e)  {
-      log.warn("Couldn't connect to database or prepare statements. " + e);
-    }
-  }
-
-  /**
-   * Get a range for days in the past, which returns a tuple
-   * that maps to (start, end) - (yyyy-mm-dd, yyyy-mm-dd)
-   */
-  public String[] getDayRange(int days)  {
-    String[] dates = new String[2];
-    Calendar today = Calendar.getInstance();
-    today.setTimeZone(TimeZone.getTimeZone("UTC"));
-    Calendar start = (Calendar)today.clone();
-    start.add(Calendar.DATE, -days);
-
-    dates[0] = simpledf.format(start.getTime());
-    dates[1] = simpledf.format(today.getTime());
-    return dates;
-  }
-
-  /**
-   * Get the years range (of current data in the database), which returns a
-   * simple numeric array of each year.
-   */
-  public int[] getYearsRange()  {
-    int min = 0, max = 0;
-    int yearsrange[] = null;
-    try {
-      ResultSet rsYearsRange = psYearsRange.executeQuery();
-      if (rsYearsRange.next())  {
-        min = rsYearsRange.getInt("min");
-        max = rsYearsRange.getInt("max");
-      }
-      yearsrange = new int[max - min + 1];
-      for (int i = 0; i <= max-min; i++) {
-        yearsrange[i] = min+i;
-      }
-    } catch (SQLException e) {
-      yearsrange = new int[0];
-      log.warn("Couldn't get results from network_size table: " + e);
-    }
-    return yearsrange;
-  }
-
-  /**
-   * Used in conjunction with getYearsRange, it accepts a year and will
-   * return the start and end date.
-   */
-  public String[] getYearsRangeDates(int year)  {
-    String[] dates = new String[2];
-    dates[0] = year + "-01-01";
-    dates[1] = year + "-12-31";
-    return dates;
-  }
-
-  /**
-   * Get the date range (of the current data in the database), which
-   * returns a map with one row that contains the start and end dates
-   * like (yyyy-mm-dd, yyyy-mm-dd).
-   */
-  public String[] getAllDataRange() {
-    String[] range = new String[2];
-    try {
-      ResultSet rsAllRange = psAllRange.executeQuery();
-      if (rsAllRange.next())  {
-        range[0] = rsAllRange.getString("min");
-        range[1] = rsAllRange.getString("max");
-      }
-    } catch (SQLException e) {
-      log.warn("Couldn't get results from network_size table: " + e);
-    }
-    return range;
-  }
-
-  /**
-   * Close database connection.
-   */
-  public void closeConnection() {
-    try {
-      this.conn.close();
-    } catch (SQLException e)  {
-      log.warn("Couldn't close database connection. " + e);
-    }
-  }
-}
diff --git a/src/org/torproject/ernie/util/ErnieProperties.java b/src/org/torproject/ernie/util/ErnieProperties.java
deleted file mode 100644
index 09695d4..0000000
--- a/src/org/torproject/ernie/util/ErnieProperties.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package org.torproject.ernie.util;
-
-import java.util.Properties;
-
-public class ErnieProperties  {
-
-  private static final String PROPS_RESOURCE = "ernie.properties";
-  private static Properties props;
-
-  static {
-    props = new Properties();
-    try {
-      props.load(ErnieProperties.class.getClassLoader()
-          .getResourceAsStream(PROPS_RESOURCE));
-    } catch (Exception e) {
-    }
-  }
-
-  public String getProperty(String key) {
-    return props.getProperty(key);
-  }
-
-  public String getProperty(String key, String defaultValue)  {
-    return props.getProperty(key, defaultValue);
-  }
-
-  public int getInt(String key) {
-    String value = getProperty(key);
-    return value == null ? 0 : Integer.parseInt(value);
-  }
-}
diff --git a/src/org/torproject/ernie/web/DescriptorServlet.java b/src/org/torproject/ernie/web/DescriptorServlet.java
index b26144c..6efaef1 100644
--- a/src/org/torproject/ernie/web/DescriptorServlet.java
+++ b/src/org/torproject/ernie/web/DescriptorServlet.java
@@ -9,8 +9,6 @@ import java.text.*;
 import java.util.*;
 import java.util.regex.*;
 
-import org.torproject.ernie.util.*;
-
 import org.apache.commons.codec.*;
 import org.apache.commons.codec.binary.*;
 
diff --git a/src/org/torproject/ernie/web/ErnieProperties.java b/src/org/torproject/ernie/web/ErnieProperties.java
new file mode 100644
index 0000000..7858285
--- /dev/null
+++ b/src/org/torproject/ernie/web/ErnieProperties.java
@@ -0,0 +1,31 @@
+package org.torproject.ernie.web;
+
+import java.util.Properties;
+
+public class ErnieProperties  {
+
+  private static final String PROPS_RESOURCE = "ernie.properties";
+  private static Properties props;
+
+  static {
+    props = new Properties();
+    try {
+      props.load(ErnieProperties.class.getClassLoader()
+          .getResourceAsStream(PROPS_RESOURCE));
+    } catch (Exception e) {
+    }
+  }
+
+  public String getProperty(String key) {
+    return props.getProperty(key);
+  }
+
+  public String getProperty(String key, String defaultValue)  {
+    return props.getProperty(key, defaultValue);
+  }
+
+  public int getInt(String key) {
+    String value = getProperty(key);
+    return value == null ? 0 : Integer.parseInt(value);
+  }
+}
diff --git a/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java b/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java
index 7429812..d447a7e 100644
--- a/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java
+++ b/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java
@@ -5,7 +5,6 @@ import javax.servlet.http.*;
 import java.io.*;
 import java.sql.*;
 import java.util.regex.*;
-import org.torproject.ernie.util.*;
 
 public class ExtraInfoDescriptorServlet extends HttpServlet {
 
diff --git a/src/org/torproject/ernie/web/GraphController.java b/src/org/torproject/ernie/web/GraphController.java
index 1c54d47..7d30b4d 100644
--- a/src/org/torproject/ernie/web/GraphController.java
+++ b/src/org/torproject/ernie/web/GraphController.java
@@ -6,8 +6,6 @@ import java.util.*;
 import org.rosuda.REngine.Rserve.*;
 import org.rosuda.REngine.*;
 
-import org.torproject.ernie.util.ErnieProperties;
-
 public class GraphController {
 
   /* Singleton instance and getInstance method of this class. */
@@ -105,13 +103,24 @@ public class GraphController {
    * and then the oldest graphs until we have minCacheSize graphs left.
    * Also update currentCacheSize and oldestGraph. */
   public void cleanUpCache() {
-
+BufferedWriter out = null;
+try {
+out = new BufferedWriter(new FileWriter("/tmp/graphcache.log"));
+out.write("cleaning up cache\n");
+out.flush();
+} catch (IOException e) {}
     /* Check if the cache is empty first. */
     File[] filesInCache = new File(this.cachedGraphsDirectory).
         listFiles();
     if (filesInCache.length == 0) {
       this.currentCacheSize = 0;
       this.oldestGraph = System.currentTimeMillis();
+try {
+if (out != null) {
+out.write("cache is empty. exiting\n");
+out.close();
+}
+} catch (IOException e) {}
       return;
     }
 
@@ -129,13 +138,25 @@ public class GraphController {
      * as many graphs as necessary to shrink to minCacheSize graphs. */
     long cutOffTime = System.currentTimeMillis()
         - this.maxCacheAge * 1000L;
+try {
+if (out != null) {
+out.write("cut off time is " + cutOffTime + "\n");
+out.flush();
+}
+} catch (IOException e) {}
     while (!graphsByLastModified.isEmpty()) {
       File oldestGraphInList = graphsByLastModified.remove(0);
       if (oldestGraphInList.lastModified() >= cutOffTime &&
           graphsByLastModified.size() < this.minCacheSize) {
         break;
       }
-      oldestGraphInList.delete();
+      boolean deleted = oldestGraphInList.delete();
+try {
+if (out != null) {
+out.write("deleting " + oldestGraphInList.getName() + " was " + (deleted ? "" : "NOT") + " successful.\n");
+out.flush();
+}
+} catch (IOException e) {}
     }
 
     /* Update currentCacheSize and oldestGraph that we need to decide when
@@ -146,6 +167,13 @@ public class GraphController {
     } else {
       this.oldestGraph = System.currentTimeMillis();
     }
+try {
+if (out != null) {
+out.write("now we have " + this.currentCacheSize + " graphs in our cache, the oldest one being from " + this.oldestGraph + "\n");
+out.close();
+}
+} catch (IOException e) {}
+
   }
 }
 
diff --git a/src/org/torproject/ernie/web/RelaySearchServlet.java b/src/org/torproject/ernie/web/RelaySearchServlet.java
index f61d70e..83f1a64 100644
--- a/src/org/torproject/ernie/web/RelaySearchServlet.java
+++ b/src/org/torproject/ernie/web/RelaySearchServlet.java
@@ -12,8 +12,6 @@ import java.util.regex.*;
 import org.apache.commons.codec.*;
 import org.apache.commons.codec.binary.*;
 
-import org.torproject.ernie.util.*;
-
 /**
  * Web page that allows users to search for relays in the descriptor
  * archives.
diff --git a/src/org/torproject/ernie/web/RelayServlet.java b/src/org/torproject/ernie/web/RelayServlet.java
index 3d82de6..9492090 100644
--- a/src/org/torproject/ernie/web/RelayServlet.java
+++ b/src/org/torproject/ernie/web/RelayServlet.java
@@ -9,8 +9,6 @@ import java.text.*;
 import java.util.*;
 import java.util.regex.*;
 
-import org.torproject.ernie.util.*;
-
 import org.apache.commons.codec.*;
 import org.apache.commons.codec.binary.*;
 
diff --git a/src/org/torproject/ernie/web/ServerDescriptorServlet.java b/src/org/torproject/ernie/web/ServerDescriptorServlet.java
index 4cfab05..69f5bd6 100644
--- a/src/org/torproject/ernie/web/ServerDescriptorServlet.java
+++ b/src/org/torproject/ernie/web/ServerDescriptorServlet.java
@@ -5,7 +5,6 @@ import javax.servlet.http.*;
 import java.io.*;
 import java.sql.*;
 import java.util.regex.*;
-import org.torproject.ernie.util.*;
 
 public class ServerDescriptorServlet extends HttpServlet {
 
-- 
1.7.1