Created
May 11, 2016 03:13
-
-
Save ronfe/6a08bd1b8b69ae6767b0cfa197c0cffa to your computer and use it in GitHub Desktop.
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 _*_ | |
from pymongo import MongoClient | |
import datetime | |
from bson.code import Code | |
# 上线时间 | |
START = datetime.datetime(2016, 5, 8, 16) | |
END = datetime.datetime.now() | |
WEEK_DIFF = datetime.timedelta(weeks = 1) | |
users = MongoClient('10.8.8.111:27017')['onions']['users'] | |
events = MongoClient('10.8.8.111:27017')['eventsV35']['eventV35'] | |
# STEP 1 Get switch state users | |
in_users = users.distinct("_id", {"allowed": True}) | |
off_users = users.distinct("_id", {"allowed": False}) | |
# get partialOpen | |
pipeline = [ | |
{"$match": {"eventKey": "getProfile" ,"user": {"$in": off_users}}}, | |
{"$sort": {"eventTime": 1}}, | |
{"$group": {"_id": "$user", "isPartial": {"$last": "$eventValue.partialOpen"}}} | |
] | |
partial_users = [] | |
x = list(events.aggregate(pipeline)) | |
for each in x: | |
if each['isPartial']: | |
partial_users.append(each["_id"]) | |
# STEP 2 Generate user activate dict | |
# {"ENDDATE": {"on": list, "partial": list}} | |
user_act_mat = {} | |
calc_date = START | |
while calc_date <= END: | |
# insert item into matrix | |
date_string = ''.join([str(calc_date.month), "-", str(calc_date.day)]) | |
user_act_mat[date_string] = {"on": [], "partial": []} | |
# get in_users | |
pipeline = [ | |
{"$match": {"user": {"$in": in_users}, "serverTime": {"$gte": calc_date, "$lt": calc_date + WEEK_DIFF}}}, | |
{"$group": {"_id": None, "activateUsers": {"$addToSet": "$user"}}} | |
] | |
x = events.aggregate(pipeline) | |
user_act_mat[date_string]['on'] = list(x)[0]['activateUsers'] | |
# get partial_users | |
pipeline[0]['$match']['user']['$in'] = partial_users | |
x = events.aggregate(pipeline) | |
user_act_mat[date_string]['partial'] = list(x)[0]['activateUsers'] | |
calc_date += WEEK_DIFF | |
# STEP 3 Output | |
f = open('./output.csv', 'w+') | |
# Header | |
f.write('Week Start,Switched Users,Partial Users\n') | |
f.write('0,' + str(len(in_users)) + ',' + str(len(partial_users))) | |
f.write('\n') | |
# Content | |
for k, v in user_act_mat.iteritems(): | |
cur_row = ','.join([k, str(len(v['on'])), str(len(v['partial'])), '\n']) | |
f.write(cur_row) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment