Last active
April 28, 2016 03:23
-
-
Save diggzhang/87218a5c42c5e847d7698b0c335edc06 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 _*_ | |
""" | |
s0. dump fully event collection by getProfile 得出所有登录过的注册用户的埋点 | |
s1. run this programm | |
""" | |
from pymongo import MongoClient | |
from bson.objectid import ObjectId | |
print("[REMEMBER] Dump fully event collection by getProfile") | |
""" | |
DB connection configure | |
""" | |
event_db_instance = MongoClient('10.8.8.111', 27017)['eventsV35'] | |
# the getProfile dump db | |
event_collection = event_db_instance['userLocationCacheEvent'] | |
location_collection = event_db_instance['userLocation'] | |
""" | |
count user id that have been count | |
""" | |
# user_id_counted_location = [] | |
# for user in location_collection.find({}): | |
# user_id_counted_location.append(user['_id']) | |
# print("%d users was recorded location")%(len(user_id_counted_location)) | |
""" | |
mongo aggregate pipeline | |
use API GET /me eventKey:getProfile | |
Returns: | |
_id : user ObjectId | |
role : user role enum['teacher','student','visitor','editor'] | |
channel : user came from which channel | |
location : user location | |
Raises: | |
empty list : active_user_id_list == [] | |
""" | |
pipeline = [ | |
{"$match": { | |
"eventKey": "getProfile" | |
}}, | |
{"$group": { | |
"_id": "$user", | |
"role": {"$last":"$role"}, | |
"channel": {"$last":"$channel"}, | |
"location": {"$first": "$location"} | |
}} | |
] | |
active_user_id_list = list(event_collection.aggregate(pipeline)) | |
""" | |
save to db | |
""" | |
location_collection.delete_many({"_id": {"$exists": True}}) | |
location_collection.insert_many(active_user_id_list) | |
print("%s users cached with filed location/role/channel")%len(active_user_id_list) |
Author
diggzhang
commented
Apr 28, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment