[tor-commits] [metrics-web/master] Accept empty start or end dates.

karsten at torproject.org karsten at torproject.org
Sun Feb 27 11:58:30 UTC 2011


commit b67e014ad4fbfb03a7f44b3f75affef81c2bd5fa
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Sun Feb 27 12:47:57 2011 +0100

    Accept empty start or end dates.
    
    So far we required that either both start and end date are provided, or
    none of them.
    
    Now we also accept the two cases when only one of the two dates is
    provided.  If no end date is provided, we set it to today.  If no start
    date is provided, we set it to 90 days before the end date.  This does
    not affect the cases when both dates are provided (in which case we use
    the provided dates), or when none are provided (in which case we use today
    as the end date and today - 90 days as the start date).
---
 .../ernie/web/GraphParameterChecker.java           |   54 ++++++++-----------
 1 files changed, 23 insertions(+), 31 deletions(-)

diff --git a/src/org/torproject/ernie/web/GraphParameterChecker.java b/src/org/torproject/ernie/web/GraphParameterChecker.java
index 20914a8..caeee06 100644
--- a/src/org/torproject/ernie/web/GraphParameterChecker.java
+++ b/src/org/torproject/ernie/web/GraphParameterChecker.java
@@ -92,50 +92,42 @@ public class GraphParameterChecker {
     Map<String, String[]> recognizedGraphParameters =
         new HashMap<String, String[]>();
 
-    /* Parse start and end dates if supported by the graph type. If
-     * neither start nor end date are given, set the default date range to
-     * the past 90 days. */
+    /* Parse start and end dates if supported by the graph type. If no end
+     * date is provided, set it to today. If no start date is provided,
+     * set it to 90 days before the end date. Make sure that start date
+     * precedes end date. */
     if (supportedGraphParameters.contains("start") ||
         supportedGraphParameters.contains("end")) {
       String[] startParameter = (String[]) requestParameters.get("start");
       String[] endParameter = (String[]) requestParameters.get("end");
-      if ((startParameter == null || startParameter.length < 1 ||
-          startParameter[0].length() < 1) &&
-          (endParameter == null || endParameter.length < 1 ||
-          endParameter[0].length() < 1)) {
-        /* If no start and end parameters are given, set default date
-         * range to the past 90 days. */
-        long now = System.currentTimeMillis();
-        startParameter = new String[] {
-            dateFormat.format(now - 90L * 24L * 60L * 60L * 1000L) };
-        endParameter = new String[] { dateFormat.format(now) };
-      } else if (startParameter != null && startParameter.length == 1 &&
-          endParameter != null && endParameter.length == 1) {
-        long startTimestamp = -1L, endTimestamp = -1L;
+      long endTimestamp = System.currentTimeMillis();
+      if (endParameter != null && endParameter.length > 0 &&
+          endParameter[0].length() > 0) {
         try {
-          startTimestamp = dateFormat.parse(startParameter[0]).getTime();
           endTimestamp = dateFormat.parse(endParameter[0]).getTime();
         } catch (ParseException e)  {
           return null;
         }
-        /* The parameters are dates. Good. Does end not precede start? */
-        if (startTimestamp > endTimestamp) {
+        if (!endParameter[0].startsWith("20")) {
+          return null;
+        }
+      }
+      endParameter = new String[] { dateFormat.format(endTimestamp) };
+      long startTimestamp = endTimestamp - 90L * 24L * 60L * 60L * 1000L;
+      if (startParameter != null && startParameter.length > 0 &&
+          startParameter[0].length() > 0) {
+        try {
+          startTimestamp = dateFormat.parse(startParameter[0]).getTime();
+        } catch (ParseException e)  {
           return null;
         }
-        /* And while we're at it, make sure both parameters lie in this
-         * century. */
-        if (!startParameter[0].startsWith("20") ||
-            !endParameter[0].startsWith("20")) {
+        if (!startParameter[0].startsWith("20")) {
           return null;
         }
-        /* Looks like sane parameters. Re-format them to get a canonical
-         * version, not something like 2010-1-1, 2010-01-1, etc. */
-        startParameter = new String[] {
-            dateFormat.format(startTimestamp) };
-        endParameter = new String[] { dateFormat.format(endTimestamp) };
-      } else {
-        /* Either none or both of start and end need to be set. */
-        return null;
+      }
+      startParameter = new String[] { dateFormat.format(startTimestamp) };
+      if (startTimestamp > endTimestamp) {
+       return null;
       }
       recognizedGraphParameters.put("start", startParameter);
       recognizedGraphParameters.put("end", endParameter);



More information about the tor-commits mailing list