# -*- coding: utf-8 -*- import datetime from ops.init_tga import GetFromTga from ops.plog import define_logger import logging import pdb define_logger("/data/logs/datacollect_2001.log") log = logging.getLogger(__name__) def write_tga(times, event_type, data): kwargs = {} kwargs['gameid'] = 2001 kwargs['channelid'] = 6001 kwargs['date'] = times class Mycollect2001(GetFromTga): def workflow(self): ad_channel = self.get_ad_challel() ad_channel.append('') self.collect_playtimes() self.collect_grand_users(ad_channel) def get_ad_challel(self): sql = f"""SELECT distinct ad_channel FROM v_event_22 where "$part_event"='event_11_1' and "$part_date"='{self.date}'""" temp = [] data = self.tga.get_data(sql) if data: for line in data: try: if line[0]: temp.append(line[0]) except Exception: log.error(f"get ad channel failed!", exc_info=True) return temp def collect_playtimes(self): sql = f"""SELECT sum(cast(online_duration as int)),ad_channel FROM v_event_22 where "$part_event"='event_21_2' and "$part_date"='{self.date}' group by ad_channel """ data = self.tga.get_data(sql) event_type = "playtimes" if data: for line in data: if line: temp = {} try: temp['playtimes'], temp['ad_channel'] = line self.write_tga_files(event_type, temp) except Exception: log.error(f"split {line} error", exc_info=True) def collect_grand_users(self, ad_channel): for channel in ad_channel: sql = f"""SELECT count("#account_id") FROM v_user_22 where gameid='{self.gameid}' and first_ad_channel='{channel}'""" data = self.tga.get_data(sql) event_type = "grand_users" if data: for line in data: if line: temp = {} try: temp['num'] = line[0] temp['ad_channel'] = channel self.write_tga_files(event_type, temp) except Exception: log.error(f"split {line} error", exc_info=True) def main(): times = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d') kwargs = {} kwargs['gameid'] = 2001 kwargs['channelid'] = 6001 kwargs['date'] = times mc = Mycollect2001(**kwargs) mc.workflow() if __name__ == "__main__": main()