创建一个mongo统计用户历次调用信息

创建一个mongo统计用户历次调用信息

建立与库链接的信息
from pymongo import MongoClient

client = MongoClient(‘mongodb://ip:port’, username=‘xx’, password=‘xxx’,
authSource=‘库名’)
db = client.ego_gw_db
collection_single_image = db[‘record’]
counter_client_data = db[‘counter’]

import re

from config import collection_single_image, counter_client_data
from pub import yesterday, weekly, which_month, today

class StatisticsClientData(object):
def current_date_total_data(self, appidlist=None, date=yesterday()):
# 当日调用次数,因为邮件是第二天发送,所以日期是昨天
if appidlist is None:
return []
else:
counter_list = []
for appId in appidlist:
counter = collection_single_image.find({‘appId’: appId, ‘recordDate’: date}).count()
counter_list.append(counter)
return counter_list

    def current_date_valid_data(self, appidlist=None, date=yesterday()):
        if appidlist is None:
            return []
        else:
            counter_list = []
            for appId in appidlist:
                counter = collection_single_image.find(
                    {'appId': appId, 'recordDate': date, 'recognitionList': {'$elemMatch': {'$ne': 'null'}}}).count()   # 匹配recognitionList!=0
                counter_list.append(counter)
            return counter_list

    def total_data(self, appidlist=None, date=yesterday()):
        """
        累计统计数据
        :param appId:
        :return:
        """
        if appidlist is None:
            return []
        else:
            counter_list = []
            for appId in appidlist:
                counter = collection_single_image.find({'appId': appId, 'recordDate': {'$lte': date}}).count()
                counter_list.append(counter)
        return counter_list

    def total_valid_data(self, appidlist=None, date=yesterday()):
        if appidlist is None:
            return []
        else:
            counter_list = []
            for appId in appidlist:
                counter = collection_single_image.find({'appId': appId, 'recordDate': {'$lte': date},
                                                        'recognitionList': {'$elemMatch': {'$ne': 'null'}}}).count()
                counter_list.append(counter)
            return counter_list

    def save_into_mongo(self, data1, data2, data3, data4, data5, data6, week=None, month=None):

        data = {
            'oppo_data_daily': data1,
            'oppo_valid_data_daily': data2,
            'vivo_data_daily': data3,
            'vivo_valid_data_daily': data4,
            'huawei_data_daily': data5,
            'huawei_valid_data_daily': data6,
            'recordDate': yesterday(),
            'createTime': today()
        }
        if not week:
            week_d = {}
        else:
            week_d = {
                'weekly_data': week[0],
                'weekly_data_last': week[1]
            }
            data.update()
        if not month:
            month_d = {}
        else:
            month_d = {
                'monthly_data': month[0],
                'monthly_data_last': month[1],
            }
        data.update(week_d)
        data.update(month_d)
        counter_client_data.insert_one(data)

    def before_day_data(self, date):
        # 前天的数据,在mongo里面查询
        data = counter_client_data.find({'recordDate': date},
                                        {'oppo_data_daily': 1, 'vivo_data_daily': 1, 'huawei_data_daily': 1})
        record = {}
        for record in data:
            record = record
        return record.get('oppo_data_daily'), record.get('vivo_data_daily'), record.get('huawei_data_daily')

    def weekly_data_statistic(self, appid_list=None, before=1):
        # before=1是上周的数据
        # ==2 是上上周的数据
        date = weekly(before=before)
        if appid_list is None:
            return []
        else:
            counter_list = []
            for appId in appid_list:
                counter = collection_single_image.find(
                    {'appId': appId, 'recordDate': {'$lte': date[1], '$gte': date[0]}}).count()
                counter_list.append(counter)
            return counter_list

    def monthly_data_statistic(self, date=which_month(), appid_list=None):
        # 每个月的数据统计
        regex = re.compile(date + "[0-9]+", re.IGNORECASE)
        # print(regex, '111111')
        if appid_list is None:
            return []
        else:
            counter_list = []
            for appId in appid_list:
                counter = collection_single_image.find({'appId': appId, 'recordDate': regex}).count()
                counter_list.append(counter)
            return counter_list
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值