data_cleaning/task/summary_func.py
2021-02-01 18:33:39 +08:00

57 lines
2.3 KiB
Python

import traceback
from typing import List, Dict
from pymongo import UpdateOne
from pydantic import Field
import pandas as pd
from .task import Task
from utils import *
from model import GBaseModel, MdbObjectId
class SummaryFunc(Task):
"""
功能分析
"""
class Model(GBaseModel):
id: MdbObjectId = Field(..., title="id", alias='_id')
prize: List[Dict] = Field(None, title='奖励')
need: List[Dict] = Field(None, title='消耗')
ftype: str = Field(..., title='功能')
data: dict = Field(None, title='功能数据')
cdate: int = Field(..., title='当天0点')
def cleaning(self, cursor_list):
for cursor in cursor_list: # type:dict
for source_coll, ts in cursor.items(): # type:str,dict
if ts['cursor_st'] == ts['cursor_et']:
continue
logger.info(f'开始处理{self.game_name} 处理 {source_coll} 游标 {ts}')
where = {
'_event_name': 'Func',
'_event_time': {
'$gte': ts['cursor_st'],
'$lt': ts['cursor_et'],
}
}
projection = self.Model.get_fields()
bulk_data = []
for item in self.local_db[source_coll].find(where, projection):
try:
item['cdate'] = int(pd.Timestamp(item['_event_time'], unit='s', tz=self.timezone) \
.normalize().timestamp())
model = self.Model(**item)
data = model.dict(by_alias=True)
bulk_data.append(UpdateOne({'_id': data['_id']}, {'$set': data}, upsert=True))
except Exception as e:
msg = traceback.format_exc()
ddsend_msg(f'{self.game_name} {source_coll} 功能打点 {item["ftype"]} 字段异常 {msg}')
logger.error(f'ftype {item["ftype"]} 字段异常 {msg}')
if bulk_data:
self.local_db[self.dest_coll].bulk_write(bulk_data, ordered=False)
self.remote_db[self.dest_coll].bulk_write(bulk_data, ordered=False)
self.set_cursor(cursor_st=ts['cursor_st'], cursor_et=ts['cursor_et'])