def quick_search_result_counter(request_user: User): regex = request.args.get('searchValue', Search.DEFAULT_REGEX, str) plb = PipelineBuilder() regex = plb.regex_('fields.value', f'{regex}', 'ims') pipe_match = plb.match_(regex) plb.add_pipe(pipe_match) pipe_count = plb.count_('count') plb.add_pipe(pipe_count) pipeline = plb.pipeline try: result = list( object_manager.aggregate(collection='framework.objects', pipeline=pipeline)) except Exception as err: LOGGER.error(f'[Search count]: {err}') return abort(400) if len(result) > 0: return make_response(result[0]['count']) else: return make_response(0)
def quick_search_result_counter(): regex = request.args.get('searchValue', Search.DEFAULT_REGEX, str) plb = PipelineBuilder() regex = plb.regex_('fields.value', f'{regex}', 'ims') pipe_and = plb.and_([ regex, { 'active': { "$eq": True } } if _fetch_only_active_objs() else {} ]) pipe_match = plb.match_(pipe_and) plb.add_pipe(pipe_match) plb.add_pipe( {'$group': { "_id": { 'active': '$active' }, 'count': { '$sum': 1 } }}) plb.add_pipe({ '$group': { '_id': 0, 'levels': { '$push': { '_id': '$_id.active', 'count': '$count' } }, 'total': { '$sum': '$count' } } }) plb.add_pipe({'$unwind': '$levels'}) plb.add_pipe({'$sort': {"levels._id": -1}}) plb.add_pipe({ '$group': { '_id': 0, 'levels': { '$push': { 'count': "$levels.count" } }, "total": { '$avg': '$total' } } }) plb.add_pipe({ '$project': { 'total': "$total", 'active': { '$arrayElemAt': ["$levels", 0] }, 'inactive': { '$arrayElemAt': ["$levels", 1] } } }) plb.add_pipe({ '$project': { '_id': 0, 'active': { '$cond': [{ '$ifNull': ["$active", False] }, '$active.count', 0] }, 'inactive': { '$cond': [{ '$ifNull': ['$inactive', False] }, '$inactive.count', 0] }, 'total': '$total' } }) pipeline = plb.pipeline try: result = list( object_manager.aggregate(collection='framework.objects', pipeline=pipeline)) except Exception as err: LOGGER.error(f'[Search count]: {err}') return abort(400) if len(result) > 0: return make_response(result[0]) else: return make_response({'active': 0, 'inactive': 0, 'total': 0})