Beispiel #1
0
def calc_purchase_rate(target_date=None):
    # 首先获得本周新加入的用户
    user_collect = db['User']
    if target_date is None:
        start_date = local2utc(get_first_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
    else:
        start_date = local2utc(get_first_datetime_in_week(target_date - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(target_date - datetime.timedelta(days=7)))
    users = user_collect.find({'created_at': {'$gte': start_date}})
    user_count = 0
    once = 0  # 用户的一次购买人数
    twice = 0  # 用户的二次购买人数
    user_pay_collect = db['UserPay']
    for user in users:
        user_count += 1
        user_id = user.get('_id')
        user_pay_count = user_pay_collect.find({'user._id': user_id, 'status': 'PAY_SUCCESS', 'created_at': {'$gte': start_date, '$lt': end_date}}).count()
        if user_pay_count >= 1:
            once += 1
        elif user_pay_count > 1:
            twice += 1
    if user_count <= 0 or once <=0 :
        return 0, 0, once, twice, user_count
    return once / (user_count * 1.0), twice / (once * 1.0), once, twice, user_count
Beispiel #2
0
def paid_purchases(target_date=None):
    user_pay = db['UserPay']
    if target_date is None:
        start_date = local2utc(get_first_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
    else:
        start_date = local2utc(get_first_datetime_in_week(target_date - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(target_date - datetime.timedelta(days=7)))
    return user_pay.find({'status': 'PAY_SUCCESS', 'updated_at': {'$gte': start_date, '$lt': end_date}}).count()
Beispiel #3
0
def end_clazz_count(target_date=None):
    clazz_collect = db['Clazz']
    if target_date is None:
        start_date = local2utc(get_first_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
    else:
        start_date = local2utc(get_first_datetime_in_week(target_date - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(target_date - datetime.timedelta(days=7)))
    end_count = clazz_collect.find({'clazzType': {'$ne': 'LTS'}, 'endDate': {'$gte': start_date, '$lt': end_date}}).count()
    return end_count
Beispiel #4
0
def subscriptions(target_date=None):
    clazz_account = db['ClazzAccount']
    if target_date is None:
        start_date = local2utc(get_first_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(get_today_date() - datetime.timedelta(days=7)))
    else:
        start_date = local2utc(get_first_datetime_in_week(target_date - datetime.timedelta(days=7)))
        end_date = local2utc(get_last_datetime_in_week(target_date - datetime.timedelta(days=7)))
    return clazz_account.find({'status': 'PROCESSING', 'clazz.created_at': {'$gte': start_date, '$lt': end_date},
                               'clazz.clazzType': 'SEMESTER' }).count()
Beispiel #5
0
def end_clazz_count_global(target_date=None):
    clazz_collect = db['Clazz']
    if target_date is None:
        count = clazz_collect.find({'clazzType': {'$ne': 'LTS'}, 'status': 'CLOSE'}).count()
    else:
        target_datetime = local2utc(datetime.datetime(year=target_date.year, month=target_date.month, day=target_date.day))
        count = clazz_collect.find({'clazzType': {'$ne': 'LTS'}, 'status': 'CLOSE', 'endDate': {'$lt': target_datetime}}).count()
    return count
Beispiel #6
0
def purchase_rate(target_date=None, flag='day'):
    '''
    返回在 start_date 和 end_date 之间新增的用户,在整个时间段内的购买情况
    :param target_date: 目标日期(目标日期的这一天,这一周,这一个月)
    :param flag: day week month
    :return:
        one: 用户的一次购买量
        twice: 用户的二次购买量
        more: 用户的多次购买量
        pay_user_count: 付费用户总数
        once_rate: 用户的一次购买率
        twice_rate: 用户的二次购买率
        more_rate: 用户的三次以及三次以上购买率
    '''
    start_date_before, end_date_before, start_date, end_date = get_date_info(target_date, flag)
    user_collect = db['User']
    # start_date end_date之间的用户增长量
    growth_user_list = user_collect.find(
        {'created_at': {'$gte': local2utc(start_date), '$lt': local2utc(end_date)}}).distinct('_id')
    growth = len(growth_user_list)

    ca_collect = db['ClazzAccount']
    # 统计start_date end_date这段时间之内新增的用户,发生过购买行为的数量
    user_pay_info = ca_collect.aggregate(
        [
            {'$match': {'user._id': {'$in': growth_user_list}}},
            {'$group': {'_id': '$user._id', 'count': {'$sum': 1}}}
        ]
    )
    one = 0
    twice = 0
    more = 0
    for item in user_pay_info:
        if item['count'] == 3:
            more += 1
        elif item['count'] == 2:
            twice += 1
        elif item['count'] == 1:
            one += 1
    return one, twice, more, one + twice + more, (one * 1.0 / growth) if growth > 0 else 0, (twice * 1.0 / growth) \
        if growth > 0 else 0, (more * 1.0 / growth) if growth > 0 else 0
Beispiel #7
0
def user_growth(target_date=None, flag='day'):
    '''
    统计用户日、周、月的增长量、增长率
    :param target_date:
    :param flag:
    :return:
        growth 用户本日、周、月增长量
        growth_rate 用户本日、周、月增长率
        growth_pay 付费用户本日、周、月增长量
        growth_pay_rate 付费用户本日、周、月增长率
        pay_rate 新用户本日、周、月付费比率
    '''
    start_date_before, end_date_before, start_date, end_date = get_date_info(target_date, flag)
    user_collect = db['User']
    ca_collect = db['ClazzAccount']

    # 首先获得去前一个阶段用户的总数(今天,昨天23:59:59,这周,上周周日23:59:59之前)
    user_count = user_collect.find({'created_at': {'$lt': local2utc(end_date_before)}}).count()

    # 统计用户增长量
    growth_user_list = user_collect.find(
        {'created_at': {'$gte': local2utc(start_date), '$lt': local2utc(end_date)}}).distinct('_id')
    growth = len(growth_user_list)

    # 统计付费用户增长量
    growth_pay_user_list = ca_collect.find({'created_at': {'$gte': local2utc(start_date), '$lt': local2utc(end_date)},
                                           '$or': [{'status': 'WAITENTER'}, {'status': 'PROCESSING'},
                                                   {'status': 'CLOSE'}]}).distinct('user._id')
    growth_pay = len(growth_pay_user_list)

    # 统计新增长的用户中  付费用户  的数量
    new_pay = len(set(growth_user_list) & set(growth_pay_user_list))

    growth_pay_count = len(
        ca_collect.find({'created_at': {'$lt': local2utc(end_date_before)},
                         '$or': [{'status': 'WAITENTER'}, {'status': 'PROCESSING'}, {'status': 'CLOSE'}]}).distinct('user._id'))
    # today用户增长量,yesterday 用户增长量,today用户增长率, today付费用户增长量,yesterday付费用户增长量,today付费用户增长量
    return growth, growth * 1.0 / user_count, growth_pay, growth_pay * 1.0 / growth_pay_count, (new_pay * 1.0 / growth)\
        if growth > 0 else 0
Beispiel #8
0
def calc_purchase_rate_global(target_date=None):
    user_collect = db['User']
    users = user_collect.find({})
    user_count = 0
    once = 0
    twice = 0
    user_pay_collect = db['UserPay']
    for user in users:
        user_count += 1
        user_id = user.get('_id')
        if target_date is None:
            user_pay_count = user_pay_collect.find({'user._id': user_id, 'status': 'PAY_SUCCESS'}).count()
        else:
            target_datetime = local2utc(datetime.datetime(year=target_date.year, month=target_date.month, day=target_date.day))
            user_pay_count = user_pay_collect.find({'user._id': user_id, 'status': 'PAY_SUCCESS', 'created_at': {'$lt': target_datetime}}).count()
        if user_pay_count >= 1:
            once += 1
        elif user_pay_count > 1:
            twice += 1
    if user_count <= 0 or once <= 0:
        return 0, 0, once, twice, user_count
    return once / (user_count * 1.0), twice / (once * 1.0), once, twice, user_count