diff --git a/daily_report/dreport.py b/daily_report/dreport.py index 6ddeecd..21ea05b 100644 --- a/daily_report/dreport.py +++ b/daily_report/dreport.py @@ -11,6 +11,7 @@ import logging import datetime define_logger("/data/logs/ops/daily_report.log") import pdb +from ops.mp_interface import MpInterface log = logging.getLogger(__name__) sender = "ops@kingsome.cn" @@ -26,7 +27,8 @@ mail = Mail(app) recipients = ["pengtao@kingsome.cn"] -FROMAPPID_CN = {"wxdb103a128e118619": "拯救熊猫泡泡", "wxc137c93eedeab6f2": "爆冰达人"} + +# FROMAPPID_CN = {"wxdb103a128e118619": "拯救熊猫泡泡", "wxc137c93eedeab6f2": "爆冰达人"} def send_async_email(app, msg): @@ -42,9 +44,10 @@ def send_dailyreport(): gameid = request.args.get('gameid') channelid = request.args.get('channelid') day = request.args.get('day') + project = request.args.get('project') or 'mini_games' if not (gameid and channelid and day): return jsonify("PLS input arfs") - rp = Report(gameid, channelid, day) + rp = Report(gameid, channelid, day, project) data = rp.run() data[gameid] = gameid data[channelid] = channelid @@ -59,17 +62,10 @@ def send_dailyreport(): class Report: - def __init__(self, gameid, channelid, day): - self.gameid = gameid - self.channelid = channelid + def __init__(self, day, project): self.day = day - g = GetTgaConfig() - item = g.get_api_key(self.gameid) - print(f"1={item} 2={gameid}3={day}") - self.url = item['url'] - self.suffix = item.get('suffix', 0) - self.api_key = item.get('api_key', None) - self.tga = FromTga(self.url, self.api_key) + self.project = project + def get_data(self): @@ -80,46 +76,46 @@ class Report: data[fromappid] = (activa, new, share, k) - def get_all_data(self): + def get_all_data(self, args): activa_sql = f"""SELECT count(distinct "#account_id") FROM - v_event_{self.suffix} + v_event_{args['suffix']} where - gameid='{self.gameid}' - and channel='{self.channelid}' + gameid='{args['gameid']}' + and channel='{args['channelid']}' and "$part_event"='event_11_1' and "$part_date"='{self.day}'""" new_sql = f"""SELECT count(distinct "#account_id") FROM - v_event_{self.suffix} + v_event_{args['suffix']} where - gameid='{self.gameid}' - and channel='{self.channelid}' + gameid='{args['gameid']}' + and channel='{args['channelid']}' and "$part_event"='event_11_1' and account_register_date between timestamp'{self.day} 00:00:00' and timestamp'{self.day} 23:59:59'""" share_sql = f"""SELECT count(distinct \"#account_id\") FROM - v_event_{self.suffix} + v_event_{args['suffix']} where "$part_event"='event_11_10' - and gameid='{self.gameid}' - and channel='{self.channelid}' + and gameid='{args['gameid']}' + and channel='{args['channelid']}' and "$part_event"='event_11_1' and "$part_date"='{self.day}'""" # byshare_sql = f"""SELECT # count(distinct \"#account_id\") # FROM - # v_event_{self.suffix} + # v_event_{args['suffix']} # where # "$part_event"='event_11_11' - # and gameid='{self.gameid}' - # and channel='{self.channelid}' + # and gameid='{args['gameid']}' + # and channel='{args['channelid']}' # and "$part_event"='event_11_1' # and "$part_date"='{self.day}'""" @@ -129,39 +125,39 @@ class Report: v_event_19 where "$part_event"='event_21_2' - and gameid='{self.gameid}' + and gameid='{args['gameid']}' and "$part_date"='{self.day}' """ ad_101_sql = f"""SELECT count(1) FROM - v_event_{self.suffix} + v_event_{args['suffix']} WHERE "$part_event"='event_11_21' - and gameid='{self.gameid}' - and "channel"='{self.channelid}' + and gameid='{args['gameid']}' + and "channel"='{args['channelid']}' AND activity_id=101 and "$part_date"='{self.day}' """ ad_1_sql = f"""SELECT count(1) FROM - v_event_{self.suffix} + v_event_{args['suffix']} WHERE "$part_event"='event_11_21' - and gameid='{self.gameid}' - and "channel"='{self.channelid}' + and gameid='{args['gameid']}' + and "channel"='{args['channelid']}' AND activity_id=1 and "$part_date"='{self.day}' """ ad_201_sql = f"""SELECT count(1) FROM - v_event_{self.suffix} + v_event_{args['suffix']} WHERE "$part_event"='event_11_21' - and gameid='{self.gameid}' - and "channel"='{self.channelid}' + and gameid='{args['gameid']}' + and "channel"='{args['channelid']}' AND activity_id=201 and "$part_date"='{self.day}' """ jumpout_sql = f"""SELECT @@ -173,82 +169,82 @@ class Report: and "$part_date"='{self.day}' and "jump_result"=1 """ try: - activa = self.tga.get_data(activa_sql)[0][0] - new = self.tga.get_data(new_sql)[0][0] - share = self.tga.get_data(share_sql)[0][0] - timeonlie = self.tga.get_data(timeonlie_sql)[0][0] - ad_101 = self.tga.get_data(ad_101_sql)[0][0] - ad_1 = self.tga.get_data(ad_1_sql)[0][0] - ad_201 = self.tga.get_data(ad_201_sql)[0][0] - jumpout = self.tga.get_data(jumpout_sql)[0][0] + activa = args['tga'].get_data(activa_sql)[0][0] + new = args['tga'].get_data(new_sql)[0][0] + share = args['tga'].get_data(share_sql)[0][0] + timeonlie = args['tga'].get_data(timeonlie_sql)[0][0] + ad_101 = args['tga'].get_data(ad_101_sql)[0][0] + ad_1 = args['tga'].get_data(ad_1_sql)[0][0] + ad_201 = args['tga'].get_data(ad_201_sql)[0][0] + jumpout = args['tga'].get_data(jumpout_sql)[0][0] jump_per = (100 * jumpout) / activa return [activa, new, share, timeonlie, ad_1, ad_101, ad_201, jump_per] except Exception: - log.error(f"get data from tga failed ,{self.gameid}", exc_info=True) + log.error(f"get data from tga failed ,{args['gameid']}", exc_info=True) return None - def get_input_fromappid(self, fromappid): + def get_input_fromappid(self, args): activa_sql = f"""SELECT count(distinct "#account_id") FROM - v_event_{self.suffix} + v_event_{args['suffix']} where - gameid='{self.gameid}' - and channel='{self.channelid}' + gameid='{args['gameid']}' + and channel='{args['channelid']}' and "$part_event"='event_11_1' and "$part_date"='{self.day}' - and from_appid='{fromappid}'""" + and from_appid='{args['fromappid']}'""" new_sql = f"""SELECT count(distinct "#account_id") FROM - v_event_{self.suffix} + v_event_{args['suffix']} where - gameid='{self.gameid}' - and channel='{self.channelid}' + gameid='{args['gameid']}' + and channel='{args['channelid']}' and "$part_event"='event_11_1' and account_register_date between timestamp'{self.day} 00:00:00' and timestamp'{self.day} 23:59:59' - and from_appid='{fromappid}' """ + and from_appid='{args['fromappid']}' """ share_sql = f"""SELECT count(distinct \"#account_id\") FROM - v_event_{self.suffix} + v_event_{args['suffix']} where "$part_event"='event_11_10' - and gameid='{self.gameid}' - and channel='{self.channelid}' + and gameid='{args['gameid']}' + and channel='{args['channelid']}' and "$part_event"='event_11_1' and "$part_date"='{self.day}' - and from_appid='{fromappid}' """ + and from_appid='{args['fromappid']}' """ byshare_sql = f"""SELECT count(distinct \"#account_id\") FROM - v_event_{self.suffix} + v_event_{args['suffix']} where "$part_event"='event_11_11' - and gameid='{self.gameid}' - and channel='{self.channelid}' + and gameid='{args['gameid']}' + and channel='{args['channelid']}' and "$part_event"='event_11_1' and "$part_date"='{self.day}' - and from_appid='{fromappid}' """ + and from_appid='{args['fromappid']}' """ try: - activa = self.tga.get_data(activa_sql)[0][0] or 0 - new = self.tga.get_data(new_sql)[0][0] or 0 - share = self.tga.get_data(share_sql)[0][0] or 0 - byshare = self.tga.get_data(byshare_sql)[0][0] or 0 + activa = args['tga'].get_data(activa_sql)[0][0] or 0 + new = args['tga'].get_data(new_sql)[0][0] or 0 + share = args['tga'].get_data(share_sql)[0][0] or 0 + byshare = args['tga'].get_data(byshare_sql)[0][0] or 0 if activa == 0: k = 0 else: k = (100 * byshare / (activa - byshare)) - return (fromappid, FROMAPPID_CN.get(fromappid, None), activa, new, share, k) + return (args['fromappid'], args['fromappid_cn'], activa, new, share, k) except Exception: - log.error(f"collect input failed {self.gameid} {fromappid}", exc_info=True) + log.error(f"collect input failed {args['gameid']} {args['fromappid']}", exc_info=True) return None - def get_output_fromappid(self, jump_appid): + def get_output_fromappid(self, args): jump_sql = f"""SELECT count("#account_id"),count(distinct "#account_id") FROM @@ -256,49 +252,72 @@ class Report: where "$part_event"='event_1_4' and "$part_date"='{self.day}' - and "jump_appid"='{jump_appid}' + and "jump_appid"='{args['fromappid']}' and "jump_result"=1 """ - data = self.tga.get_data(jump_sql) + data = args['tga'].get_data(jump_sql) if data: try: jump_num, jump_pre = data[0] - return (jump_appid, FROMAPPID_CN.get(jump_appid, None), jump_num, jump_pre) + return (args['fromappid'], args['fromappid_cn'], jump_num, jump_pre) except Exception: - log.error(f"get data from output by {self.gameid} {jump_appid} failed", exc_info=True) + log.error(f"get data from output by {args['gameid']} {args['fromappid']} failed", exc_info=True) return None else: return None def run(self): + if self.project == 'mini_games': + parms = [(1004, 6001), (2001, 6001), (1001, 6001)] + else: + parms = [(2001, 6001), (2002, 6001)] + + data = dict() + data['day'] = self.day - data['channelid'] = self.channelid - data['gameid'] = self.gameid - data['all'] = self.get_all_data() - data['input'] = defaultdict(list) - data['output'] = defaultdict(list) - data['input'] = [] - data['output'] = [] - fromappids = FROMAPPID_CN.keys() - for fromappid in fromappids: - data['input'].append(self.get_input_fromappid(fromappid)) - data['output'].append(self.get_output_fromappid(fromappid)) - print(data) - return data + for item in parms: + args = {} + args['gameid'], args['channelid'] = item + key = f"{args['gameid']}#{args['channelid']}" + g = GetTgaConfig() + item = g.get_api_key(args['gameid']) + url = item['url'] + args['suffix'] = item.get('suffix', 0) + api_key = item.get('api_key', None) + tga = FromTga(url, api_key) + + args['tga'] = tga + mp = MpInterface() + fromappids = mp.get_fromappid_cn(args['gameid'], args['channelid']) + + data[key] = {} + data[key]['all'] = self.get_all_data(**args) + data[key]['input'] = defaultdict(list) + data[key]['output'] = defaultdict(list) + data[key]['input'] = [] + data[key]['output'] = [] + keys = fromappids.keys() + if not keys: + break + for key in keys: + args['fromappid'] = key + args['fromappid_cn'] = fromappids.get(key, None) or "未知" + data[key]['input'].append(self.get_input_fromappid(**args)) + data[key]['output'].append(self.get_output_fromappid(**args)) + print(data) + return data def main(): - channelid = 6001 - gameids = (1004, 2001) day = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d') - for gameid in gameids: - cc = Report(gameid, channelid, day) - data = cc.run() + project = 'mini_games' + cc = Report() + data = cc.run(day, project) + print(data) if __name__ == "__main__": - # main() - app.run(host='0.0.0.0', port=6700, debug=False) + main() # app.run(host='0.0.0.0', port=6700, debug=False) diff --git a/daily_report/templates/report.html b/daily_report/templates/report.html index d3ebfa1..7d5ae7a 100644 --- a/daily_report/templates/report.html +++ b/daily_report/templates/report.html @@ -24,8 +24,8 @@ > - - + + @@ -34,17 +34,16 @@ - - -> - + + + {% for item in data.get('all') %} {% endfor %} -> +
活跃 新增 分享banner显示 插屏显示 导出率
{{ item }}
diff --git a/ops/mp_interface.py b/ops/mp_interface.py new file mode 100644 index 0000000..6be693a --- /dev/null +++ b/ops/mp_interface.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +import requests + + +class MpInterface: + def __init__(self): + self.base_url = "https://mp.kingsome.cn/api/open/cfg/all?" + + def get_data(self, url): + r = requests.get(url) + if r.status_code == requests.codes.ok: + return r.json() + else: + return None + + + def get_fromappid_cn(self, gameid, channelid): + key = "fromappid_cn" + url = f"{self.base_url}channelid={channelid}&gameid={gameid}&key={key}" + return self.get_data(url)