Commit 6b26a267 authored by 高雅喆's avatar 高雅喆

add gray stat pyScript

parent 859d1b6a
from utils import con_sql,tuple2dict,get_yesterday_date
class GrayStat(object):
def __init__(self, cid_type, uid_type, platform,ndays=get_yesterday_date()):
"""
cid_type : diary,answer,question
uid_type : 8:_8结尾;6:_6结尾;6|8:_6或者_8结尾;^68:不是6或者8结尾的
platform : ios;android
ndays : '2018-08-30'....
"""
self.cid_type = cid_type
self.uid_type = uid_type
if platform == "ios":
self.platform = "='App Store'"
else:
self.platform = "!='App Store'"
self.ndays = ndays
def get_uid_count(self):
sql = "select count(distinct(device_id)) from data_feed_click2 \
where stat_date='{0}' \
and cid_type='{3}' \
and device_id regexp '[{1}]$' \
and device_type{2}".format(self.ndays,self.uid_type,\
self.platform.replace(' ',''),self.cid_type)
uid_count = con_sql(sql)[0][0]
return uid_count
def get_uid_clk_times(self):
sql = "select count(device_id) from data_feed_click2 \
where stat_date='{0}' \
and cid_type='{3}' \
and device_id regexp '[{1}]$' \
and device_type{2}".format(self.ndays,self.uid_type,\
self.platform.replace(' ',''),self.cid_type)
uid_clk_times = con_sql(sql)[0][0]
return uid_clk_times
def get_uid_imp_times(self):
sql = "select count(device_id) from data_feed_exposure2 \
where stat_date='{0}' \
and cid_type='{4}' \
and device_id regexp '[{1}]$' \
and device_type{2} \
and device_id in (select device_id from data_feed_click2 \
where stat_date='{0}' \
and cid_type='{4}' \
and device_id regexp '[{1}]$' \
and device_type{3})".format(self.ndays,self.uid_type,self.platform,\
self.platform.replace(' ',''),self.cid_type)
uid_imp_times = con_sql(sql)[0][0]
return uid_imp_times
if __name__ == '__main__':
#1.ffm中的灰度非灰度(ios和安卓一样):
# 灰度:_6
# 非灰度:_8
result1 = []
platforms = ['ios','android']
grays = ['6','8']
for platform in platforms:
for gray in grays:
g_class = GrayStat('diary',gray,platform)
uid_count = g_class.get_uid_count()
uid_clk_times = g_class.get_uid_clk_times()
uid_imp_times = g_class.get_uid_imp_times()
uid_clk_rate = round(uid_clk_times/uid_imp_times,4)
result1.append([g_class.ndays,g_class.cid_type,platform,gray,uid_count,\
uid_clk_times,uid_imp_times,uid_clk_rate])
result1.append([g_class.ndays,g_class.cid_type,platform,'all',\
result1[-1][4]+result1[-2][4],\
result1[-1][5]+result1[-2][5],\
result1[-1][6]+result1[-2][6],\
round((result1[-1][5]+result1[-2][5])/(result1[-1][6]+result1[-2][6]),4)])
labels = ['6','8','all']
for i in range(3):
result1.append([g_class.ndays,g_class.cid_type,'all',labels[i],\
result1[i][4]+result1[i+3][4],\
result1[i][5]+result1[i+3][5],\
result1[i][6]+result1[i+3][6],\
round((result1[i][5]+result1[i+3][5])/(result1[i][6]+result1[i+3][6]),4)])
#2.实际中的灰度非灰度:
# ios:
# 灰度:6|8
#非灰度:^68
# android:
# 灰度:0|1|2|6|8
#非灰度:^01268
result2 = []
platforms = ['ios','android']
for platform in platforms:
#TODO 对于(安卓灰度放到 0 1 2 6 8;iOS灰度保持 6 8)问题,做一个id判断即可
if platform == 'ios':
grays = ['6|8','^68']
else:
grays = ['0|1|2|6|8','^01268']
for gray in grays:
g_class = GrayStat('diary',gray,platform)
uid_count = g_class.get_uid_count()
uid_clk_times = g_class.get_uid_clk_times()
uid_imp_times = g_class.get_uid_imp_times()
uid_clk_rate = round(uid_clk_times/uid_imp_times,4)
result2.append([g_class.ndays,g_class.cid_type,platform,gray,uid_count,\
uid_clk_times,uid_imp_times,uid_clk_rate])
result2.append([g_class.ndays,g_class.cid_type,platform,'all',\
result2[-1][4]+result2[-2][4],\
result2[-1][5]+result2[-2][5],\
result2[-1][6]+result2[-2][6],\
round((result2[-1][5]+result2[-2][5])/(result2[-1][6]+result2[-2][6]),4)])
labels = ['gray','not gray','all']
for i in range(3):
result2.append([g_class.ndays,g_class.cid_type,'all',labels[i],\
result2[i][4]+result2[i+3][4],\
result2[i][5]+result2[i+3][5],\
result2[i][6]+result2[i+3][6],\
round((result2[i][5]+result2[i+3][5])/(result2[i][6]+result2[i+3][6]),4)])
# -*- coding: UTF-8 -*-
import pymysql
import datetime
def con_sql(sql):
#从数据库的表里获取数据
"""
:type sql : str
:rtype : tuple
"""
db = pymysql.connect(host='10.66.157.22', port=4000, user='root', passwd='3SYz54LS9#^9sBvC', db='jerry_test')
cursor = db.cursor()
cursor.execute(sql)
result = cursor.fetchall()
db.close()
return result
def tuple2dict(tuple_result):
#把sql结果从tuple(tuple,tuple)格式转换成dict格式
"""
:type tuple_result : tuple
:rtype : dict
"""
dict_result = {}
for i in range(len(tuple_result)):
dict_result[tuple_result[i][0]] = tuple_result[i][1]
return dict_result
def get_yesterday_date():
#自动获取昨天的日期,如"2018-08-08"
"""
:rtype : str
"""
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
yesterday = yesterday.strftime("%Y-%m-%d")
return yesterday
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