diff --git a/api/api_v1/endpoints/query.py b/api/api_v1/endpoints/query.py index 54cc652..a024fb2 100644 --- a/api/api_v1/endpoints/query.py +++ b/api/api_v1/endpoints/query.py @@ -783,6 +783,9 @@ async def user_property_model( quota = res['quota'] groupby = res['groupby'] df = await ckdb.query_dataframe(sql) + if len(df) == 0: + return schemas.Msg(code=0, msg='查无数据', data=None) + # 没有分组 data = {'groupby': groupby} title = [] diff --git a/models/user_analysis.py b/models/user_analysis.py index 67839cf..107f138 100644 --- a/models/user_analysis.py +++ b/models/user_analysis.py @@ -33,6 +33,8 @@ class UserAnalysis: self.time_particle = None self.date_range = None self.unit_num = None + self.global_relation = 'and' + self.ext_filters = (self.data_in.ext_filter.get('filts', []), self.data_in.ext_filter.get('relation', 'and')) async def init(self, *args, **kwargs): if self.data_in.report_id: @@ -50,11 +52,11 @@ class UserAnalysis: self.time_particle = self._get_time_particle_size() self.groupby = self._get_group_by() self.unit_num = self._get_unit_num() + self.global_relation = self.event_view.get('relation', 'and') # 用户自带过滤 if 'data_where' in kwargs: self.global_filters.extend(kwargs['data_where'].get(self.game, [])) - self.global_filters.extend(self.data_in.ext_filter) async def _init_table(self): """ 从redis中取出表字段,构建表结构 @@ -77,47 +79,107 @@ class UserAnalysis: def _get_zone_time(self): return int(self.event_view.get('zone_time', 8)) - def _get_filters(self, filters): - tbl = self.user_tbl - where = [] - for item in filters: - col = getattr(tbl.c, item['columnName']) + # def _get_filters(self, filters): + # tbl = self.user_tbl + # where = [] + # for item in filters: + # col = getattr(tbl.c, item['columnName']) + # + # comparator = item['comparator'] + # ftv = item['ftv'] + # if comparator == '==': + # if len(ftv) > 1: + # where.append(or_(*[col == v for v in ftv])) + # else: + # where.append(col == ftv[0]) + # elif comparator == '>=': + # where.append(col >= ftv[0]) + # elif comparator == '<=': + # where.append(col <= ftv[0]) + # elif comparator == '>': + # where.append(col > ftv[0]) + # elif comparator == '<': + # where.append(col < ftv[0]) + # + # elif comparator == 'is not null': + # where.append(col.isnot(None)) + # elif comparator == 'is null': + # where.append(col.is_(None)) + # + # elif comparator == '!=': + # where.append(col != ftv[0]) + # + # elif comparator == 'like': + # where.append(col.like(f'%{ftv[0]}%')) + # + # elif comparator == 'not like': + # where.append(col.notlike(f'%{ftv[0]}%')) + # + # elif comparator == 'in': + # where.append(col.in_(ftv)) + # + # + # return where - comparator = item['comparator'] - ftv = item['ftv'] - if comparator == '==': - if len(ftv) > 1: - where.append(or_(*[col == v for v in ftv])) - else: - where.append(col == ftv[0]) - elif comparator == '>=': - where.append(col >= ftv[0]) - elif comparator == '<=': - where.append(col <= ftv[0]) - elif comparator == '>': - where.append(col > ftv[0]) - elif comparator == '<': - where.append(col < ftv[0]) + def handler_filts(self, *filters): + """ + :param filters: (filts:list,relation:str) + :param g_f: + :param relation: + :return: + """ - elif comparator == 'is not null': - where.append(col.isnot(None)) - elif comparator == 'is null': - where.append(col.is_(None)) + user_filters = [] + for filter in filters: + filts = filter[0] + relation = filter[1] + user_filter = [] + for item in filts: - elif comparator == '!=': - where.append(col != ftv[0]) + where = user_filter - elif comparator == 'like': - where.append(col.like(f'%{ftv[0]}%')) + col = sa.Column(item['columnName']) - elif comparator == 'not like': - where.append(col.notlike(f'%{ftv[0]}%')) + comparator = item['comparator'] + ftv = item['ftv'] + if comparator == '==': + if len(ftv) > 1: + where.append(or_(*[col == v for v in ftv])) + else: + where.append(col == ftv[0]) + elif comparator == '>=': + where.append(col >= ftv[0]) + elif comparator == '<=': + where.append(col <= ftv[0]) + elif comparator == '>': + where.append(col > ftv[0]) + elif comparator == '<': + where.append(col < ftv[0]) - elif comparator == 'in': - where.append(col.in_(ftv)) + elif comparator == 'is not null': + where.append(col.isnot(None)) + elif comparator == 'is null': + where.append(col.is_(None)) + elif comparator == 'like': + where.append(col.like(f'%{ftv[0]}%')) - return where + elif comparator == 'not like': + where.append(col.notlike(f'%{ftv[0]}%')) + + elif comparator == 'in': + where.append(col.in_(ftv)) + + elif comparator == '!=': + where.append(col != ftv[0]) + if relation == 'and': + if user_filter: + user_filters.append(and_(*user_filter)) + else: + if user_filter: + user_filters.append(or_(*user_filter)) + + return user_filters def property_model(self): event = self.events @@ -140,7 +202,10 @@ class UserAnalysis: func.round(getattr(func, analysis)(getattr(self.user_tbl.c, event['quota'])), 2).label( 'values')] - where = self._get_filters(event['filts']+self.global_filters) + where = self.handler_filts((event['filts'], event.get('relation')), + (self.global_filters, self.global_relation), + self.ext_filters + ) qry = sa.select((*self.groupby, *selectd)).where(*where) qry = qry.group_by(*self.groupby) diff --git a/schemas/dashboard.py b/schemas/dashboard.py index b94d0cd..ca0f113 100644 --- a/schemas/dashboard.py +++ b/schemas/dashboard.py @@ -32,6 +32,7 @@ class Report(BaseModel): name: str report_id: str graph_type: str + ascending: bool model: str graph_size: str sort: int = 1