Advertisement
NamPNQ

Process data

Jan 8th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. def process_data(data, interval, from_date, to_date, week_day=0, fill_zero=False, order=None):
  2.     # FIXME: Currently influxdb group by time and fill zero not correct.
  3.     # @see: https://github.com/influxdb/influxdb/issues/1229
  4.     # @see: https://github.com/influxdb/influxdb/issues/1190
  5.     import bisect
  6.     from dateutil import rrule
  7.     from collections import Counter
  8.     from datetime import datetime
  9.  
  10.     from_date = from_date.replace(tzinfo=None)
  11.     to_date = to_date.replace(tzinfo=None)
  12.  
  13.     if interval == 'day':
  14.         grid = list(rrule.rrule(rrule.DAILY, dtstart=from_date, until=to_date))
  15.     elif interval == 'week':
  16.         grid = list(rrule.rrule(rrule.WEEKLY, dtstart=from_date, until=to_date, byweekday=week_day))
  17.     elif interval == 'month':
  18.         month_day = 1 if from_date.day == 1 else -1
  19.         grid = list(rrule.rrule(rrule.MONTHLY, dtstart=from_date, until=to_date, bymonthday=month_day))
  20.     else:
  21.         return []
  22.  
  23.     if to_date not in grid:
  24.         grid = grid + [to_date]
  25.  
  26.     cnt = Counter()
  27.     for d in data:
  28.         idx = bisect.bisect(grid, datetime.fromtimestamp(d[0]))
  29.         idx = idx-1 if idx > 0 else 0
  30.         # print d[0], datetime.fromtimestamp(d[0]), idx, grid[idx]
  31.         cnt[grid[idx]] += 1
  32.         # cnt[grid[idx]] += d[1]
  33.     rs = dict(cnt)
  34.     if fill_zero:
  35.         for d in grid[:-1]:
  36.             if d not in rs:
  37.                 rs[d] = 0
  38.     rs = rs.items()
  39.     if order == 'asc':
  40.         rs = sorted(rs, key=lambda x: x[0])
  41.     elif order == 'desc':
  42.         rs = sorted(rs, key=lambda x: x[0], reverse=True)
  43.     return rs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement