Index: ticketstats.py
===================================================================
--- ticketstats.py	(revision 13287)
+++ ticketstats.py	(working copy)
@@ -9,6 +9,7 @@
 #
 
 import re
+import datetime as datetimemodule
 from datetime import date, datetime, time, timedelta
 
 from genshi.builder import tag
@@ -96,7 +97,7 @@
 
             at_date = parse_date(grab_at_date)
             at_date = datetime.combine(at_date,
-                                       time(11, 59, 59, 0, utc))  # Add tzinfo
+                                       time(0, 0, 0, 0, utc))  # Add tzinfo
 
             from_date = parse_date(grab_from_date)
             from_date = datetime.combine(from_date,
@@ -105,8 +106,8 @@
             graph_res = int(grab_resolution)
         else:
             # default data
-            todays_date = date.today()
-            at_date = datetime.combine(todays_date, time(11, 59, 59, 0, utc))
+            todays_date = datetimemodule.datetime.utcnow()
+            at_date = datetime.combine(todays_date, time(0, 0, 0, 0, utc))
             from_date = at_date - timedelta(self.default_days_back)
             graph_res = self.default_interval
 
@@ -115,14 +116,14 @@
         db = self.env.get_db_cnx()
 
         # Calculate 0th point
-        last_date = from_date - timedelta(graph_res)
+        last_date = from_date
         last_num_open = get_num_open_tix(db, last_date, milestone)
 
         # Calculate remaining points
-        for cur_date in date_range(from_date, at_date, graph_res):
+        for cur_date in date_range(from_date + timedelta(graph_res), at_date + 2 * timedelta(graph_res), graph_res):
             num_open = get_num_open_tix(db, cur_date, milestone)
             num_closed = get_num_closed_tix(db, last_date, cur_date, milestone)
-            date_str = format_date(cur_date)
+            date_str = format_date(last_date)
             if graph_res != 1:
                 date_str = "%s thru %s" % (format_date(last_date), date_str)
             count.append({'date': date_str,
@@ -184,79 +185,74 @@
     """Returns an integer of the number of close ticket events counted
     between from_date to at_date."""
 
+    count = 0
+
+    ma_milestone_str = ""
+    if milestone is not None:
+        ma_milestone_str = " AND t.milestone = \"%s\" " % milestone
+
     cursor = db.cursor()
 
-    args = [to_timestamp(from_date), to_timestamp(at_date)]
-    milestone_str = ''
-    if milestone:
-        args.append(milestone)
-        milestone_str += 'AND t.milestone = %s'
-
-    # Count tickets between two dates (note: does not account for tickets
-    # that were closed and then reopened between the two dates)
+    # TEMP HACK
+    # this query will not get the dates completely accurate since
+    # it does not look at ticket_change, but the bug counts are accurate.
+    # it simply presumes changetime should count as the close date.
     cursor.execute("""
-        SELECT newvalue
-        FROM ticket_change tc
-        INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > %%s
-          AND tc.time <= %%s AND tc.field == 'status' %s
-        ORDER BY tc.time""" % milestone_str, args)
+        SELECT COUNT(*)
+        FROM ticket t
+        WHERE t.changetime > %s
+              AND t.changetime <= %s
+              AND t.status = 'closed' %s""" % (to_timestamp(from_date),
+                                               to_timestamp(at_date),
+                                               ma_milestone_str))
 
-    closed_count = 0
-    for (status,) in cursor:
-        if status == 'closed':
-            closed_count += 1
+    count += cursor.fetchone()[0]
 
-    return closed_count
+    return count
 
 
 def get_num_open_tix(db, at_date, milestone):
     """Returns an integer of the number of tickets currently open on that
     date."""
 
+    count = 0
+    at_date_timestamp = to_timestamp(at_date)
+    ma_milestone_str = ""
+    if milestone is not None:
+        ma_milestone_str = " AND t.milestone = \"%s\" " % milestone
+
     cursor = db.cursor()
 
-    args = [to_timestamp(at_date)]
-    milestone_str = ''
-    if milestone:
-        args.append(milestone)
-        milestone_str += 'AND t.milestone = %s'
-
-    # Count number of tickets created before specified date
+    # TEMP HACK
+    # this query will not get the dates completely accurate,
+    # but it will get the bug count accurate just like the analog
+    # in get_num_closed_tix
     cursor.execute("""
-        SELECT COUNT(*) FROM ticket t
-        WHERE t.time <= %%s %s
-        """ % milestone_str, args)
-    open_count = cursor.fetchone()[0]
+        SELECT COUNT(*) 
+        FROM ticket t
+        WHERE t.time <= %s
+            AND (t.status != 'closed'
+                 OR t.changetime > %s) %s""" % (at_date_timestamp,
+                                                at_date_timestamp,
+                                                ma_milestone_str))
 
-    # Count closed and reopened tickets
-    cursor.execute("""
-        SELECT newvalue
-        FROM ticket_change tc
-        INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > 0
-          AND tc.time <= %%s AND tc.field == 'status' %s
-        ORDER BY tc.time""" % milestone_str, args)
+    count += cursor.fetchone()[0]
 
-    for (status,) in cursor:
-        if status == 'closed':
-            open_count -= 1
-        elif status == 'reopened':
-            open_count += 1
+    return count
 
-    return open_count
 
-
 def date_range(begin, end, delta=timedelta(1)):
     """Stolen from:
     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/574441
 
-    Form a range of dates and iterate over them.  
+    Form a range of dates and iterate over them.
 
     Arguments:
     begin -- a date (or datetime) object; the beginning of the range.
     end   -- a date (or datetime) object; the end of the range.
     delta -- (optional) a timedelta object; how much to step each iteration.
              Default step is 1 day.
-             
+
     Usage:
 
     """
@@ -277,3 +273,5 @@
     while test(begin):
         yield begin
         begin += delta
+
+
