Compare commits

...

69 Commits

Author SHA1 Message Date
c4b287dfe7 更新 '初始化事件表.sql' 2023-02-27 10:42:30 +08:00
wuaho
9b59106f4d 追踪错误 2021-10-27 09:56:38 +08:00
wuaho
5500e8386e 改为手动提交 2021-10-26 21:36:57 +08:00
wuaho
19c64b5801 改为手动提交 2021-10-26 21:36:14 +08:00
wuaho
e33a22d1d4 改为手动提交 2021-10-26 21:14:30 +08:00
wuaho
1d6854beff 改为手动提交 2021-10-26 20:43:45 +08:00
wuaho
bf55fafe46 改为手动提交 2021-10-26 20:35:54 +08:00
wuaho
dad6c0b072 改为手动提交 2021-10-26 19:55:54 +08:00
wuaho
cf46c8b6d0 改为手动提交 2021-10-26 19:25:35 +08:00
wuaho
d639660839 改为手动提交 2021-10-26 18:29:23 +08:00
wuaho
e6331341a5 改为手动提交 2021-10-26 17:31:20 +08:00
wuaho
17571d01d3 改为手动提交 2021-10-26 17:06:40 +08:00
wuaho
25392daede 改为手动提交 2021-10-26 11:20:45 +08:00
wuaho
22fcd5e651 改为手动提交 2021-10-26 10:40:44 +08:00
wuaho
4eea6929d7 add unique id 2021-10-21 10:32:00 +08:00
wuaho
3b9ed0c98f add unique id 2021-10-21 10:17:57 +08:00
wuaho
4176aae943 add unique id 2021-10-21 10:13:21 +08:00
wuaho
debadeb774 add unique id 2021-10-21 10:09:50 +08:00
wuaho
1b32bafe44 重订单问题加日志跟踪 2021-10-21 09:46:45 +08:00
wuaho
80ec356516 重订单问题加日志跟踪 2021-10-11 19:12:24 +08:00
wuaho
f198c68b4e pipenv 2021-09-29 11:39:28 +08:00
wuaho
6abac2e9c5 优化 2021-09-26 19:07:51 +08:00
wuaho
d655407df7 优化 2021-09-23 17:21:18 +08:00
wuaho
f8a9938a77 优化 2021-09-23 16:32:11 +08:00
wuaho
d361aba1a0 优化 2021-09-23 12:34:38 +08:00
wuaho
8ae3568c15 优化 2021-09-18 10:42:26 +08:00
wuaho
7fb03eb90c 测试消费 2021-09-18 10:19:34 +08:00
wuaho
4a670de26b 停止消费 2021-09-18 10:09:18 +08:00
wuaho
75bca68aeb 1 2021-09-17 19:19:41 +08:00
wuaho
15591fd7ae 1 2021-09-17 18:11:56 +08:00
wuaho
678b1c93c0 1 2021-09-17 16:54:06 +08:00
wuaho
8f1ba1040b 1 2021-09-09 21:31:12 +08:00
wuaho
402def6734 1 2021-09-09 21:10:07 +08:00
wuaho
c81abb3006 收集事件名 2021-09-09 21:08:27 +08:00
wuaho
628f993e1b 添加密码 2021-09-07 20:35:25 +08:00
wuaho
73becdce74 修改 表引擎 和排序键 2021-09-07 16:30:47 +08:00
wuaho
6603e14434 svrindex String 2021-08-24 20:24:20 +08:00
wuaho
69060898d7 视图过滤 2021-08-18 15:42:44 +08:00
wuaho
ba3b1b0370 视图过滤 2021-08-18 15:06:16 +08:00
wuaho
28a1b31523 视图过滤 2021-08-17 15:37:07 +08:00
wuaho
8e003a4137 视图过滤 2021-08-17 15:35:16 +08:00
wuaho
346290412a 视图过滤 2021-08-17 14:33:52 +08:00
wuaho
da05dd532c 1 2021-08-16 18:07:38 +08:00
wuaho
ba9bb564dc 1 2021-08-15 15:17:48 +08:00
wuaho
53d9cd1594 1 2021-08-14 20:35:32 +08:00
wuaho
262222f6d2 1 2021-08-14 20:35:06 +08:00
wuaho
dfb489dd9c 1 2021-08-14 15:39:11 +08:00
wuaho
f44b67d585 1 2021-08-14 15:16:28 +08:00
wuaho
1213b6162c 1 2021-08-14 15:07:44 +08:00
wuaho
1814777322 1 2021-08-14 15:04:34 +08:00
wuaho
34ea378ebc 1 2021-08-14 14:55:39 +08:00
wuaho
38890d3a5c 1 2021-08-14 14:33:52 +08:00
wuaho
84c3b3442a 1 2021-08-14 13:58:47 +08:00
wuaho
1c04efd709 1 2021-08-14 12:04:03 +08:00
wuaho
83a43bf3da 1 2021-08-14 11:57:36 +08:00
wuaho
86aa16574e 1 2021-08-14 11:19:50 +08:00
wuaho
eef92b3fdc 1 2021-08-14 11:15:12 +08:00
wuaho
a6991c3421 1 2021-08-14 11:11:01 +08:00
wuaho
4e05b9f461 1 2021-08-14 11:04:23 +08:00
wuaho
0841c29438 1 2021-08-14 09:31:57 +08:00
wuaho
3ca8b92f90 1 2021-08-14 09:29:46 +08:00
wuaho
14d1ab3f0a 1 2021-08-14 09:26:23 +08:00
wuaho
9340bf8e58 1 2021-08-14 09:25:36 +08:00
wuaho
199eb8dba7 1 2021-08-14 00:35:19 +08:00
wuaho
3b3e900930 1 2021-08-14 00:32:03 +08:00
wuaho
baf68b2832 数据清理 2021-08-11 20:08:48 +08:00
wuaho
494fa8d74d 1 2021-08-03 18:24:30 +08:00
wuaho
ce0d6d717e 1 2021-08-03 17:55:20 +08:00
wuaho
79b47321c5 像素 2021-08-03 17:51:21 +08:00
22 changed files with 355 additions and 151 deletions

3
.gitignore vendored
View File

@ -129,3 +129,6 @@ dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
.idea .idea
# 不同游戏单独配置
settings.py
clear_up.py

12
Pipfile
View File

@ -4,12 +4,16 @@ verify_ssl = false
name = "pypi" name = "pypi"
[packages] [packages]
kafka-python = "*" kafka-python = "2.0.2"
clickhouse-driver = "*" clickhouse-driver = "0.2.2"
pipfile = "*" pipfile = "0.0.2"
pandas = "*" pandas = "1.3.3"
redis = "==3.5.3"
loguru = "==0.5.3"
[dev-packages] [dev-packages]
[requires] [requires]
python_version = "3.8" python_version = "3.8"

50
app.py
View File

@ -2,6 +2,8 @@
import time import time
from multiprocessing import Process from multiprocessing import Process
from kafka import TopicPartition
from settings import settings from settings import settings
from v2 import * from v2 import *
from v2.struct_cache import StructCacheFile, StructCacheRedis from v2.struct_cache import StructCacheFile, StructCacheRedis
@ -11,6 +13,7 @@ class XProcess(Process):
def __init__(self, partition, lock, ipsearch, log, rdb=None, event_attr=None): def __init__(self, partition, lock, ipsearch, log, rdb=None, event_attr=None):
super(XProcess, self).__init__() super(XProcess, self).__init__()
# self.daemon = True
self.partition = partition self.partition = partition
self.lock = lock self.lock = lock
self.ipsearch = ipsearch self.ipsearch = ipsearch
@ -25,43 +28,64 @@ class XProcess(Process):
handler_user = HandlerUser(db_client, settings.GAME) handler_user = HandlerUser(db_client, settings.GAME)
transmitter = Transmitter(db_client, settings.GAME, sketch, self.log, self.lock, self.event_attr, transmitter = Transmitter(db_client, settings.GAME, sketch, self.log, self.lock, self.event_attr,
self.partition) self.partition)
transmitter.start_ping()
transmitter.add_source(handler_event, 5000, 60) transmitter.add_source(handler_event, 5000, 60)
transmitter.add_source(handler_user, 500, 60) transmitter.add_source(handler_user, 500, 60)
last_ts = int(time.time()) last_ts = int(time.time())
consumer, kafka_client = create_consumer(self.partition) consumer, kafka_client = create_consumer(self.partition)
for topic, msg in consumer(): for msg in consumer():
# print(msg) data = msg.value
type_ = msg['#type'] type_ = data['#type']
del msg['#type'] del data['#type']
ts = int(time.time()) ts = int(time.time())
try:
data['properties']['unique_id'] = f'{msg.topic}-{msg.partition}-{msg.offset}'
except:
pass
if msg.topic == 'debug':
self.log.info(data)
if 'user' in type_: if 'user' in type_:
# continue # continue
obj = getattr(handler_user, type_) obj = getattr(handler_user, type_)
handler_user.receive_data.append(UserAct(obj, msg)) handler_user.receive_data.append(UserAct(obj, data))
if len(handler_user.receive_data) >= 1000 or last_ts + 60 < ts: if len(handler_user.receive_data) >= 1000 or last_ts + 60 < ts:
last_ts = ts last_ts = ts
handler_user.execute() handler_user.execute()
elif 'track' in type_: elif 'track' in type_:
# continue # continue
if data['#event_name'] == 'pay':
self.log.info(f'topid->{msg.topic} | partition->{msg.partition} | offset->{msg.offset} | data-> {data}')
obj = getattr(handler_event, type_) obj = getattr(handler_event, type_)
obj(msg) obj(data)
elif type_ == settings.STOP_SIGNAL: elif type_ == settings.STOP_SIGNAL:
# continue
# 1 小时内有效 # 1 小时内有效
if msg.get('#time', 0) + 3600 < int(time.time()): self.log.info(type_)
if data.get('#time', 0) + 3600 < int(time.time()):
continue continue
kafka_client.close()
# 停止消费kafka # 停止消费kafka
print(f'进程{self.partition} 等待90秒') self.log.info(f'进程{self.partition} 等待90秒')
time.sleep(90) time.sleep(90)
print(f'进程{self.partition} 写入数据') self.log.info(f'进程{self.partition} 写入数据')
transmitter.run() transmitter.run(kafka_client)
print(f'进程{self.partition} 结束') self.log.info(f'进程{self.partition} 结束')
kafka_client.commit()
kafka_client.close()
break break
elif type_ == 'test':
self.log.info(f'topid->{msg.topic} | partition->{msg.partition} | offset->{msg.offset} | data-> {data}')
else: else:
continue continue
transmitter.run() transmitter.run(kafka_client)
while True:
time.sleep(5)
self.log.info(f'消费分区{self.partition} 已结束。。。')

48
clear_up.py.template Normal file
View File

@ -0,0 +1,48 @@
"""
ÇåÀí²âÊÔÊý¾Ý
"""
import json
from settings import settings
game = ''
db = settings.GAME
server_list_url = f'http://gametools.legu.cc/?app=api&act=getServerList&game={game}'
import pandas as pd
from clickhouse_driver import Client
client = Client(**settings.CK_CONFIG)
df = pd.read_json(server_list_url)
df = df[~df['hostname'].isin(['119.3.89.14', '119.3.105.109'])]
serverid = tuple((str(i) for i in df['serverid'].to_list()))
sql = f"""select `#account_id`, `#event_time`, `#event_name`,`#os`,`svrindex`, count() as n
from {db}.event
where
`#event_time`>addDays(now('UTC'),-3) and (
lower(`#os`) = 'windows'
or svrindex not in {serverid}
)
group by `#account_id`, `#event_time`, `#event_name`,`#os`,`svrindex`
having n = 1 limit 2000"""
data, columns = client.execute(
sql, columnar=True, with_column_types=True
)
if not data:
exit(0)
data_df = pd.DataFrame(
{col[0]: d for d, col in zip(data, columns)}
)
data_df.drop('n', axis=1, inplace=True)
data_df['sign'] = -1
data_df['#event_time'] = data_df['#event_time'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))
insert_sql = f'INSERT INTO {db}.event FORMAT JSONEachRow '
insert_sql = insert_sql + '\n'.join([json.dumps(item) for item in data_df.T.to_dict().values()])
client.execute(insert_sql)

View File

@ -1 +1 @@
create database zhengba; create database xiangsu;

View File

@ -5,27 +5,31 @@ class Config:
# ck数据库连接 # ck数据库连接
CK_CONFIG = {'host': '139.159.159.3', CK_CONFIG = {'host': '139.159.159.3',
'port': 9654, 'port': 9654,
'send_receive_timeout': 30} 'user': 'legu',
'password': 'gncPASUwpYrc'
}
# 每个游戏不一样 游戏上报 kafka 主题 # 每个游戏不一样 游戏上报 kafka 主题
SUBSCRIBE_TOPIC = 'zhengba_test' # *************
SUBSCRIBE_TOPIC = ''
KAFKA_CONSUMER_CONF = { KAFKA_CONSUMER_CONF = {
'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"], 'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"],
'value_deserializer': json.loads, 'value_deserializer': json.loads,
'auto_offset_reset': 'earliest', 'auto_offset_reset': 'earliest',
'enable_auto_commit': True, 'enable_auto_commit': False,
'auto_commit_interval_ms': 10000,
# 每个游戏不一样 # 每个游戏不一样
'group_id': 'zhengba_consumer_group' # *************
'group_id': ''
} }
KAFKA_PRODUCER_CONF = { KAFKA_PRODUCER_CONF = {
'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"], 'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"],
'value_serializer': lambda v: json.dumps(v).encode('utf-8'), 'value_serializer': lambda v: json.dumps(v).encode('utf-8'),
} }
# 游戏数据库名 # 游戏数据库名
GAME = 'zhengba' # *************
GAME = ''
STOP_SIGNAL = 'stop_MntxuXMc' STOP_SIGNAL = 'stop_MntxuXMc'

View File

@ -1,6 +1,8 @@
# coding:utf-8
import time import time
import redis import redis
from kafka import TopicPartition
from settings import settings from settings import settings
from v2 import * from v2 import *
@ -16,30 +18,29 @@ from v2.log import logger
rdb = redis.Redis(**settings.REDIS_CONF) rdb = redis.Redis(**settings.REDIS_CONF)
event_attr = EventAttr(rdb) event_attr = EventAttr(rdb)
partition = 0
def run(): def run():
db_client = CK(**settings.CK_CONFIG) db_client = CK(**settings.CK_CONFIG)
sketch = Sketch(db_client) sketch = Sketch(db_client)
handler_event = HandlerEvent(db_client, settings.GAME, ipsearch) handler_event = HandlerEvent(db_client, settings.GAME, ipsearch)
handler_user = HandlerUser(db_client, settings.GAME) handler_user = HandlerUser(db_client, settings.GAME)
transmitter = Transmitter(db_client, settings.GAME, sketch, logger, lock, event_attr) transmitter = Transmitter(db_client, settings.GAME, sketch, logger, lock, event_attr,partition)
transmitter.add_source(handler_event, 10000, 60) transmitter.add_source(handler_event, 1000, 10)
transmitter.add_source(handler_user, 1000, 60) transmitter.add_source(handler_user, 1000, 10)
last_ts = int(time.time()) last_ts = int(time.time())
consumer = create_consumer(-1) consumer, kafka_client = create_consumer(partition)
for topic, msg in consumer(): for msg in consumer():
# print(msg) data = msg.value
type_ = msg['#type'] type_ = data['#type']
if msg['#app_id']!='e903ab24ad8f4bfca8a3ce7e122cd102': del data['#type']
print(msg)
del msg['#type']
ts = int(time.time()) ts = int(time.time())
if 'user' in type_: if 'user' in type_:
# continue # continue
obj = getattr(handler_user, type_) obj = getattr(handler_user, type_)
handler_user.receive_data.append(UserAct(obj, msg)) handler_user.receive_data.append(UserAct(obj, data))
if len(handler_user.receive_data) >= 1000 or last_ts + 60 < ts: if len(handler_user.receive_data) >= 1000 or last_ts + 60 < ts:
last_ts = ts last_ts = ts
handler_user.execute() handler_user.execute()
@ -47,11 +48,32 @@ def run():
elif 'track' in type_: elif 'track' in type_:
# continue # continue
obj = getattr(handler_event, type_) obj = getattr(handler_event, type_)
obj(msg) obj(data)
elif type_ == settings.STOP_SIGNAL:
# continue
# 1 小时内有效
print(type_)
if data.get('#time', 0) + 3600 < int(time.time()):
continue
kafka_client.close()
# 停止消费kafka
print(f'进程{msg.partition} 等待90秒')
time.sleep(1)
print(f'进程{msg.partition} 写入数据')
transmitter.run(kafka_client)
print(f'进程{msg.partition} 结束')
break
elif type_ == 'test':
print(f'topid->{msg.topic} | partition->{msg.partition} | offset->{msg.offset} | data-> {data}')
else: else:
continue continue
transmitter.run() transmitter.run(kafka_client)
while True:
time.sleep(5)
print(f'消费分区{partition} 已结束。。。')
if __name__ == '__main__': if __name__ == '__main__':

13
sync_event_name.py Normal file
View File

@ -0,0 +1,13 @@
import redis
from clickhouse_driver import Client
from settings import settings
rdb = redis.Redis(**settings.REDIS_CONF)
client = Client(**settings.CK_CONFIG)
sql = f"""select distinct `#event_name` as v from {settings.GAME}.event"""
df = client.query_dataframe(sql)
data = df['v'].to_list()
rdb.sadd(f'{settings.GAME}_event_set', *data)

View File

@ -0,0 +1,42 @@
# coding:utf-8
"""
更新事件表视图
"""
import json
import os
from settings import settings
game = ''
db = settings.GAME
svrid_file = f'{game}_svrid.json'
server_list_url = f'http://gametools.legu.cc/?app=api&act=getServerList&game={game}'
import pandas as pd
from clickhouse_driver import Client
client = Client(**settings.CK_CONFIG)
df = pd.read_json(server_list_url)
df = df[~df['hostname'].isin(['119.3.89.14', '119.3.105.109'])]
serverid = tuple((str(i) for i in df['serverid'].to_list()))
# if os.path.exists(svrid_file):
# with open(svrid_file, 'r') as f:
# old_svrid = json.load(f)
# if set(old_svrid) == set(serverid):
# exit(0)
sql = f"""drop table if exists {db}.event_view"""
res1 = client.execute(sql)
# 筛选有效数据
sql = f"""create view {db}.event_view as
select *
from {db}.event
where (`#os`is null or lower(`#os`) != 'windows')
and svrindex in {serverid}
and not startsWith(`orderid`,'debugPay')
"""
res2 = client.execute(sql)
with open(svrid_file, 'w') as f:
json.dump(sorted(serverid), f)

View File

@ -1,5 +1,5 @@
drop table if exists zhengba.user_view; drop table if exists xiangsu.user_view;
create view zhengba.user_view as select * create view xiangsu.user_view as select *
from zhengba.user from xiangsu.user
order by `#reg_time` desc order by `#reg_time` desc
LIMIT 1 by `#account_id` LIMIT 1 by `#account_id`

View File

@ -6,18 +6,19 @@ from settings import settings
__all__ = 'create_consumer', __all__ = 'create_consumer',
def create_consumer(partition=-1): def create_consumer(partition: int = -1):
c = KafkaConsumer(**settings.KAFKA_CONSUMER_CONF) c = KafkaConsumer(**settings.KAFKA_CONSUMER_CONF)
def consumer(): def consumer():
if partition > 0: if partition > -1:
c.assign([TopicPartition(settings.SUBSCRIBE_TOPIC, partition)]) c.assign([TopicPartition(settings.SUBSCRIBE_TOPIC, partition)])
else: else:
c.subscribe([settings.SUBSCRIBE_TOPIC]) c.subscribe([settings.SUBSCRIBE_TOPIC])
for msg in c: for msg in c:
# print(msg) # print(msg)
topic = msg.topic yield msg
val = msg.value # topic = msg.topic
yield topic, val # val = msg.value
# yield topic, val
return consumer, c return consumer, c

View File

@ -1,5 +1,6 @@
__all__ = 'CK', __all__ = 'CK',
import threading
import traceback import traceback
import pandas as pd import pandas as pd
@ -15,12 +16,20 @@ class CK:
self.args = args self.args = args
self.kwargs = kwargs self.kwargs = kwargs
self.__client = self.__create_client() self.__client = self.__create_client()
self.look = threading.Lock()
def __create_client(self): def __create_client(self):
return Client(*self.args, **self.kwargs) return Client(*self.args, **self.kwargs)
def execute(self, *args, **kwargs): def execute(self, *args, **kwargs):
return self.__client.execute(*args, **kwargs) try:
self.look.acquire(timeout=10)
res = self.__client.execute(*args, **kwargs)
except Exception as e:
raise e
finally:
self.look.release()
return res
def get_one(self, db, tb, try_cnt=3, **where): def get_one(self, db, tb, try_cnt=3, **where):
@ -30,7 +39,7 @@ class CK:
sql += ' limit 1' sql += ' limit 1'
data = None data = None
try: try:
data, columns = self.__client.execute(sql, with_column_types=True) data, columns = self.execute(sql, with_column_types=True)
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()
self.__client.disconnect() self.__client.disconnect()
@ -62,9 +71,9 @@ class CK:
sql += where sql += where
data = None data = None
try: try:
data, columns = self.__client.execute(sql, columnar=True, with_column_types=True) data, columns = self.execute(sql, columnar=True, with_column_types=True)
except Exception as e: except Exception as e:
print('*'*50) print('*' * 50)
print(sql) print(sql)
traceback.print_exc() traceback.print_exc()
if e.code == 60: if e.code == 60:

View File

@ -7,26 +7,12 @@ class EventAttr:
def __init__(self, rdb: Redis): def __init__(self, rdb: Redis):
self.rdb = rdb self.rdb = rdb
def get_event_attr(self, key): def set_event_name(self, key, *data):
attr = self.event_attr.get(key)
if not attr:
self.event_attr[key] = self.rdb.smembers(key) or set()
return set(self.event_attr[key])
def set_event_attr(self, key, *data):
self.rdb.sadd(key, *data) self.rdb.sadd(key, *data)
self.event_attr[key] = data
def check_attr(self, db, data): def add_event(self, db, data):
event_name = data.get('#event_name') event_name = data.get('#event_name')
if not event_name: if not event_name:
return return
key = f'{db}_event_set'
key = f'{db}_event_{event_name}' self.set_event_name(key, event_name)
attr = self.get_event_attr(key)
data_attr = set(data)
extra_attr = data_attr - attr
if extra_attr:
self.set_event_attr(key, *extra_attr)

View File

@ -98,7 +98,7 @@ class Sketch:
default_field[k] = 'Nullable(String)' default_field[k] = 'Nullable(String)'
if isinstance(v, int): if isinstance(v, int):
default_field[k] = 'Nullable(UInt64)' default_field[k] = 'Nullable(Int64)'
if isinstance(v, float): if isinstance(v, float):
default_field[k] = 'Nullable(Float32)' default_field[k] = 'Nullable(Float32)'
@ -109,9 +109,9 @@ class Sketch:
if isinstance(v, bool): if isinstance(v, bool):
default_field[k] = 'Nullable(UInt8)' default_field[k] = 'Nullable(UInt8)'
sql = f'alter table {db}.{tb} add column `{k}` {default_field[k]}'
print(sql)
try: try:
sql = f'alter table {db}.{tb} add column `{k}` {default_field[k]}'
print(sql)
self.db_client.execute(sql) self.db_client.execute(sql)
except Exception as e: except Exception as e:
print(f'添加字段 {k} 失败,同步数据库表结构') print(f'添加字段 {k} 失败,同步数据库表结构')

View File

@ -1,14 +1,41 @@
import json import json
import os import os
import re import re
import threading
import time import time
import traceback import traceback
from settings import settings
from .valid_data import * from .valid_data import *
__all__ = 'Transmitter', __all__ = 'Transmitter',
class Ping(threading.Thread):
def __init__(self, db_client, p, log):
threading.Thread.__init__(self)
self.daemon = True
self.ping_ts = 0
self.time_out = 60
self.log = log
self.db_client = db_client
self.p = p
def run(self):
while True:
time.sleep(10)
ts = int(time.time())
if self.ping_ts + self.time_out < ts:
# 保持连接
try:
self.ping_ts = ts
self.log.info(f'保持连接{self.p} ping')
self.db_client.execute('select 1')
except:
self.log.error('ping error')
class Transmitter: class Transmitter:
def __init__(self, db_client, db_name, sketch, log, lock, event_attr, p=0): def __init__(self, db_client, db_name, sketch, log, lock, event_attr, p=0):
self.db_client = db_client self.db_client = db_client
@ -20,12 +47,16 @@ class Transmitter:
self.event_attr = event_attr self.event_attr = event_attr
self.p = p self.p = p
def start_ping(self):
t = Ping(self.db_client, self.p, self.log)
t.start()
def add_source(self, handler, bulk_max=1000, time_out=60): def add_source(self, handler, bulk_max=1000, time_out=60):
self.slots[handler] = {'bulk_max': bulk_max, 'time_out': time_out, "ts": int(time.time())} self.slots[handler] = {'bulk_max': bulk_max, 'time_out': time_out, "ts": int(time.time())}
def check_send(self): def check_send(self):
ts = int(time.time())
for h, p in self.slots.items(): for h, p in self.slots.items():
ts = int(time.time())
tb, buffer = h.buffer_pool tb, buffer = h.buffer_pool
buffer_size = len(buffer) buffer_size = len(buffer)
if (p['ts'] + p['time_out'] <= ts or buffer_size >= p['bulk_max']) and buffer_size > 0: if (p['ts'] + p['time_out'] <= ts or buffer_size >= p['bulk_max']) and buffer_size > 0:
@ -51,9 +82,11 @@ class Transmitter:
self.db_client.execute(sql) self.db_client.execute(sql)
self.log.info(f'进程{self.p} 写入耗时 {int(time.time() * 1000) - ts}') self.log.info(f'进程{self.p} 写入耗时 {int(time.time() * 1000) - ts}')
except Exception as e: except Exception as e:
self.log.error(traceback.format_exc())
# 丢弃错误行 再次发送 # 丢弃错误行 再次发送
if hasattr(e, 'code') and e.code == 26: if hasattr(e, 'code') and e.code == 26:
m = re.match('(.*)?Stack trace', e.message) m = re.match('(.*)?Stack trace', e.message)
self.log.error(data)
if m: if m:
error_msg = m.group(1) error_msg = m.group(1)
error_row = re.match('.*?errors out of (\d+) rows', error_msg) error_row = re.match('.*?errors out of (\d+) rows', error_msg)
@ -75,10 +108,11 @@ class Transmitter:
def check_table(self, db, tb, data): def check_table(self, db, tb, data):
[self.sketch.alter_table(db, tb, item) for item in data] [self.sketch.alter_table(db, tb, item) for item in data]
def set_event_attr(self, db, tb, data): def collect_event(self, db, tb, data):
if tb != 'event': if tb != 'event':
return return
[self.event_attr.check_attr(db, item) for item in data]
[self.event_attr.add_event(db, item) for item in data]
def check_type(self, db, tb, data): def check_type(self, db, tb, data):
struct_dict = self.sketch.struct_dict[f'{db}_{tb}'] struct_dict = self.sketch.struct_dict[f'{db}_{tb}']
@ -96,14 +130,19 @@ class Transmitter:
for key in del_keys: for key in del_keys:
del item[key] del item[key]
def run(self): def run(self, kafka_client):
for tb, buffer in self.check_send(): for tb, buffer in self.check_send():
try: try:
data = [self.flat_data(x) for x in buffer.values()] data = [self.flat_data(x) for x in buffer.values()]
self.check_table(self.db_name, tb, data) self.check_table(self.db_name, tb, data)
self.check_type(self.db_name, tb, data) self.check_type(self.db_name, tb, data)
self.set_event_attr(self.db_name, tb, data) self.collect_event(self.db_name, tb, data)
self.__send(self.db_name, tb, [json.dumps(item) for item in data]) self.__send(self.db_name, tb, [json.dumps(item) for item in data])
except Exception as e: except Exception as e:
self.log.error(e) self.log.error(traceback.format_exc())
self.log.error(data)
buffer.clear() buffer.clear()
try:
kafka_client.commit()
except Exception as e:
self.log.error(f'进程:{self.p} error:{e}')

View File

@ -26,9 +26,10 @@ def is_valid_int(v, **kwargs):
def is_valid_srt(v, **kwargs): def is_valid_srt(v, **kwargs):
if isinstance(v, str): try:
return v return str(v)
return None except:
return None
def is_valid_float(v, **kwargs): def is_valid_float(v, **kwargs):
@ -39,15 +40,17 @@ def is_valid_float(v, **kwargs):
def is_valid_bool(v, **kwargs): def is_valid_bool(v, **kwargs):
if isinstance(v, bool): try:
return v return bool(v)
return None except:
return None
def is_valid_array(v, **kwargs): def is_valid_array(v, **kwargs):
if isinstance(v, list): try:
return [str(i) for i in v] return [str(i) for i in v]
return None except:
return None
def is_valid_ipv4(v, **kwargs): def is_valid_ipv4(v, **kwargs):

View File

@ -1,5 +1,5 @@
drop table if exists zhengba.recharge_game; drop table if exists xiangsu.recharge_game;
create view zhengba.recharge_game as create view xiangsu.recharge_game as
select toDate(addHours(`#event_time`, `#zone_offset`)) as date, select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
`#os`, `#os`,
`#bundle_id`, `#bundle_id`,
@ -8,6 +8,6 @@ select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
arrayDistinct(groupArray(binduid)) as account, arrayDistinct(groupArray(binduid)) as account,
length(account) as account_num, length(account) as account_num,
sum(money) as money sum(money) as money
from zhengba.event from xiangsu.event
where `#event_name` = 'rechargeGame' where `#event_name` = 'rechargeGame'
group by toDate(addHours(`#event_time`, `#zone_offset`)), `#os`, `#bundle_id`, owner_name, channel group by toDate(addHours(`#event_time`, `#zone_offset`)), `#os`, `#bundle_id`, owner_name, channel

View File

@ -1,55 +1,61 @@
drop table if exists zhengba.event; -- auto-generated definition
create table zhengba.event create table event
( (
`#ip` Nullable(IPv4), `#ip` Nullable(IPv4),
`#country` Nullable(String), `#country` Nullable(String),
`#country_code` Nullable(String), `#province` Nullable(String),
`#province` Nullable(String), `#city` Nullable(String),
`#city` Nullable(String), `#os` Nullable(String),
`#os_version` Nullable(String), `#device_id` Nullable(String),
`#manufacturer` Nullable(String), `#screen_height` Nullable(UInt16),
`#os` Nullable(String), `#screen_width` Nullable(UInt16),
`#device_id` Nullable(String), `#device_model` Nullable(String),
`#screen_height` Nullable(UInt16), `#app_version` Nullable(String),
`#screen_width` Nullable(UInt16), `#bundle_id` Nullable(String),
`#device_model` Nullable(String), `#app_name` Nullable(String),
`#app_version` Nullable(String), `#game_version` Nullable(String),
`#bundle_id` Nullable(String), `#os_version` Nullable(String),
`#lib` Nullable(String), `#network_type` Nullable(String),
`#lib_version` Nullable(String), `#carrier` Nullable(String),
`#network_type` Nullable(String), `#manufacturer` Nullable(String),
`#carrier` Nullable(String), `#app_id` Nullable(String),
`#browser` Nullable(String), `#account_id` String,
`#browser_version` Nullable(String), `#distinct_id` Nullable(String),
`#duration` Nullable(String), binduid Nullable(String),
`#url` Nullable(String), channel Nullable(String),
`#url_path` Nullable(String), owner_name String default '',
`#referrer` Nullable(String), role_name Nullable(String),
`#referrer_host` Nullable(String), exp Nullable(UInt64),
`#title` Nullable(String), zhanli Nullable(UInt64),
`#screen_name` Nullable(String), maxmapid Nullable(UInt16),
`#element_id` Nullable(String), mapid Nullable(UInt16),
`#element_type` Nullable(String), ghid Nullable(String),
`#resume_from_background` Nullable(String), rmbmoney Nullable(UInt64),
`#element_selector` Nullable(String), jinbi Nullable(UInt64),
`#element_position` Nullable(String), svrindex Nullable(String),
`#element_content` Nullable(String), lv Nullable(UInt16),
`#scene` Nullable(String), vip Nullable(UInt16),
`#mp_platform` Nullable(String), game Nullable(String),
`#app_crashed_reason` Nullable(String),
`#zone_offset` Int8 default 8,
`#event_id` String,
`#event_time` DateTime('UTC'), `#zone_offset` Int8 default 8,
`#account_id` String, `#event_time` DateTime('UTC'),
`#distinct_id` Nullable(String), `#event_name` String,
`#event_name` String, `#server_time` DateTime('UTC') default now(),
`#server_time` DateTime('UTC') default now(),
unitPrice Nullable(UInt32),
money Nullable(String),
islishishouci Nullable(UInt8),
isdangrishouci Nullable(UInt8),
is_today_reg Nullable(UInt8),
orderid Nullable(String),
proid Nullable(String),
step_id Nullable(UInt16),
step_group Nullable(UInt16),
guide_start_time Nullable(UInt32),
online_ts Nullable(UInt16),
`#time` Nullable(DateTime('UTC'))
)
engine = ReplacingMergeTree PARTITION BY toYYYYMMDD(`#event_time`)
ORDER BY (owner_name, `#event_name`, `#event_time`, `#account_id`)
SETTINGS index_granularity = 8192;
`sign` Int8 default 1
) ENGINE = CollapsingMergeTree(sign)
PARTITION BY toYYYYMMDD(`#event_time`)
order by (`#account_id`, `#event_time`, `#event_name`)
-- TTL event_time + toIntervalDay(365)

View File

@ -1,10 +1,10 @@
drop table if exists zhengba.user; drop table if exists xiangsu.user;
create table zhengba.user create table xiangsu.user
( (
`#reg_time` DateTime('UTC'), `#reg_time` DateTime('UTC'),
`#account_id` String, `#account_id` String,
`svrindex` UInt16, `svrindex` String,
`#zone_offset` Int8 default 8, `#zone_offset` Int8 default 8,
`#server_time` DateTime('UTC') default now() `#server_time` DateTime('UTC') default now()

View File

@ -1,5 +1,5 @@
drop table if exists zhengba.new_account; drop table if exists xiangsu.new_account;
create view zhengba.new_account as create view xiangsu.new_account as
select toDate(addHours(`#event_time`, `#zone_offset`)) as date, select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
`#os`, `#os`,
`#bundle_id`, `#bundle_id`,
@ -7,6 +7,6 @@ select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
channel, channel,
groupArray(`binduid`) as account, groupArray(`binduid`) as account,
length(account) as num length(account) as num
from zhengba.event from xiangsu.event
where role_idx = 1 where role_idx = 1
group by toDate(addHours(`#event_time`, `#zone_offset`)), `#os`, `#bundle_id`, owner_name, channel group by toDate(addHours(`#event_time`, `#zone_offset`)), `#os`, `#bundle_id`, owner_name, channel

View File

@ -1,5 +1,5 @@
drop table if exists zhengba.new_account_recharge; drop table if exists xiangsu.new_account_recharge;
create view zhengba.new_account_recharge as (select date, create view xiangsu.new_account_recharge as (select date,
`#os`, `#os`,
`#bundle_id`, `#bundle_id`,
owner_name, owner_name,
@ -13,11 +13,11 @@ from (select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
channel, channel,
binduid, binduid,
money money
from zhengba.event from xiangsu.event
where `#event_name` = 'rechargeGame') as tb1 where `#event_name` = 'rechargeGame') as tb1
right join (select toDate(addHours(`#event_time`, `#zone_offset`)) as date, right join (select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
binduid binduid
from zhengba.event from xiangsu.event
where role_idx = 1) as tb2 where role_idx = 1) as tb2
on tb1.date = tb2.date and tb2.binduid = tb1.binduid on tb1.date = tb2.date and tb2.binduid = tb1.binduid
group by date, `#os`, `#bundle_id`, owner_name, channel) group by date, `#os`, `#bundle_id`, owner_name, channel)

View File

@ -1,5 +1,5 @@
drop table if exists zhengba.active_account; drop table if exists xiangsu.active_account;
create view zhengba.active_account as create view xiangsu.active_account as
select toDate(addHours(`#event_time`, `#zone_offset`)) as date, select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
`#os`, `#os`,
`#bundle_id`, `#bundle_id`,
@ -7,5 +7,5 @@ select toDate(addHours(`#event_time`, `#zone_offset`)) as date,
channel, channel,
uniqCombined(binduid) as num, uniqCombined(binduid) as num,
arrayDistinct(groupArray(binduid)) as account arrayDistinct(groupArray(binduid)) as account
from zhengba.event from xiangsu.event
group by toDate(addHours(`#event_time`, `#zone_offset`)), `#os`, `#bundle_id`, owner_name, channel group by toDate(addHours(`#event_time`, `#zone_offset`)), `#os`, `#bundle_id`, owner_name, channel