From 40025960f10cb8358a9361ea4babf8eddaa33e29 Mon Sep 17 00:00:00 2001 From: pengtao Date: Tue, 3 Sep 2019 11:20:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E6=97=B6=E6=96=B0=E5=A2=9E=E4=BA=BA?= =?UTF-8?q?=E6=95=B0=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gather_new_user.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++ get_new_user.py | 24 ++++++++++++----- 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 gather_new_user.py diff --git a/gather_new_user.py b/gather_new_user.py new file mode 100644 index 0000000..66753fa --- /dev/null +++ b/gather_new_user.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from ops.plog import define_logger +import logging +import datetime +from ops.mmysql import MysqlBase + + +define_logger("/data/logs/init_first_ad_channel.log") +log = logging.getLogger(__name__) +TimeDelay = 5 + +DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'games_report'} + +mydb = MysqlBase(**DB) + + +def get_last_time(gameid, channelid, ad_channel): + sql = f"""SELECT + create_time + FROM + newusers_line + where + gameid={gameid} + and channelid={channelid} + and ad_channel={ad_channel} + ORDER BY + create_time DESC LIMIT 1""" + data = mydb.query(sql) + try: + last_time = data[0][0] + except Exception: + log.info("get last time form db failed!", exc_info=True) + last_time = "2019-08-30 00:00:00" + return last_time + + +def comp_datetime(x, y): + xx = datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S") + yy = datetime.datetime.strptime(y, "%Y-%m-%d %H:%M:%S") + if xx - datetime.timedelta(minutes=TimeDelay) > yy: + return True + else: + return False + + +def gather_data(times): + print(f"start via {times}") + + +def run(gameid, channelid, ad_channel): + now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + last_time = get_last_time(gameid, channelid, ad_channel) + while comp_datetime(now, last_time): + print(f"lasttime={last_time}") + gather_data(last_time, gameid, channelid, ad_channel) + last_time = (last_time - datetime.timedelta(minutes=TimeDelay)).strftime("%Y-%m-%d %H:%M:%S") + + +def main(): + gameid = 2001 + channel = 6001 + ad_channel = "xx" + run(gameid, channel, ad_channel) + + +if __name__ == "__main__": + main() diff --git a/get_new_user.py b/get_new_user.py index 5f00246..743f2f8 100644 --- a/get_new_user.py +++ b/get_new_user.py @@ -15,9 +15,9 @@ DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'games class InitFirstAD(): def __init__(self, kwargs): - self.end = 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.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() @@ -32,8 +32,10 @@ class InitFirstAD(): self.tga.init_tga_write(self.tgaid) def run(self): - users = self.get_new_user() + begin = self.get_last_times() + users = self.get_new_user(begin) mark_users = self.hide_user(users) + log.info(f"get users {len(users)},write {len(mark_users)} to mysql!") table_name = "newuser" if mark_users: @@ -47,6 +49,16 @@ class InitFirstAD(): except Exception: log.error(f"insert {line} to mysql Failed !", exc_info=True) + def get_last_times(self): + sql = f"""SELECT create_time FROM newuser ORDER BY create_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: @@ -58,7 +70,7 @@ class InitFirstAD(): return None - def get_new_user(self): + def get_new_user(self, begin): rdata = [] sql = f"""SELECT "#account_id", @@ -68,7 +80,7 @@ class InitFirstAD(): v_user_{self.suffix} where gameid='{self.gameid}' - AND "account_register_time" BETWEEN timestamp'{self.begin}' AND timestamp'{self.end}' """ + AND "account_register_time" BETWEEN timestamp'{begin}' AND timestamp'{self.now}' """ data = self.tga.get_data(sql) if data: for line in data: