1. Mikhail Korobov
  2. django-qsstats-magic

Commits

Pawel Tomasiewicz  committed 980d033

Long interval added for _fast_time_series

  • Participants
  • Parent commits af596e5
  • Branches experimental

Comments (0)

Files changed (2)

File qsstats/__init__.py

View file
                           date_field=None, aggregate=None):
         ''' Aggregate over time intervals using 1 sql query for one interval '''
 
-        if interval not in ['minutes', 'hours', 'days', 'weeks', 'months', 'years']:
-            raise InvalidInterval('Interval is not supported.')
+        num, interval = _parse_interval(interval)
+
+        if interval not in ['minutes', 'hours', 
+                            'days', 'weeks', 
+                            'months', 'years'] or num != 1:
+            raise InvalidInterval('Interval is currently not supported.')
 
         method = getattr(self, 'for_%s' % interval[:-1])
         stat_list = []
 
         def to_dt(d): # leave dates as-is
             return parse(d, yearfirst=True) if isinstance(d, basestring) else d
+
         data = dict((to_dt(item['d']), item['agg']) for item in aggregate_data)
 
         stat_list = []
         dt = start
         while dt < end:
-            value = data.get(dt, 0)
-            stat_list.append((dt, value,))
-            dt = dt + relativedelta(**{interval : 1})
+            idx = 0
+            for i in range(num):
+                if i == 0:
+                    stat_list.append([dt, data.get(dt, 0)])
+                    idx = len(stat_list) - 1
+                else:
+                    stat_list[idx][0] = dt
+                    stat_list[idx][1] = stat_list[idx][1] + data.get(dt, 0)
+
+                dt = dt + relativedelta(**{interval : 1})
+
         return stat_list
 
     # Aggregate totals using a date or datetime as a pivot

File qsstats/utils.py

View file
 
 def _parse_interval(interval):
     num = 1
-    match = re.match('(\d+)#(\w+)', interval)
+    match = re.match('(\d+)([A-Za-z]+)', interval)
+
     if match:
-        num = match.group(1)
+        num = int(match.group(1))
         interval = match.group(2)
-
     return num, interval
 
 def get_bounds(dt, interval):