datacollect/cron_2001.py

94 lines
3.2 KiB
Python

# -*- 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__)
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"
tga_data={}
tga_data['account_id'] = f"{self.gameid}_{self.channelid}"
tga_data['gameid'] = self.gameid
tga_data['channelid'] = self.channelid
tga_data['date'] = self.date
tga_data['#time'] = datetime.datetime.strptime(self.date, "%Y-%m-%d")
if data:
for line in data:
if line:
try:
tga_data['playtimes'],tga_data['ad_channel']=line
self.tga.put_event_data(tga_data, f'rep_{event_type}')
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"
tga_data = {}
tga_data['account_id'] = f"{self.gameid}_{self.channelid}"
tga_data['gameid'] = self.gameid
tga_data['channelid'] = self.channelid
tga_data['date'] = self.date
tga_data['#time'] = datetime.datetime.strptime(self.date, "%Y-%m-%d")
if data:
for line in data:
if line:
try:
if channel == '':
tga_data['num'] = 37545 + int(line[0])
tga_data['num'] = line[0]
tga_data['ad_channel'] = channel
self.tga.put_event_data(tga_data, f'rep_{event_type}')
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()