Commit 035ea575 authored by 高雅喆's avatar 高雅喆

add a new class getCidRate.py

parent 716da1d4
# -*- coding: UTF-8 -*-
from utils import con_sql
class CidRate(object):
def __init__(self, ndays, platform, cid_type):
"""
ndays : 1;2;3;4.. #The number of days from the current time
platform : 'all';'ios';'android'
cid_type : 'diary';'answer';'question'...
"""
self.ndays = ndays
if platform == "ios":
self.platform = "='AppStore'"
elif platform == "android":
self.platform = "!='AppStore'"
else:
self.platform = " is not null"
self.cid_type = cid_type
def get_cid_clk_rate(self, platform):
"""
platform : "所有";"苹果","安卓" #方便显示
rtype : list
"""
if self.platform[-2] == 'e':
self.platform = self.platform.replace(' ','')
sql_cid = "select count(cid) from data_feed_click \
where from_unixtime(time,'%Y-%m-%d')=date_add(curdate(), interval -{0} day) \
and device_type{1} and cid_type='{2}'".format(self.ndays,self.platform,self.cid_type)
cid_clk_count = con_sql(sql_cid)[0][0]
sql_all = "select count(cid) from data_feed_click \
where from_unixtime(time,'%Y-%m-%d')=date_add(curdate(), interval -{0} day) \
and device_type{1}".format(self.ndays, self.platform)
all_clk_count = con_sql(sql_all)[0][0]
cid_clk_rate = round(cid_clk_count/all_clk_count,4)
return [platform,cid_clk_count,all_clk_count,cid_clk_rate]
def get_cid_imp_rate(self, platform):
"""
platform : "所有";"苹果","安卓" #方便显示
rtype : list
"""
if self.platform[-2] == 'e':#注意:曝光表中AppStore有空格
self.platform = self.platform[:-6] + ' ' + self.platform[-6:]
sql_cid = "select count(cid) from data_feed_exposure \
where from_unixtime(time,'%Y-%m-%d')=date_add(curdate(), interval -{0} day) \
and device_type{1} and cid_type='{2}'".format(self.ndays,self.platform,self.cid_type)
cid_imp_count = con_sql(sql_cid)[0][0]
sql_all = "select count(cid) from data_feed_exposure \
where from_unixtime(time,'%Y-%m-%d')=date_add(curdate(), interval -{0} day) \
and device_type{1}".format(self.ndays, self.platform)
all_imp_count = con_sql(sql_all)[0][0]
cid_imp_rate = round(cid_imp_count/all_imp_count,4)
return [platform,cid_imp_count,all_imp_count,cid_imp_rate]
def main():
answer_rate_all = CidRate(1,"all","answer").get_cid_imp_rate("所有")
answer_rate_ios = CidRate(1,"ios","answer").get_cid_imp_rate("苹果")
answer_rate_android = CidRate(1,"android","answer").get_cid_imp_rate("安卓")
answer_rate_result = [answer_rate_all,answer_rate_ios,answer_rate_android]
if __name__ == '__main__':
main()
# -*- coding: UTF-8 -*-
from utils import con_sql,tuple2dict,get_yesterday_date
from config import DIRECTORY_PATH
from utils import con_sql
class ClkCidUidRate(object):
......@@ -8,7 +7,7 @@ class ClkCidUidRate(object):
"""
ndays : 1;2;3;4.. #The number of days from the current time
platform : 'all';'ios';'android'
cid_type : 'diary';'answer';'question';"nothing"...
cid_type : 'diary';'answer';'question';"everything"...
"""
self.ndays = ndays
if platform == "ios":
......@@ -17,7 +16,7 @@ class ClkCidUidRate(object):
self.platform = "!='AppStore'"
else:
self.platform = " is not null"
if cid_type == "nothing":
if cid_type == "everything":
self.cid_type = " is not null"
else:
self.cid_type = "='" + cid_type + "'"
......@@ -43,11 +42,8 @@ class ClkCidUidRate(object):
and cid_type{2}".format(self.ndays,self.platform,self.cid_type)
imp_count = con_sql(sql_imp)[0][0]
if self.cid_type == " is not null":
no_clk_count = imp_count - clk_count
return [platform,no_clk_count,imp_count,round(no_clk_count/imp_count,4)]
else:
return [platform,clk_count,imp_count,round(clk_count/imp_count,4)]
clk_rate = round(clk_count/imp_count,4)
return [platform,clk_count,imp_count,clk_rate]
def result2file(self, result_lst, fpath):
......@@ -74,12 +70,12 @@ def main():
#click_question_android = ClkCidUidRate(1,"android","question").get_clk_cid_uid_rate("安卓")
#click_question_result = [click_question_all,click_question_ios,click_question_android]
#print("已获取点击question用户占比")
#4.点击用户占比
click_nothing_all = ClkCidUidRate(1,"all","nothing").get_clk_cid_uid_rate("所有")
click_nothing_ios = ClkCidUidRate(1,"ios","nothing").get_clk_cid_uid_rate("苹果")
click_nothing_android = ClkCidUidRate(1,"android","nothing").get_clk_cid_uid_rate("安卓")
click_nothing_result = [click_nothing_all,click_nothing_ios,click_nothing_android]
print("已获取点击用户占比")
#4.点击用户占比
click_everything_all = ClkCidUidRate(1,"all","everything").get_clk_cid_uid_rate("所有")
click_everything_ios = ClkCidUidRate(1,"ios","everything").get_clk_cid_uid_rate("苹果")
click_everything_android = ClkCidUidRate(1,"android","everything").get_clk_cid_uid_rate("安卓")
click_everything_result = [click_everything_all,click_everything_ios,click_everything_android]
print("已获取点击用户占比")
......
......@@ -45,9 +45,10 @@ class TopFeatures(object):
imp_times = tuple2dict(con_sql(sql))
return imp_times
def get_result(self, platform, clk={}, imp={}, clk_n=2, result_types="ctr"):
def get_result(self, platform, cid_type, clk={}, imp={}, clk_n=2, result_types="ctr"):
"""
platform : "所有";"苹果","安卓" #方便显示
cid_type : 'diary';'answer';'question';"everything"... #方便显示
clk : dict
imp : dict
clk_n : 获取topN点击率时,过滤的点击数
......@@ -87,8 +88,9 @@ class TopFeatures(object):
topn.sort(key=lambda x:x[4],reverse=True)
return topn[:int(self.top_n)]
def result2file(self, result_lst, fpath):
def result2file(self, cid_type, result_lst, fpath):
"""
cid_type : 'diary';'answer';'question';"everything"... #方便显示
result_lst : [all_result,ios_result,android_result]
fpath : output filename
rtype : none
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment