优化分布分析

This commit is contained in:
wuaho 2021-09-16 11:41:23 +08:00
parent 5b91b6490f
commit e1ab6c5b6d
4 changed files with 49 additions and 20 deletions

View File

@ -653,13 +653,23 @@ async def scatter_model(
return schemas.Msg(code=0, msg='ok', data=resp) return schemas.Msg(code=0, msg='ok', data=resp)
if interval_type == 'def' and analysis == 'number_of_days': # elif analysis == 'number_of_days':
else:
resp = {'list': {}} resp = {'list': {}}
for key, tmp_df in df.groupby('date'): for key, tmp_df in df.groupby('date'):
total = round(tmp_df['values'].sum(), 2) total = int(tmp_df['values'].sum())
resp['list'][key.strftime('%Y-%m-%d')] = {'n': total, 'total': total, 'p': 100} resp['list'][key.strftime('%Y-%m-%d')] = {'n': total, 'total': total, 'p': 100}
return schemas.Msg(code=0, msg='ok', data=resp) return schemas.Msg(code=0, msg='ok', data=resp)
# bins_s = pd.cut(tmp_df['values'], bins=bins,
# 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': '总体'}
@router.post("/trace_model_sql") @router.post("/trace_model_sql")
async def trace_model_sql( async def trace_model_sql(

View File

@ -230,16 +230,20 @@ class Settings(BaseSettings):
], ],
'datetime': [ 'datetime': [
{ {
'id': '==', 'id': '>',
'title': '绝对时间' 'title': '大于'
}, },
{ {
'id': '==', 'id': '>=',
'title': '相对当前日期' 'title': '大于等于'
}, },
{ {
'id': '==', 'id': '<',
'title': '相对事件发生时刻' 'title': '小于'
},
{
'id': '<=',
'title': '小于等于'
}, },
{ {
'id': 'is not null', 'id': 'is not null',

View File

@ -135,6 +135,9 @@ class BehaviorAnalysis:
report = await crud.report.get(db, id=self.data_in.report_id) report = await crud.report.get(db, id=self.data_in.report_id)
self.event_view = report['query']['eventView'] self.event_view = report['query']['eventView']
self.events = report['query']['events'] self.events = report['query']['events']
if self.event_view.get('date_type') == 'static':
pass
else:
try: try:
e_days = self.event_view['e_days'] e_days = self.event_view['e_days']
s_days = self.event_view['s_days'] s_days = self.event_view['s_days']
@ -415,6 +418,7 @@ class BehaviorAnalysis:
for event in self.events: for event in self.events:
event_name_display = event.get('eventNameDisplay') event_name_display = event.get('eventNameDisplay')
select_exprs = [] select_exprs = []
if self.time_particle != 'total': if self.time_particle != 'total':
select_exprs.append( select_exprs.append(
@ -590,6 +594,7 @@ ORDER BY level
event_name = event['eventName'] event_name = event['eventName']
analysis = event['analysis'] analysis = event['analysis']
e_account_id_col = getattr(self.event_tbl.c, '#account_id') e_account_id_col = getattr(self.event_tbl.c, '#account_id')
u_account_id_col = getattr(self.user_tbl.c, '#account_id')
event_name_col = getattr(self.event_tbl.c, '#event_name') event_name_col = getattr(self.event_tbl.c, '#event_name')
event_time_col = getattr(self.event_tbl.c, '#event_time').label('date') event_time_col = getattr(self.event_tbl.c, '#event_time').label('date')
event_date_col = settings.TIME_GRAIN_EXPRESSIONS[self.time_particle](event_time_col, self.zone_time) event_date_col = settings.TIME_GRAIN_EXPRESSIONS[self.time_particle](event_time_col, self.zone_time)
@ -601,11 +606,15 @@ ORDER BY level
# event_date_col <= self.end_date, # event_date_col <= self.end_date,
func.addHours(event_time_col, self.zone_time) >= self.start_date, func.addHours(event_time_col, self.zone_time) >= self.start_date,
func.addHours(event_time_col, self.zone_time) <= self.end_date, func.addHours(event_time_col, self.zone_time) <= self.end_date,
event_name_col == event_name
] ]
event_filter, _ = self.handler_filts((event['filts'], event.get('relation', 'and')), if event_name != '*':
where.append(event_name_col == event_name)
event_filter, user_filter = self.handler_filts((event['filts'], event.get('relation', 'and')),
(self.global_filters, self.global_relation) (self.global_filters, self.global_relation)
, self.ext_filters) , self.ext_filters)
if user_filter:
where.append(e_account_id_col.in_(sa.select(u_account_id_col).where(*user_filter)))
where.extend(event_filter) where.extend(event_filter)
values_col = func.count().label('values') values_col = func.count().label('values')
if analysis in ['number_of_days', 'number_of_hours']: if analysis in ['number_of_days', 'number_of_hours']:

View File

@ -0,0 +1,6 @@
with '2021-09-15' as date
select owner_name, uniqExact(`#account_id`) as n
from xiangsu.event
where `#event_name` = 'create_account'
and toDate(addHours(`#event_time`, 8)) = date
group by owner_name