# -*- coding: utf-8 -*- from ops.mtga import FromTga, GetTgaConfig from ops.plog import define_logger import logging import requests import json from config.config import is_debug, adv_id_state, ad_type from ops.scripts import get_activity_ids, get_area_name define_logger("/data/logs/reports.log") log = logging.getLogger(__name__) class GetFromTga: def __init__(self, **kwargs): # log.info("begin collect gameid={},times={}!".format(kwargs.get('gameid'), kwargs.get('date'))) self.date = kwargs.get('date') self.b_time=kwargs.get('b_time',None) self.e_time = kwargs.get('e_time', None) self.gameid = kwargs.get('gameid') self.channelid = kwargs.get('channelid') self.event_type = kwargs.get('event_type') g = GetTgaConfig() item = g.get_api_key(self.gameid) self.url = item['url'] self.tgaid = 9999 self.suffix = item.get('suffix', 0) self.api_key = item.get('api_key', None) self.tga = FromTga(self.url, self.api_key) self.tga.init_tga_write(self.tgaid) def write_tga_files(self, data): try: tga_tap = {} tga_tap['result'] = json.dumps(data, ensure_ascii=False) tga_tap['account_id'] = self.gameid tga_tap['gameid'] = self.gameid tga_tap['channelid'] = self.channelid tga_tap['date'] = self.date if data: self.tga.put_event_data(tga_tap, f'rep_{self.event_type}') else: log.error(f"write {data} 2 tga!,event_type=f{self.event_type}", exc_info=True) except Exception: log.error(f"write 2 tga failed!", exc_info=True) class TAP_data(GetFromTga): def event_vistis_page(self): area_name = get_area_name() sql = f"""SELECT count(1) as tap , count(distinct "#account_id") as tap_people, sum(cast(json_extract(button_param,'$.event_value') as int))/1000 as delay_times, button_name FROM v_event_19 where gameid='{self.gameid}' and "#event_name"='event_11_31' and "$part_date"='{self.date}' and cast(json_extract(button_param,'$.event_type') as int)=1134 group by button_name, json_extract(button_param,'$.event_type') order by button_name""" data = self.tga.get_data(sql) if data: for line in data: temp = {} try: temp['vistis'], temp['vistis_pelple'], temp['delay_times'], temp['area_id'] = line temp['area_name'] = area_name.get(temp['area_id']) self.write_tga_files(temp) except Exception: log.error(f"write {line} 2 tga failed!", exc_info=True) class AD_data(GetFromTga): def event_ad_video(self): video_15 = f"""SELECT count(1) FROM v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_21' and activity_id=2 and activity_param like '%"isEnded":false%' and "$part_date"='{self.date}'""" video_all = f"""SELECT count(1) FROM v_event_{self.suffix} WHERE gameid='{self.gameid}' and "channel"='{self.channelid}' AND "$part_event"='event_11_21' and "$part_date"='{self.date}'""" video_data = {} video_15_d = self.tga.get_data(video_15) video_all_d = self.tga.get_data(video_all) try: video_data['video_15'] = video_15_d[0][0] video_data['video_all'] = video_all_d[0][0] self.write_tga_files(video_data) except Exception: log.error("get video data failed!", exc_info=True) def event_ad_video_new(self): video_15 = f"""SELECT count(1) FROM v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_21' and activity_id=2 and activity_param like '%"isEnded":false%' and "$part_date"='{self.date}' and account_register_date BETWEEN timestamp'{self.date} 00:00:00' and timestamp'{self.date} 23:59:59'""" video_all = f"""SELECT count(1) FROM v_event_{self.suffix} WHERE gameid='{self.gameid}' and "channel"='{self.channelid}' AND "$part_event"='event_11_21' and "$part_date"='{self.date}' and account_register_date BETWEEN timestamp'{self.date} 00:00:00' and timestamp'{self.date} 23:59:59' """ video_data = {} video_15_d = self.tga.get_data(video_15) video_all_d = self.tga.get_data(video_all) try: video_data['video_15'] = video_15_d[0][0] video_data['video_all'] = video_all_d[0][0] self.write_tga_files(video_data) except Exception: log.error("get video data failed!", exc_info=True) def event_ad_info(self): adv_id_state = {1: '重新拉取', 0: '显示'} ad_types = {1: '视屏启动', 2: '视频错误回调', 101: 'banner显示', 201: '插屏显示'} area_name = self._get_area_name() # 统计activity_id=101 banner显示的数据,其中activity_id_str 对应area数据,其来源从方法_get_area_name 获得 # activity_id=201 插屏广告,activity_id=1视屏启动 ,activity_id=2 视频错误回调(暂不统计) for ad_type in ad_types.keys(): sql_101 = f"""SELECT count(1) as num, count(distinct "#account_id")as num_per, activity_id, adv_id_str, activity_id_str, adv_id_state FROM v_event_{self.suffix} where "$part_event"='event_11_21' and gameid='{self.gameid}' and "$part_date"='{self.date}' and activity_id={ad_type} group by activity_id, adv_id_str, adv_id_state, activity_id_str""" data = self.tga.get_data(sql_101) if data: for line in data: try: temp = {} temp['views'], temp['u_views'], temp['ad_type_id'], temp['adv_id_str'], temp['activity_id_str'], \ temp['adv_id_state'] = line temp['ad_type_id'] = int(temp['ad_type_id']) temp['adv_id_state'] = int(temp['adv_id_state']) temp['ad_type'] = ad_types[temp.get('ad_type_id')] temp['ad_status'] = adv_id_state[temp.get('adv_id_state')] print(f"1 {temp}") if temp['ad_type_id'] == 101: temp['area_name'] = area_name[temp.get('activity_id_str')] self.write_tga_files(temp) except Exception: print(f"write {line} 2 tga!", exc_info=True) class Stage_data(GetFromTga): def event_stage(self): sql = f"""SELECT count("#account_id"), cast(json_extract(button_param,'$.level') as int) as level from v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_31' and "button_name"='stageover' and "$part_date"='{self.date}' group by cast(json_extract(button_param,'$.level') as int)""" data = self.tga.get_data(sql) if data: for line in data: if line: try: temp = {} temp['num'], temp['level'] = line self.write_tga_files(temp) except Exception: log.error("get stage data failed!", exc_info=True) def event_stage_new(self): sql = f"""SELECT count("#account_id"), cast(json_extract(button_param,'$.level') as int) as level from v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_31' and "button_name"='stageover' and "$part_date"='{self.date}' and account_register_date BETWEEN timestamp'{self.date} 00:00:00' and timestamp'{self.date} 23:59:59' group by cast(json_extract(button_param,'$.level') as int)""" data = self.tga.get_data(sql) if data: for line in data: if line: try: temp = {} temp['num'], temp['level'] = line self.write_tga_files(temp) except Exception: log.error("get stage data failed!", exc_info=True) class Share_data(GetFromTga): def event_share_map(self): activity_ids = get_activity_ids() sql = f"""SELECT count(1) AS num , count(DISTINCT "#account_id") AS u_num, activity_id_str FROM v_event_19 WHERE gameid='1001' AND "$part_event"='event_11_10' AND "$part_date"='2019-08-07' GROUP BY activity_id_str""" out = self.tga.get_data(sql) if out: for line in out: temp = {} if line: try: temp['num'], temp['u_num'], temp['activity_id_str'] = line print(f"123 {temp}") temp['activity_id'] = activity_ids.get(temp['activity_id_str']) self.write_tga_files(temp) except Exception: log.error(f"write {line} 2 tga failed!", exc_info=True) def event_share(self): share_sql = f"""SELECT count(1) FROM v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_10' and "$part_date"='{self.date}'""" invited_sql = f"""SELECT count(1) FROM v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_11' and "$part_date"='{self.date}'""" result = {} try: result['share'] = self.tga.get_data(share_sql)[0][0] result['invited'] = self.tga.get_data(invited_sql)[0][0] self.write_tga_files(result) except Exception: log.error("get data from tga failed!", exc_info=True) def event_share_new(self): share_sql = f"""SELECT count(1) FROM v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_10' and "$part_date"='{self.date}' and account_register_date BETWEEN timestamp'{self.date} 00:00:00'and timestamp'{self.date} 23:59:59'""" invited_sql = f"""SELECT count(1) FROM v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_11' and "$part_date"='{self.date}' and account_register_date BETWEEN timestamp'{self.date} 00:00:00' and timestamp'{self.date} 23:59:59'""" result = {} try: result['share'] = self.tga.get_data(share_sql)[0][0] result['invited'] = self.tga.get_data(invited_sql)[0][0] self.write_tga_files(result) except Exception: log.error("get data from tga failed!", exc_info=True) class Item_data(GetFromTga): def event_items_produce(self): sql = f"""SELECT sum(item_cnt) as item_num, item_id, reason , reason_param FROM v_event_19 where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_8' and "$part_date"='{self.date}' group by item_id, reason, reason_param """ out = self.tga.get_data(sql) if out: for line in out: try: temp = {} temp['nums'], temp['item_id'], temp['reason'], temp['reason_param'] = line self.write_tga_files(temp) except Exception: log.error(f"write {line} 2 tga failed!", exc_info=True) def event_items_consum(self): sql = f"""SELECT sum(item_cnt) as item_num, item_id, reason , reason_param FROM v_event_{self.suffix} where gameid='{self.gameid}' and "channel"='{self.channelid}' and "$part_event"='event_11_9' and "$part_date"='{self.date}' group by item_id, reason, reason_param """ out = self.tga.get_data(sql) if out: for line in out: try: temp = {} temp['nums'], temp['item_id'], temp['reason'], temp['reason_param'] = line self.write_tga_files(temp) except Exception: log.error(f"write {line} 2 tga failed!", exc_info=True) class Retain_data(GetFromTga): def event_retain_1(self): pass def event_retain_7(self): pass