--- 0.11/ticketstats-plugin/ticketstats/ticketstats.py	2010-11-22 18:42:37.368134685 -0800
+++ 0.11/ticketstats-plugin/ticketstats/ticketstats.py.new	2010-11-22 18:44:38.408091867 -0800
@@ -26,7 +26,8 @@
 
 # ************************
 DEFAULT_DAYS_BACK = 30*6 
-DEFAULT_INTERVAL = 30
+#DEFAULT_INTERVAL = 30
+DEFAULT_INTERVAL = 7
 # ************************
 
 class TicketStatsPlugin(Component):
@@ -45,6 +46,9 @@
          doc='Number of days between each data point'\
             ' (resolution) by default')
 
+   def __init__(self):
+       self.count = []
+            
    # ==[ INavigationContributor methods ]==
 
    def get_active_navigation_item(self, req):
@@ -80,12 +84,22 @@
       cursor = db.cursor()
    
       # TODO clean up this query
-      cursor.execute("SELECT t.id, tc.field, tc.time, tc.oldvalue, tc.newvalue, t.priority FROM ticket_change tc, enum p INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > %s AND tc.time <= %s WHERE p.name = t.priority AND p.type = 'priority' %s ORDER BY tc.time" % (to_timestamp(from_date), to_timestamp(at_date), ma_milestone_str))
+      #cursor.execute("SELECT t.id, tc.field, tc.time, tc.oldvalue, tc.newvalue, t.priority "
+      #   "FROM ticket_change tc, enum p INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > %s AND tc.time <= %s "
+      #   "WHERE p.name = t.priority AND p.type = 'priority' %s "
+      #   "ORDER BY tc.time" % (to_timestamp(from_date), to_timestamp(at_date), ma_milestone_str))
+      cursor.execute("SELECT t.id, tc.field, tc.time, tc.oldvalue, tc.newvalue, t.priority "
+         "FROM ticket_change tc, enum p INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > %s AND tc.time <= %s "
+         "WHERE p.name = t.priority AND p.type = 'priority' %s AND tc.field = 'status' "
+         "AND tc.newvalue in ('new', 'assigned', 'reopened', 'closed', 'edit') "
+         "ORDER BY tc.time" % (to_timestamp(from_date), to_timestamp(at_date), ma_milestone_str))
 
       for id, field, time, old, status, priority in cursor:
-         if field == 'status':
-            if status in ('new', 'assigned', 'reopened', 'closed', 'edit'):
-               count+=status_map[status]
+         #if field == 'status':
+         #   if status in ('new', 'assigned', 'reopened', 'closed', 'edit'):
+         #      count+=status_map[status]
+         count+=status_map[status]
+      #self.log.debug('closed tix count=%d)' % count)
 
       return count
 
@@ -111,25 +125,40 @@
       cursor = db.cursor()
    
       # TODO clean up this query
-      cursor.execute("SELECT t.type AS type, owner, status, time AS created FROM ticket t, enum p WHERE p.name = t.priority AND p.type = 'priority' AND created <= %s %s" % (to_timestamp(at_date), ma_milestone_str))
+      cursor.execute("SELECT COUNT(*) FROM ( "
+          "SELECT t.type AS type, owner, status, time AS created FROM ticket t, enum p "
+          "WHERE p.name = t.priority AND p.type = 'priority' "
+          "AND created <= %s %s )" % (to_timestamp(at_date), ma_milestone_str))
 
       for rows in cursor:
-         count += 1
+         #count += 1
+         count += rows[0]
+      #self.log.debug('open tix count=%d)' % count)
 
       # TODO clean up this query
-      cursor.execute("SELECT t.id, tc.field, tc.time, tc.oldvalue, tc.newvalue, t.priority FROM ticket_change tc, enum p INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > 0 AND tc.time <= %s WHERE p.name = t.priority AND p.type = 'priority' %s ORDER BY tc.time" % (to_timestamp(at_date), ma_milestone_str))
+      #cursor.execute("SELECT t.id, tc.field, tc.time, tc.oldvalue, tc.newvalue, t.priority "
+      #"FROM ticket_change tc, enum p INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > 0 AND tc.time <= %s "
+      #"WHERE p.name = t.priority AND p.type = 'priority' %s "
+      #"ORDER BY tc.time" % (to_timestamp(at_date), ma_milestone_str))
+      cursor.execute("SELECT t.id, tc.field, tc.time, tc.oldvalue, tc.newvalue, t.priority "
+          "FROM ticket_change tc, enum p INNER JOIN ticket t ON t.id = tc.ticket AND tc.time > 0 AND tc.time <= %s "
+          "WHERE p.name = t.priority AND p.type = 'priority' %s AND tc.field = 'status' "
+          "AND tc.newvalue in ('new', 'assigned', 'reopened', 'closed', 'edit') "
+          "ORDER BY tc.time" % (to_timestamp(at_date), ma_milestone_str))
 
       for id, field, time, old, status, priority in cursor:
-         if field == 'status':
-            if status in ('new', 'assigned', 'reopened', 'closed', 'edit'):
-               count+=status_map[status]
+         #if field == 'status':
+         #   if status in ('new', 'assigned', 'reopened', 'closed', 'edit'):
+         #      count+=status_map[status]
+         count+=status_map[status]
+      #self.log.debug('open2 tix count=%d)' % count)
 
       return count
 
    # ==[ IRequestHandle methods ]==
 
    def match_request(self, req):
-      return re.match(r'/ticketstats(?:_trac)?(?:/.*)?$', req.path_info)
+      return re.match(r'/ticketstats(?:_trac)?(?:/.*)?$', req.path_info) #' to make code-navigator happy
 
    def process_request(self, req):
       req_content = req.args.get('content')
@@ -189,36 +218,39 @@
          from_date = datetime(*strptime(from_date_str, "%m/%d/%Y")[0:6])
          from_date = datetime.combine(from_date, time(0,0,0,0,utc)) # Add tzinfo
          
-      count = []
-
-      # Calculate 0th point 
-      last_date = from_date - timedelta(graph_res)
-      last_num_open = self._get_num_open_tix(last_date, milestone, req)
-
-      # Calculate remaining points
-      for cur_date in daterange(from_date, at_date, graph_res):
-         num_open = self._get_num_open_tix(cur_date, milestone, req)
-         num_closed = self._get_num_closed_tix(last_date, cur_date, milestone, req)
-         datestr = cur_date.strftime("%m/%d/%Y") 
-         if graph_res != 1:
-            datestr = "%s thru %s" % (last_date.strftime("%m/%d/%Y"), datestr) 
-         count.append( {'date'  : datestr,
-                   'new'   : num_open - last_num_open + num_closed,
-                   'closed': num_closed,
-                   'open'  : num_open })
-         last_num_open = num_open
-         last_date = cur_date
+      #count = []
+      if len(self.count) == 0:
+          # Calculate 0th point 
+          last_date = from_date - timedelta(graph_res)
+          last_num_open = self._get_num_open_tix(last_date, milestone, req)
+          self.log.debug('last_num_open tix count=%d)' % last_num_open)
+    
+          # Calculate remaining points
+          for cur_date in daterange(from_date, at_date, graph_res):
+             num_open = self._get_num_open_tix(cur_date, milestone, req)
+             num_closed = self._get_num_closed_tix(last_date, cur_date, milestone, req)
+             datestr = cur_date.strftime("%m/%d/%Y") 
+             if graph_res != 1:
+                datestr = "%s thru %s" % (last_date.strftime("%m/%d/%Y"), datestr) 
+             self.count.append( {'date'  : datestr,
+                       'new'   : num_open - last_num_open + num_closed,
+                       'closed': num_closed,
+                       'open'  : num_open })
+             last_num_open = num_open
+             last_date = cur_date
 
       # if chartdata is requested, raw text is returned rather than data object
       # for templating
       if (not req_content == None) and (req_content == "chartdata"):
          jsdstr = '{"chartdata": [\n'
-         for x in count:
+         for x in self.count:
             jsdstr += '{"date": "%s",' % x['date']
             jsdstr += ' "new_tickets": %s,' % x['new']
             jsdstr += ' "closed": %s,' % x['closed']
             jsdstr += ' "open": %s},\n' % x['open']
          jsdstr = jsdstr[:-2] +'\n]}'
+         self.log.debug('jsdstr=%s)' % jsdstr)
+         self.count = []      #done with count. reset for next update request
          req.write(jsdstr)
          return 
       else:
@@ -233,13 +265,14 @@
             milestone_num = 0
 
          data = {}
-         data['ticket_data'] = count
+         data['ticket_data'] = self.count
          data['start_date'] = from_date.strftime("%m/%d/%Y")
          data['end_date'] = at_date.strftime("%m/%d/%Y")
          data['resolution'] = str(graph_res)
          data['baseurl'] = req.base_url
          data['milestones'] = milestone_list
          data['cmilestone'] = milestone_num
+         self.log.debug('data[ticket_data]=%s)' % data['ticket_data'])
          return 'greensauce.html', data, None
  
    def get_htdocs_dirs(self):
