# -*- coding: utf-8 -*- from ops.plog import define_logger import logging import sys import datetime from ops.mmysql import MysqlBase import pdb define_logger("/data/logs/get_newusers_days.log") log = logging.getLogger(__name__) DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'ad'} mydb = MysqlBase(**DB) table_name = "new_users_days" def get_data(times, gameid, channelid): sql = f"""SELECT count(accountid) AS users, ad_channel FROM newuser WHERE gameid={gameid} AND channelid={channelid} AND date(register_time) ='{times}' GROUP BY ad_channel;""" out = mydb.query(sql) data = [] if out: try: for line in out: temp = {} temp['gameid'] = gameid temp['channelid'] = channelid temp['usernum'], temp['ad_channel'] = line data.append(temp) except Exception: log.error(f"split {line} error!") return data def write2db(data, times, gameid, channelid): for line in data: line['date'] = times line['key'] = f"{gameid}#{channelid}#{line['ad_channel']}#{times}" try: mydb.insert(table_name, line) except Exception: log.error(f" insert {line} to db failed", exc_info=True) def main(): if len(sys.argv) == 2: times = sys.argv[1] else: times = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d') gameids = (1004, 1011, 2001, 1001, 1013) for gameid in gameids: channelid = 6001 data = get_data(times, gameid, channelid) write2db(data, times, gameid, channelid) if __name__ == "__main__": main()