2019-10-22 15:16:35 +08:00

295 lines
9.8 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from ops.mtga import GetTgaConfig, FromTga
import os
from flask import Flask, render_template, request, jsonify
from flask_mail import Mail, Message
from threading import Thread
from collections import defaultdict
from ops.plog import define_logger
import logging
import datetime
define_logger("/data/logs/ops/daily_report.log")
import pdb
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"]
FROMAPPID_CN = {"wxdb103a128e118619": "拯救熊猫泡泡", "wxc137c93eedeab6f2": "爆冰达人"}
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')
if not (gameid and channelid and day):
return jsonify("PLS input arfs")
rp = Report(gameid, channelid, day)
data = rp.run()
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 jsonify("邮件发送成功")
class Report:
def __init__(self, gameid, channelid, day):
self.gameid = gameid
self.channelid = channelid
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)
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_1, ad_101, 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 from_appid='{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 from_appid='{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 from_appid='{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 from_appid='{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 (fromappid, FROMAPPID_CN.get(fromappid, None), 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 """
data = self.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)
except Exception:
log.error(f"get data from output by {self.gameid} {jump_appid} failed", exc_info=True)
return None
else:
return None
def run(self):
data = dict()
data['day'] = self.day
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
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()
if __name__ == "__main__":
# main()
app.run(host='0.0.0.0', port=6700, debug=False)