diff --git a/api/api_v1/endpoints/query.py b/api/api_v1/endpoints/query.py index 97bd6d7..584398b 100644 --- a/api/api_v1/endpoints/query.py +++ b/api/api_v1/endpoints/query.py @@ -469,7 +469,7 @@ async def trace_model_sql( return schemas.Msg(code=0, msg='ok', data=data) -@router.post("/user_property_sql") +@router.post("/user_property_model_sql") async def user_property_sql( request: Request, game: str, @@ -494,13 +494,29 @@ async def user_property_model( await analysis.init() res = analysis.property_model() sql = res['sql'] + quota = res['quota'] groupby = res['groupby'] df = await ckdb.query_dataframe(sql) # 没有分组 data = {'groupby': groupby} - if not groupby: - data['总体'] = df['values'][0] - else: - data = df.groupby(groupby).sum().to_dict() + title = [] - return schemas.Msg(code=0, msg='ok', data=data) + if not groupby: + data['总体'] = int(df['values'][0]) + title = ['总体', quota] + + else: + sum_s = df.groupby(groupby)['values'].sum().sort_values(ascending=False) + data = dict() + for key, val in sum_s.items(): + if isinstance(key, tuple): + key = ','.join([str(i) for i in key]) + else: + key = str(key) + data[key] = val + title = ['.'.join(groupby), quota] + + return schemas.Msg(code=0, msg='ok', data={ + 'value': data, + 'title': title + }) diff --git a/models/user_analysis.py b/models/user_analysis.py index 5326e7a..61ac4b9 100644 --- a/models/user_analysis.py +++ b/models/user_analysis.py @@ -92,7 +92,7 @@ class UserAnalysis: return where def property_model(self): - event = self.events[0] + event = self.events selectd = getattr(self.user_tbl.c, event['quota']) qry = sa.select(selectd) @@ -122,6 +122,7 @@ class UserAnalysis: print(sql) result = {'sql': sql, 'groupby': [i.key for i in self.groupby], + 'quota':event['quota'] } return result