diff --git a/daily_report/dreport.py b/daily_report/dreport.py index 5b147c7..6b15379 100644 --- a/daily_report/dreport.py +++ b/daily_report/dreport.py @@ -1,16 +1,268 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import from flask import render_template +from ops.mtga import GetTgaConfig, FromTga +import os +from flask import Flask, render_template, request +from flask_mail import Mail, Message +from threading import Thread +from collections import defaultdict +from ops.plog import define_logger +import logging + +define_logger("/data/logs/ops/daily_report.log") + +log = logging.getLogger(__name__) + +sender = "ops@kingsome.cn" +app = Flask(__name__) +app.config['MAIL_SERVER'] = 'smtp.exmail.qq.com' +app.config['MAIL_PORT'] = '465' +app.config['MAIL_USE_SSL'] = True +app.config['MAIL_USE_TLS'] = False ## 默认就是 false, 加上警示自己 +app.config['MAIL_USERNAME'] = sender +app.config['MAIL_PASSWORD'] = 'bX8cfBAyj9MBqH22' + +mail = Mail(app) + +recipients = ["pengtao@kingsome.cn"] + + +def send_async_email(app, msg): + with app.app_context(): + mail.send(msg) + + +@app.route('/send-dailyreport') +def send_dailyreport(): + title = "主题" + msg = Message(title, sender=sender, recipients=recipients) + + gameid = request.args.get('gameid') + channelid = request.args.get('channelid') + day = request.args.get('day') + + rp = Report(gameid, channelid, day) + data = rp.get_data() + data[gameid] = gameid + data[channelid] = channelid + data[day] = day + msg.subject = f"{gameid}_{day}_数据" + msg.html = render_template('report.html', data=data) + + thread = Thread(target=send_async_email, args=[app, msg]) + thread.start() + + return '

邮件发送成功

' class Report: - def __init__(self, gameid, channelid): + def __init__(self, gameid, channelid, day): self.gameid = gameid self.channelid = channelid + self.day = day + g = GetTgaConfig() + item = g.get_api_key(self.gameid) + 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) + + + def get_data(self): + data = defaultdict(tuple) + fromappids = () + for fromappid in fromappids: + activa, new, share, k = self.collect_tga(fromappid) + data[fromappid] = (activa, new, share, k) + + + def get_all_data(self): + activa_sql = f"""SELECT + count(distinct "#account_id") + FROM + v_event_{self.suffix} + where + gameid='{self.gameid}' + and channel='{self.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} + where + gameid='{self.gameid}' + and channel='{self.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} + where + "$part_event"='event_11_10' + and gameid='{self.gameid}' + and channel='{self.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} + # where + # "$part_event"='event_11_11' + # and gameid='{self.gameid}' + # and channel='{self.channelid}' + # and "$part_event"='event_11_1' + # and "$part_date"='{self.day}'""" + + timeonlie_sql = f"""SELECT + sum(cast(online_duration as int)) + FROM + v_event_19 + where + "$part_event"='event_21_2' + and gameid='{self.gameid}' + and "$part_date"='{self.day}' """ + + ad_101_sql = f"""SELECT + count(1) + FROM + v_event_{self.suffix} + WHERE + "$part_event"='event_11_21' + and gameid='{self.gameid}' + and "channel"='{self.channelid}' + AND activity_id=101 + and "$part_date"='{self.day}' """ + + ad_1_sql = f"""SELECT + count(1) + FROM + v_event_{self.suffix} + WHERE + "$part_event"='event_11_21' + and gameid='{self.gameid}' + and "channel"='{self.channelid}' + AND activity_id=1 + and "$part_date"='{self.day}' """ + + ad_201_sql = f"""SELECT + count(1) + FROM + v_event_{self.suffix} + WHERE + "$part_event"='event_11_21' + and gameid='{self.gameid}' + and "channel"='{self.channelid}' + AND activity_id=201 + and "$part_date"='{self.day}' """ + + 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] + return (activa, new, share, timeonlie, ad_101, ad_1, ad_201) + except Exception: + log.error(f"get data from tga failed ,{self.gameid}", exc_info=True) + return None + + + def get_input_fromappid(self, fromappid): + activa_sql = f"""SELECT + count(distinct "#account_id") + FROM + v_event_{self.suffix} + where + gameid='{self.gameid}' + and channel='{self.channelid}' + and "$part_event"='event_11_1' + and "$part_date"='{self.day}' + and fromappid='{fromappid}'""" + new_sql = f"""SELECT + count(distinct "#account_id") + FROM + v_event_{self.suffix} + where + gameid='{self.gameid}' + and channel='{self.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 fromappid='{fromappid}' """ + + share_sql = f"""SELECT + count(distinct \"#account_id\") + FROM + v_event_{self.suffix} + where + "$part_event"='event_11_10' + and gameid='{self.gameid}' + and channel='{self.channelid}' + and "$part_event"='event_11_1' + and "$part_date"='{self.day}' + and fromappid='{fromappid}' """ + + byshare_sql = f"""SELECT + count(distinct \"#account_id\") + FROM + v_event_{self.suffix} + where + "$part_event"='event_11_11' + and gameid='{self.gameid}' + and channel='{self.channelid}' + and "$part_event"='event_11_1' + and "$part_date"='{self.day}' + and fromappid='{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 + if activa == 0: + k = 0 + else: + k = (100 * byshare / (activa - byshare)) + return (activa, new, share, k) + except Exception: + log.error(f"collect input failed {self.gameid} {fromappid}", exc_info=True) + return None + + + def get_output_fromappid(self, jump_appid): + jump_sql = f"""SELECT + count("#account_id"),count(distinct "#account_id") + FROM + v_event_19 + where + "$part_event"='event_1_4' + and "$part_date"='{self.day}' + and "jump_appid"='{jump_appid}' + and "jump_result"=1 + order by + "#server_time" desc limit 10""" + + try: + jump_num, jump_pre = self.tga.get_data(jump_sql)[0] + return (jump_num, jump_pre) + except Exception: + log.error(f"get data from output by {self.gameid} {jump_appid} failed", exc_info=True) + return None def run(self): - pass + fromappid = 'wxcff7381e631cf54e' + a = self.get_all_data() + b = self.get_input_fromappid(fromappid) + c = self.get_output_fromappid(fromappid) + print(f"{a},{b},{c}") def main(): diff --git a/daily_report/templates/report.html b/daily_report/templates/report.html new file mode 100644 index 0000000..c43f006 --- /dev/null +++ b/daily_report/templates/report.html @@ -0,0 +1,50 @@ + + + + + 金蚕游戏日报,By ops + + +游戏ID:{{data.get(gameid)}} +渠道ID:{{data.get('channelid')}}='6001' +日期:{{data.get(day)}} + +总计数据: +活跃 +新增 +时长 +分享 +K值 +视频/banner + + + +导入数据: + + 渠道ID + 游戏名称 + 活跃 + 新增 + 分享 + K值 + + + + +{% for line in data.get('data') %} + + {%for item in line%} + {{ item }} + {% endfor %} + +{% endfor %} + +导出数据: +fromappid +中文 +跳转次数 +跳转人数 + + + + \ No newline at end of file