This commit is contained in:
kf_wuhao 2021-04-02 16:03:26 +08:00
parent 4e08f4b68b
commit 369efd9da7
5 changed files with 63 additions and 53 deletions

View File

@ -4,43 +4,21 @@
- 打点上报接口
##### 请求URL
- ` http://taapi.legu.cc/v1/point/ `
- ` http://taapi.legu.cc/v1/ `
##### 请求方式
- POST
##### 参数
| 参数名 | 必选 | 类型 | 说明 |
| :---------- | :--- | :----- | -------------------------------------------------- |
| distinct_id | 是 | string | 账户id |
| game | 是 | string | 游戏代号 |
| account_id | 是 | string | 访客id |
| act | 是 | string | ta sdk方法trackuser_setuser_setOnce等 |
| event_name | 否 | string | 事件名 当act为track时 此参数必须 |
| properties | 是 | 字典 | 属性 |
| ts | 是 | int | 时间戳 |
| sign | 是 | string | 签名 md5(distinct_id+account_id+act+ts+"0r4X00mH") |
| 参数名 | 必选 | 类型 | 说明 |
| :--------- | :--- | :--------- | -------------------------------------------------- |
| public | 是 | dict | 公共部分 |
| data | 是 | list[dict] | 记录列表 |
##### 请求示例
```shell
curl -X 'POST' \
'http://taapi.legu.cc/v1/point/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"distinct_id":"aaa",
"account_id":"bb",
"game":"test",
"act":"track",
"event_name":"ceshi",
"properties":{
"lv":6,
"vip":7,
"#device_id":"aaaa",
"herostate": [["fffgsa", 2, 3, 4, 5], ["ddd", 4, 5, 6, 8]]
},
"sign":"b0991b9928fd17bd43b4092677f05088",
"ts":1617096477
}'
```
##### 返回示例

View File

@ -6,9 +6,20 @@ __all__ = 'restore_field', 'sort_kv', 'check_sign'
def restore_field(data: dict) -> dict:
"""
不在预置字段 去掉前置#
:param data:
:return:
"""
res = dict()
for k, v in data.items():
res[settings.FIELD_MAP.get(k) or k] = v
map_name = settings.FIELD_MAP.get(k)
if map_name:
res[map_name] = v
elif k.startswith('#'):
res[k[1:]] = v
else:
res[k] = v
return res

View File

@ -1,3 +1,7 @@
import time
import arrow
from settings import settings
@ -12,11 +16,11 @@ class TaHandler:
msg = dict()
for k in settings.TA_OUTER:
v = data.get(k)
if v:
if v is not None:
msg[k] = data.pop(k)
msg['properties'] = data.pop('properties')
if msg['#type'] == 'track':
msg.update(data)
msg['properties'].update(data)
elif data['#type'] == 'user':
data['#type'] = 'user' + data['#event_name']
msg['properties']['#user_id'] = data['#user_id']
@ -36,6 +40,18 @@ async def add_ip(request, rdb, data):
data['#ip'] = data.get('#ip') or ip
@TaHandler
async def to_date(request, rdb, data):
"""
时间戳转日期字符串
:param request:
:param rdb:
:param data:
:return:
"""
data['#time'] = arrow.get(data['#time'], tzinfo='Asia/Shanghai').strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
@TaHandler
async def device_label(request, rdb, data):
"""

View File

@ -11,23 +11,26 @@ router = APIRouter()
@router.post("/")
async def point(request: Request, items: DataModel):
public_data: dict = restore_field(items.public)
rdb = request.app.state.redis
data_factory = request.app.state.data_factory
output_factory = request.app.state.output_factory
appid = items.public['b01']
output_factory.name = settings.OUTPUT_NAME[appid]
for item in items.data:
data: dict = restore_field(item)
sign = data.pop('sign')
properties = data.pop('properties')
if not check_sign(sign, settings.SALT.get('appid',''), data, properties):
# continue
return {"code": -1, 'msg': '签名错误'}
single_data = dict(**public_data, **data, properties=properties)
await asyncio.gather(
*map(lambda o: asyncio.create_task(o(request, rdb, single_data)), data_factory.handler_link))
msg = data_factory.format_data(single_data)
output_factory.send(msg)
try:
public_data: dict = restore_field(items.public)
rdb = request.app.state.redis
data_factory = request.app.state.data_factory
output_factory = request.app.state.output_factory
appid = items.public['b01']
output_factory.name = settings.OUTPUT_NAME[appid]
for item in items.data:
data: dict = restore_field(item)
sign = data.pop('sign')
properties = data.pop('properties')
if not check_sign(sign, settings.SALT.get(appid,''), data, properties):
# continue
return {"code": -1, 'msg': '签名错误'}
single_data = dict(**public_data, **data, properties=properties)
await asyncio.gather(
*map(lambda o: asyncio.create_task(o(request, rdb, single_data)), data_factory.handler_link))
msg = data_factory.format_data(single_data)
output_factory.send(msg)
return {"code": 0, 'msg': 'ok'}
return {"code": 0, 'msg': 'ok'}
except Exception as e:
return {"code": -1, 'msg': '数据异常'}

View File

@ -57,10 +57,12 @@ class Config:
'db': 1
}
SALT = {
'3F9AdWKZGhhNS2': 's4epprEG8DdyG5'
'3F9AdWKZGhhNS2': 's4epprEG8DdyG5',
'G5OYZLM21H': 's4epprEG8DdyG5'
}
OUTPUT_NAME = {
'3F9AdWKZGhhNS2': 'test'
'3F9AdWKZGhhNS2': 'test',
'G5OYZLM21H': 'test'
}
KAFKA_CONF = {
'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"],