# -*- 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()