diff --git a/api/api_v1/endpoints/dashboard.py b/api/api_v1/endpoints/dashboard.py index a00ced5..4633558 100644 --- a/api/api_v1/endpoints/dashboard.py +++ b/api/api_v1/endpoints/dashboard.py @@ -62,8 +62,10 @@ async def add_report(data_in: schemas.AddReport, ): """添加报表""" reports = [item.dict() for item in data_in.report_ids] - res = await crud.dashboard.update_one(db, {'_id': data_in.id}, - {'$push': {'reports': {'$each': reports}}}) + # res = await crud.dashboard.update_one(db, {'_id': data_in.id}, + # {'$push': {'reports': {'$each': reports}}}) + await crud.dashboard.update_one(db, {'_id': data_in.id}, + {'$set': {'reports': reports}}) return schemas.Msg(code=0, msg='ok', data='ok') diff --git a/api/api_v1/endpoints/query.py b/api/api_v1/endpoints/query.py index 48b10b2..c481bb8 100644 --- a/api/api_v1/endpoints/query.py +++ b/api/api_v1/endpoints/query.py @@ -101,6 +101,7 @@ async def event_model( q['values'].append(df['values'].to_list()) q['sum'].append(int(df['values'].sum())) q['date_range'] = [d.strftime('%Y-%m-%d %H:%M:%S') for d in q['date_range']] + # q['eventNameDisplay']=item['event_name_display'] res.append(q) return schemas.Msg(code=0, msg='ok', data=res) diff --git a/api/api_v1/endpoints/report.py b/api/api_v1/endpoints/report.py index 4f99c64..84191db 100644 --- a/api/api_v1/endpoints/report.py +++ b/api/api_v1/endpoints/report.py @@ -11,8 +11,6 @@ from api import deps router = APIRouter() - - @router.post("/create") async def create( request: Request, @@ -30,6 +28,22 @@ async def create( return schemas.Msg(code=0, msg='ok', data='创建成功') +@router.post("/edit") +async def edit( + request: Request, + data_in: schemas.ReportEdit, + game: str, + db: AsyncIOMotorDatabase = Depends(get_database), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """编辑报表""" + + await crud.report.update_one(db, {'_id': data_in.report_id}, + {'$set': {'query': data_in.query, 'name': data_in.name, 'desc': data_in.desc}}) + + return schemas.Msg(code=0, msg='ok', data='编辑成功') + + @router.post("/read_report") async def read_report( request: Request, diff --git a/main.py b/main.py index 7a0406b..6270829 100644 --- a/main.py +++ b/main.py @@ -110,4 +110,4 @@ async def add_process_time_header(request: Request, call_next): if __name__ == '__main__': - uvicorn.run(app='main:app', host="0.0.0.0", port=9861, reload=True, debug=True) + uvicorn.run(app='main:app', host="0.0.0.0", port=7889, reload=True, debug=True) diff --git a/models/behavior_analysis.py b/models/behavior_analysis.py index a2d2e61..9c497ee 100644 --- a/models/behavior_analysis.py +++ b/models/behavior_analysis.py @@ -1,5 +1,6 @@ from typing import Tuple +import arrow import sqlalchemy as sa import json @@ -42,6 +43,11 @@ class BehaviorAnalysis: report = await crud.report.get(db, id=self.data_in.report_id) self.event_view = report['query']['eventView'] self.events = report['query']['events'] + e_days, s_days = self.event_view['recentDay'].split('-') + self.event_view['endTime'] = arrow.get().shift(days=-int(e_days)).strftime('%Y-%m-%d 23:59:59') + self.event_view['startTime'] = arrow.get().shift(days=-int(s_days)).strftime('%Y-%m-%d 00:00:00') + + else: self.event_view = self.data_in.eventView self.events = self.data_in.events @@ -190,19 +196,19 @@ class BehaviorAnalysis: return func.sum(getattr(func, 'if')(getattr(self.event_tbl.c, '#event_name') == event_name, 1, 0)) elif comp == 'touch_user_count': return func.uniqCombined(getattr(func, 'if')(getattr(self.event_tbl.c, '#event_name') == event_name, - getattr(self.event_tbl.c, 'binduid'), 0)) + getattr(self.event_tbl.c, 'binduid'), '')) elif comp == 'touch_user_avg': return func.divide( func.sum(getattr(func, 'if')(getattr(self.event_tbl.c, '#event_name') == event_name, 1, 0)), func.uniqCombined(getattr(func, 'if')(getattr(self.event_tbl.c, '#event_name') == event_name, - getattr(self.event_tbl.c, 'binduid'), 0))) + getattr(self.event_tbl.c, 'binduid'), ''))) opt = ({'+', '-', '*', '/'} & set(s)).pop() a, b = s.split(opt) r1 = a.split('.') r2 = b.split('.') return {'event_name': [r1[0], r2[0]], - 'select': (settings.ARITHMETIC[opt](f(r1), f(r2))).label('values') + 'select': func.round(settings.ARITHMETIC[opt](f(r1), f(r2)), 2).label('values') } def event_model_sql(self): @@ -210,6 +216,7 @@ class BehaviorAnalysis: event_time_col = getattr(self.event_tbl.c, '#event_time') for event in self.events: + event_name_display = event.get('eventNameDisplay') select_exprs = [ settings.TIME_GRAIN_EXPRESSIONS[self.time_particle](event_time_col, self.zone_time)] @@ -273,7 +280,7 @@ class BehaviorAnalysis: sqls.append({'sql': sql, 'groupby': [i.key for i in self.groupby], 'date_range': self.date_range, - 'event_name': event_name + 'event_name': event_name_display or event_name, }) return sqls diff --git a/schemas/dashboard.py b/schemas/dashboard.py index 3c988ea..93ab20f 100644 --- a/schemas/dashboard.py +++ b/schemas/dashboard.py @@ -40,6 +40,7 @@ class DashboardMove(BaseModel): class Report(BaseModel): + name: str report_id: str graph_type: str model: str diff --git a/schemas/report.py b/schemas/report.py index 1231e0d..47893fc 100644 --- a/schemas/report.py +++ b/schemas/report.py @@ -23,6 +23,13 @@ class ReportCreate(ReportBase): cat: str +class ReportEdit(BaseModel): + report_id: str + query: dict + name: str + desc: str + + class ReportDelete(DBBase): pass