实时新增人数计算
This commit is contained in:
parent
634d3654da
commit
40025960f1
67
gather_new_user.py
Normal file
67
gather_new_user.py
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
# -*- 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()
|
@ -15,9 +15,9 @@ DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'games
|
|||||||
|
|
||||||
class InitFirstAD():
|
class InitFirstAD():
|
||||||
def __init__(self, kwargs):
|
def __init__(self, kwargs):
|
||||||
self.end = kwargs.get('time')
|
self.now = kwargs.get('time')
|
||||||
self.begin = (datetime.datetime.strptime(self.end, '%Y-%m-%d %H:%M:%S') - datetime.timedelta(
|
# self.begin = (datetime.datetime.strptime(self.end, '%Y-%m-%d %H:%M:%S') - datetime.timedelta(
|
||||||
minutes=TimeDelay)).strftime('%Y-%m-%d %H:%M:%S')
|
# minutes=TimeDelay)).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
self.gameid = kwargs.get('gameid')
|
self.gameid = kwargs.get('gameid')
|
||||||
self.channelid = kwargs.get('channelid')
|
self.channelid = kwargs.get('channelid')
|
||||||
g = GetTgaConfig()
|
g = GetTgaConfig()
|
||||||
@ -32,8 +32,10 @@ class InitFirstAD():
|
|||||||
self.tga.init_tga_write(self.tgaid)
|
self.tga.init_tga_write(self.tgaid)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
users = self.get_new_user()
|
begin = self.get_last_times()
|
||||||
|
users = self.get_new_user(begin)
|
||||||
mark_users = self.hide_user(users)
|
mark_users = self.hide_user(users)
|
||||||
|
|
||||||
log.info(f"get users {len(users)},write {len(mark_users)} to mysql!")
|
log.info(f"get users {len(users)},write {len(mark_users)} to mysql!")
|
||||||
table_name = "newuser"
|
table_name = "newuser"
|
||||||
if mark_users:
|
if mark_users:
|
||||||
@ -47,6 +49,16 @@ class InitFirstAD():
|
|||||||
except Exception:
|
except Exception:
|
||||||
log.error(f"insert {line} to mysql Failed !", exc_info=True)
|
log.error(f"insert {line} to mysql Failed !", exc_info=True)
|
||||||
|
|
||||||
|
def get_last_times(self):
|
||||||
|
sql = f"""SELECT create_time FROM newuser ORDER BY create_time DESC LIMIT 1;"""
|
||||||
|
data = self.mysql.query(sql)
|
||||||
|
try:
|
||||||
|
last_time = data[0][0]
|
||||||
|
except Exception:
|
||||||
|
log.info("get last time error!", exc_info=True)
|
||||||
|
last_time = "1999-09-09 00:00:00"
|
||||||
|
return last_time
|
||||||
|
|
||||||
|
|
||||||
def hide_user(self, data):
|
def hide_user(self, data):
|
||||||
if data:
|
if data:
|
||||||
@ -58,7 +70,7 @@ class InitFirstAD():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_new_user(self):
|
def get_new_user(self, begin):
|
||||||
rdata = []
|
rdata = []
|
||||||
sql = f"""SELECT
|
sql = f"""SELECT
|
||||||
"#account_id",
|
"#account_id",
|
||||||
@ -68,7 +80,7 @@ class InitFirstAD():
|
|||||||
v_user_{self.suffix}
|
v_user_{self.suffix}
|
||||||
where
|
where
|
||||||
gameid='{self.gameid}'
|
gameid='{self.gameid}'
|
||||||
AND "account_register_time" BETWEEN timestamp'{self.begin}' AND timestamp'{self.end}' """
|
AND "account_register_time" BETWEEN timestamp'{begin}' AND timestamp'{self.now}' """
|
||||||
data = self.tga.get_data(sql)
|
data = self.tga.get_data(sql)
|
||||||
if data:
|
if data:
|
||||||
for line in data:
|
for line in data:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user