def handler_function(self):
        args = self.get_request_data()
        userType = args.get('userType', None)
        params = []

        if userType and userType != 'all':
            params.append({'$match': {'userId': int(userType)}})
        params.extend([{
            '$group': {
                '_id': '$contactName',
                'num': {
                    '$sum': 1
                }
            }
        }, {
            '$sort': {
                'num': -1
            }
        }])
        top10Contacts = list(DBOps.getAggregate(DBCollonfig.orders, params))

        if top10Contacts:
            if len(top10Contacts) <= 10:
                self.result['result'] = top10Contacts
            else:
                self.result['result'] = top10Contacts[:10]
            return self.response_success()
        else:
            return self.response_failure(msg=u'暂无数据')
Exemple #2
0
    def getlist(cls, page, pageSize, logType):
        skip = (page - 1) * pageSize

        # 条件搜搜参数
        params = []

        if logType != 'all':
            params.append({
                '$match': {
                    'userId': int(logType)
                }
            })

        params.extend([{
            '$lookup': {
                'from': "users",
                'localField': "userId",
                'foreignField': "_id",
                'as': "user"
            }
        }, {
            '$sort': {'logTimeStamp': -1},
        }, {
            '$skip': skip
        }, {
            '$limit': pageSize
        }])

        searchLogs = DBOps.getAggregate(DBCollonfig.log, params)

        resLogs = []
        for log in searchLogs:
            resLogs.append({
                'userId': log['userId'],
                'username': log['user'][0]['username'],
                'logTime': log['logTime'],
                'action': log['action']
            })

        # 求和搜索参数
        params = []
        if logType != 'all':
            params.append({
                '$match': {
                    'userId': int(logType)
                }
            })
        params.append({'$group': {'_id': None, 'count': {'$sum': 1}}})

        totalCount = list(
            DBOps.getAggregate(DBCollonfig.log, params)
        )[0]['count']

        res = {
            'page': page,
            'pageSize': pageSize,
            'totalCount': totalCount,
            'logs': resLogs
        }

        return res