Skip to content

Instantly share code, notes, and snippets.

@ronfe
Created April 7, 2017 08:18
Show Gist options
  • Save ronfe/53e3e71a2b9a90bd1dedf4716c6f3120 to your computer and use it in GitHub Desktop.
Save ronfe/53e3e71a2b9a90bd1dedf4716c6f3120 to your computer and use it in GitHub Desktop.
from pymongo import MongoClient
conn = MongoClient("10.8.8.111:27017")
mrjing = conn["ronfedb"]["mrjing"]
topics = conn["onions"]["topics"]
class Pair(object):
def add_pv(self, v):
self.pv += v
def add_uv(self, v):
self.uv = self.uv.union(v)
def add_pair(self, pv, uv):
self.pv += pv
self.uv = self.uv.union(uv)
def output(self):
return self.pv, len(self.uv)
def __init__(self):
self.pv = 0
self.uv = set()
class Practice(object):
def __init__(self, practice_id):
pass
class Topic(object):
def add_data(self, daily_doc):
if self.has_video and "enterVideo_pv" in daily_doc:
self.events["enterVideo"].add_pair(daily_doc["enterVideo_pv"], set(daily_doc["enterVideo_uv"]))
if self.has_video and "startVideo_pv" in daily_doc:
self.events["startVideo"].add_pair(daily_doc["startVideo_pv"], set(daily_doc["startVideo_uv"]))
if self.has_video and "clickVideoExit_pv" in daily_doc:
self.events["clickVideoExit"].add_pair(daily_doc["clickVideoExit_pv"], set(daily_doc["startVideo_uv"]))
if self.has_video and "finishVideo_pv" in daily_doc:
self.events["finishVideo"].add_pair(daily_doc["finishVideo_pv"], set(daily_doc["startVideo_uv"]))
if self.has_practice:
pass
def __repr__(self):
return self.name
def __init__(self, topic_doc):
self.has_video = True
self.has_practice = True
self.id = str(topic_doc["_id"])
self.name = topic_doc["name"]
self.payable = topic_doc["pay"]
self.type = topic_doc["type"]
self.events = {
"enterVideo": Pair(),
"startVideo": Pair(),
"clickVideoExit": Pair(),
"finishVideo": Pair(),
"startPractice": Pair(),
"clickProblemSubmit": Pair(),
"clickProblemExit": Pair(),
"enterPracticeFailure": Pair(),
"enterTopicFinish": Pair()
}
if "hyperVideo" not in topic_doc:
self.has_video = False
else:
self.hv = topic_doc["hyperVideo"]
if "practice" not in topic_doc:
self.has_practice = False
else:
self.practice = Practice(topic_doc["practice"])
topic_cursor = topics.find({}, {"name": 1, "pay": 1, "type": 1, "hyperVideo": 1, "practice": 1})
topic_result = dict()
for doc in topic_cursor:
topic_result[str(doc["_id"])] = Topic(doc)
x = mrjing.find({"date": {"$gte": "20170109", "$lte": "20170130"}})
for doc in x:
str_id = str(doc["topicId"])
if str_id not in topic_result:
continue
cur_topic = topic_result[str_id]
cur_topic.add_data(doc)
x = cur_topic.events["enterVideo"].output()
x2 = cur_topic.events["startVideo"].output()
print(""
"{} : enterVideo - pv {} , uv {} \n "
"startVideo - pv {} , uv {} \n ".format(cur_topic.name, x[0], x[1], x2[0], x2[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment