From 5f38bd20a170ec9cf554f78a10e08a17c1fc08c5 Mon Sep 17 00:00:00 2001 From: wuaho Date: Wed, 13 Oct 2021 15:21:36 +0800 Subject: [PATCH] 1 --- api/api_v1/endpoints/query.py | 17 +++++++++++++---- models/behavior_analysis.py | 27 ++++++++++++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/api/api_v1/endpoints/query.py b/api/api_v1/endpoints/query.py index d5a4efb..b56e58f 100644 --- a/api/api_v1/endpoints/query.py +++ b/api/api_v1/endpoints/query.py @@ -740,6 +740,9 @@ async def scatter_model( for i, v in enumerate(quota_interval_arr[1:]): resp['label'].append(f'[{quota_interval_arr[i]},{v})') bins.append(v) + # 合计的 + if res['time_particle'] == 'total': + df['date'] = '合计' # 这是整体的 for key, tmp_df in df.groupby('date'): @@ -747,10 +750,16 @@ async def scatter_model( right=False).value_counts() bins_s.sort_index(inplace=True) total = int(bins_s.sum()) - resp['list'][key.strftime('%Y-%m-%d')] = dict() - resp['list'][key.strftime('%Y-%m-%d')]['总体'] = {'n': bins_s.to_list(), 'total': total, - 'p': round(bins_s * 100 / total, 2).to_list(), - 'title': '总体'} + if res['time_particle'] == 'total': + resp['list']['合计'] = dict() + resp['list']['合计']['总体'] = {'n': bins_s.to_list(), 'total': total, + 'p': round(bins_s * 100 / total, 2).to_list(), + 'title': '总体'} + else: + resp['list'][key.strftime('%Y-%m-%d')] = dict() + resp['list'][key.strftime('%Y-%m-%d')]['总体'] = {'n': bins_s.to_list(), 'total': total, + 'p': round(bins_s * 100 / total, 2).to_list(), + 'title': '总体'} # 分组的 if groupby: for key, tmp_df in df.groupby(['date', *groupby]): diff --git a/models/behavior_analysis.py b/models/behavior_analysis.py index 9d03395..fecd30e 100644 --- a/models/behavior_analysis.py +++ b/models/behavior_analysis.py @@ -625,10 +625,14 @@ ORDER BY level values_col = func.count(func.distinct(e_account_id_col)).label('values') if analysis in ['times', 'number_of_days', 'number_of_hours']: - - qry = sa.select(event_date_col, *self.groupby, values_col) \ - .where(and_(*where)) \ - .group_by(event_date_col, *self.groupby) + if self.time_particle == 'total': + qry = sa.select( *self.groupby, values_col) \ + .where(and_(*where)) \ + .group_by( *self.groupby) + else: + qry = sa.select(event_date_col, *self.groupby, values_col) \ + .where(and_(*where)) \ + .group_by(event_date_col, *self.groupby) sql = str(qry.compile(compile_kwargs={"literal_binds": True})) print(sql) @@ -644,11 +648,16 @@ ORDER BY level } elif event.get('quota'): event_attr_col = getattr(self.event_tbl.c, event['quota']) - - qry = sa.select(event_date_col, e_account_id_col, - settings.CK_FUNC[analysis](event_attr_col).label('values')) \ - .where(and_(*where)) \ - .group_by(event_date_col, *self.groupby, e_account_id_col) + if self.time_particle == 'total': + qry = sa.select( e_account_id_col, + settings.CK_FUNC[analysis](event_attr_col).label('values')) \ + .where(and_(*where)) \ + .group_by( *self.groupby, e_account_id_col) + else: + qry = sa.select(event_date_col, e_account_id_col, + settings.CK_FUNC[analysis](event_attr_col).label('values')) \ + .where(and_(*where)) \ + .group_by(event_date_col, *self.groupby, e_account_id_col) sql = str(qry.compile(compile_kwargs={"literal_binds": True})) print(sql) return {