Created
May 2, 2016 02:57
-
-
Save diggzhang/8e6ad615f12de9f0d0ddae358c0c47a5 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 _*_ | |
import re | |
from pymongo import MongoClient | |
from bson.objectid import ObjectId | |
import time | |
import datetime | |
from pyqqwry.qqwry import QQWry | |
qq_wry = QQWry("./qqwry.dat") | |
# TODO: change mongo env as online configure | |
order_history_db_instance = MongoClient('10.8.8.111', 27017)['eventsV35'] | |
event_db_instance = MongoClient('10.8.8.111', 27017)['eventsV35'] | |
# order history collection | |
order_history_collection = order_history_db_instance['orderHistory'] | |
event_collection = event_db_instance['eventV35'] | |
""" | |
IP convert to location | |
""" | |
def find_location_by_ip(ip): | |
location = qq_wry.query(ip) | |
return location[1] or location[0] | |
""" | |
转码order history | |
""" | |
def parseDoc(doc, eventkey_name): | |
event_obj = { | |
"eventKey": "", | |
"eventValue": {}, | |
"category": "order", | |
"platform": "backend", | |
"ip": "", | |
"location": "", | |
"user": "", | |
"orderTime": "", | |
"serverTime": "", | |
} | |
event_obj['eventKey'] = eventkey_name | |
if 'client_ip' in doc['d']['charge']: | |
event_obj['ip'] = doc['d']['charge']['client_ip'] | |
event_obj['location'] = find_location_by_ip(event_obj['ip']) | |
else: | |
event_obj['ip'] = None | |
event_obj['location'] = None | |
if 'channel' in doc['d']['charge']: | |
event_obj['eventValue']['channel'] = doc['d']['charge']['channel'] | |
else: | |
event_obj['eventValue']['channel'] = None | |
event_obj['user'] = doc['d']['userId'] | |
event_obj['orderTime'] = doc['d']['updatedAt'] | |
event_obj['serverTime'] = doc['d']['updatedAt'] | |
return event_obj | |
""" | |
持久化 | |
""" | |
def save_eventlist_to_db(event_list, docs): | |
event_collection.insert_many(event_list) | |
""" | |
生成event | |
""" | |
def generater_events(docs, eventkey_name): | |
this_event_list = [] | |
for doc in docs: | |
this_event_list.append(parseDoc(doc, eventkey_name)) | |
save_eventlist_to_db(this_event_list, docs) | |
""" | |
订单各种状态到eventKey的映射 | |
""" | |
order_status = [ | |
["创建订单","createOrder"], | |
["等待支付","waitForPayment"], | |
["分享订单","shareOrder"], | |
["支付成功","paymentSuccess"], | |
["请求退款","refundRequest"], | |
["退款成功","refundSuccess"], | |
["支付超时","paymentTimeout"], | |
["支付关闭","paymentClose"], | |
["正在退款","refunding"], | |
["退款失败","refundFail"] | |
] | |
""" | |
Main function: | |
find the event stuff then convert to eventKey finally drop this stuff | |
""" | |
for status in order_status: | |
query_dict = {"d.status": status[0]} | |
eventkey = status[1] | |
status_list = list(order_history_collection.find(query_dict)) | |
if status_list != []: | |
print("%s %d")%(status[0], len(status_list)) | |
generater_events(status_list, eventkey) | |
order_history_collection.delete_many(query_dict) | |
else: | |
print("%s %d")%(status[0], len(status_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment