Commit 9e38af68 authored by ale's avatar ale
Browse files

have a single function to serialize unix timestamps as redis keys

parent c756b263
...@@ -4,6 +4,10 @@ import redis ...@@ -4,6 +4,10 @@ import redis
import time import time
def time2day(unix_stamp):
return time.strftime('%Y%m%d', time.gmtime(unix_stamp))
class Accounting(object): class Accounting(object):
def __init__(self, local_db, aggr_db=None): def __init__(self, local_db, aggr_db=None):
...@@ -44,7 +48,7 @@ class Accounting(object): ...@@ -44,7 +48,7 @@ class Accounting(object):
for data in conns: for data in conns:
pipe.lpush('connections:%s' % cn, data) pipe.lpush('connections:%s' % cn, data)
conn_info = json.loads(data) conn_info = json.loads(data)
day = time.strftime('%Y%m%d', time.gmtime(conn_info['end_time'])) day = time2day(conn_info['end_time'])
aggr_day = aggr.setdefault(day, {'conn_time': 0, aggr_day = aggr.setdefault(day, {'conn_time': 0,
'bytes_sent': 0, 'bytes_sent': 0,
'bytes_recv': 0}) 'bytes_recv': 0})
...@@ -98,7 +102,7 @@ class Accounting(object): ...@@ -98,7 +102,7 @@ class Accounting(object):
def get_aggregate_counts(self, cn, when=None): def get_aggregate_counts(self, cn, when=None):
if not when: if not when:
when = time.time() when = time.time()
day = time.strftime('%Y%m%d', time.gmtime(when)) day = time2day(when)
data = self._aggr_db.hget('aggr:%s' % cn, day) data = self._aggr_db.hget('aggr:%s' % cn, day)
if data: if data:
return json.loads(data) return json.loads(data)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment