131 lines
4.2 KiB
Python
131 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
from ops.mtga import FromTga, GetTgaConfig
|
|
from ops.plog import define_logger
|
|
import logging
|
|
import datetime
|
|
from ops.mmysql import MysqlBase
|
|
import random
|
|
|
|
import pdb
|
|
define_logger("/data/logs/init_first_ad_channel.log")
|
|
log = logging.getLogger(__name__)
|
|
TimeDelay = 60*60
|
|
|
|
DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'games_report'}
|
|
|
|
|
|
class InitFirstAD():
|
|
def __init__(self, kwargs):
|
|
self.now = kwargs.get('time')
|
|
# self.begin = (datetime.datetime.strptime(self.end, '%Y-%m-%d %H:%M:%S') - datetime.timedelta(
|
|
# minutes=TimeDelay)).strftime('%Y-%m-%d %H:%M:%S')
|
|
self.gameid = kwargs.get('gameid')
|
|
self.channelid = kwargs.get('channelid')
|
|
g = GetTgaConfig()
|
|
item = g.get_api_key(self.gameid)
|
|
self.url = item['url']
|
|
self.tgaid = item['tgaid']
|
|
self.suffix = item.get('suffix', 0)
|
|
self.api_key = item.get('api_key', None)
|
|
self.tga = FromTga(self.url, self.api_key)
|
|
self.mysql = MysqlBase(**DB)
|
|
self.hide = 0.8
|
|
self.tga.init_tga_write(self.tgaid)
|
|
|
|
def get_ad_channel(self):
|
|
sql = f"""SELECT
|
|
DISTINCT first_ad_channel
|
|
FROM
|
|
v_user_{self.suffix}
|
|
where
|
|
gameid='{self.gameid}'"""
|
|
temp = []
|
|
data = self.tga.get_data(sql)
|
|
if data:
|
|
for line in data:
|
|
try:
|
|
temp.append(line[0])
|
|
except Exception:
|
|
log.error(f"split data {line} failed", exc_info=True)
|
|
return temp
|
|
|
|
|
|
def run(self):
|
|
ad_channels = self.get_ad_channel()
|
|
for ad_channel in ad_channels:
|
|
|
|
begin = self.get_last_times(ad_channel)
|
|
users = self.get_new_user(begin,ad_channel)
|
|
#print(f"get {users} with {ad_channel} ")
|
|
if users:
|
|
mark_users = self.hide_user(users)
|
|
table_name = "newuser"
|
|
if mark_users:
|
|
log.info(f"get users {len(users)},write {len(mark_users)} to mysql!")
|
|
for line in mark_users:
|
|
try:
|
|
temp = {}
|
|
temp['gameid'] = self.gameid
|
|
temp['channelid'] = self.channelid
|
|
temp['accountid'], temp['register_time'], temp['ad_channel'] = line
|
|
self.mysql.insert(table_name, temp)
|
|
except Exception:
|
|
log.error(f"insert {line} to mysql Failed !", exc_info=True)
|
|
|
|
def get_last_times(self, ad_channel):
|
|
sql = f"""SELECT register_time FROM newuser where ad_channel='{ad_channel}' AND gameid={self.gameid} AND
|
|
channelid={self.channelid}
|
|
ORDER BY
|
|
register_time
|
|
DESC
|
|
LIMIT 1;"""
|
|
data = self.mysql.query(sql)
|
|
try:
|
|
last_time = data[0][0]
|
|
except Exception:
|
|
log.info("get last time error!", exc_info=True)
|
|
last_time = "1999-09-09 00:00:00"
|
|
return last_time
|
|
|
|
|
|
def hide_user(self, data):
|
|
if data:
|
|
line = len(data)
|
|
nums = int(line * self.hide)
|
|
if nums:
|
|
return random.sample(data, nums)
|
|
else:
|
|
return None
|
|
|
|
|
|
def get_new_user(self, begin,ad_channel):
|
|
rdata = []
|
|
sql = f"""SELECT
|
|
"#account_id",
|
|
account_register_time,
|
|
first_ad_channel
|
|
FROM
|
|
v_user_{self.suffix}
|
|
where
|
|
gameid='{self.gameid}'
|
|
AND first_ad_channel='{ad_channel}'
|
|
AND "account_register_time" > timestamp'{begin}' AND "account_register_time"<timestamp'{self.now}' """
|
|
data = self.tga.get_data(sql)
|
|
if data:
|
|
for line in data:
|
|
if line:
|
|
rdata.append(line)
|
|
return rdata
|
|
|
|
|
|
def main():
|
|
kwargs = {}
|
|
kwargs['time'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
kwargs['gameid'] = 2001
|
|
kwargs['channelid'] = 6001
|
|
cc=InitFirstAD(kwargs)
|
|
cc.run()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|