Created
December 28, 2014 15:36
-
-
Save snowleung/02e3090301af2c0edae3 to your computer and use it in GitHub Desktop.
devices_analytic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
import logging | |
import json | |
from datetime import datetime, timedelta | |
import time | |
def _analytic(passed_days, active_daily, active_hourly): | |
''' | |
: return : f4 | |
''' | |
# [{'mac':xx, year:xx, month:xx, day:xx, hour:xx, minute:xx, second:xx, timestamp:xx}, ...] mongo_hourly | |
# [{'mac':xx, year:xx, month:xx, day:xx, timestamp:xx}, ...] mongo_daily | |
# {(year,month,day):{0:c, 1:c, ...}, ...} f1 | |
# {(year,month,day):{0:c/tot,...},...} f2 | |
# {0:d1h0+d2h0...,1:d1h1+d2h1} f3 | |
# {0:d1h0+d2h0+.../avg_total, 1:d2h1+d2h1+.../avg_total} f4 | |
now = datetime.now() | |
now = now - timedelta(hours = now.hour) | |
now = now - timedelta(minutes = now.minute) | |
_passed_days = [now - timedelta(days=i) for i in range(passed_days)] | |
def f(x,y): | |
key = (y['year'], y['month'], y['day']) | |
x[key] = x.get(key, dict.fromkeys(range(24), 0)) | |
x[key][y['hour']]= x[key].get(y['hour'], 0) + 1 | |
return x | |
f1 = reduce(f, active_hourly, {}) | |
logging.debug( '========={(year,month,day):{0:c, 1:c, ...}, ...} f1=========') | |
logging.debug( f1) | |
def _f2(f1, dailys, days): | |
for day in days: | |
tot_total = len([i for i in dailys if i['day'] == day.day and i['month']==day.month and i['year']==day.year]) | |
if tot_total == 0: | |
continue | |
tot_total = float(tot_total) | |
key = (day.year, day.month, day.day) | |
n = {} | |
for hour,count in f1[key].items(): | |
n[hour] = count/tot_total | |
f1[key] = n | |
return f1 | |
f2 = _f2(f1, list(active_daily), _passed_days) | |
logging.debug( '========={(year,month,day):{0:c/tot,...},...}f2=========') | |
logging.debug( f2) | |
avg_total = len(f2.keys()) | |
_init =dict.fromkeys(range(24), 0) | |
def _f3(x,y): | |
for k,v in y.items(): | |
x[k] = x[k] + v | |
return x | |
f3 = reduce(_f3, f2.values(), _init) | |
logging.debug( '========={0:d1h0+d2h0...,1:d1h1+d2h1}f3=========') | |
logging.debug( f3) | |
def _f4(_f3, _avg_total): | |
for hour, _sum in _f3.items(): | |
_f3[hour] = _sum/_avg_total | |
return _f3 | |
f4 = _f4(f3, avg_total) | |
logging.debug( '========={0:d1h0+d2h0+.../avg_total, 1:d2h1+d2h1+.../avg_total}f4=========') | |
logging.debug( f4) | |
return f4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment