Esempio n. 1
0
 async def post(self, choice_rule_cid):
     r_dict = {'code': 0}
     race_cid = self.get_argument('race_cid', '')
     try:
         if race_cid:
             race_choice_rule = await RaceSubjectChoiceRules.find_one(
                 filtered={
                     'race_cid': race_cid,
                     'cid': choice_rule_cid
                 })
             if race_choice_rule:
                 if RedisCache.hget(
                         KEY_PREFIX_EXTRACTING_SUBJECT_RULE,
                         race_choice_rule.cid) in [b'0', 0, None]:
                     RedisCache.hset(race_cid, race_choice_rule.cid, 0)
                     RedisCache.hset(KEY_PREFIX_EXTRACTING_SUBJECT_RULE,
                                     race_choice_rule.cid, 1)
                     start_race_extract_subjects.delay(
                         race_cid, race_choice_rule)
                     r_dict['code'] = 1  # 任务已提交
                 else:
                     r_dict['code'] = -1  # 任务在执行
             else:
                 r_dict['code'] = 2
     except Exception:
         logger.error(traceback.format_exc())
     return r_dict
Esempio n. 2
0
def set_cached_subject_accuracy(subject_code, correct=False):
    """
     # 缓存题目结果便于统计
    :param subject_code: 题目编号
    :param correct: 是否回答正确
    :return:
    """

    if subject_code:
        cache_key = '%s-E' % subject_code
        if correct:
            cache_key = '%s-C' % subject_code
        value = RedisCache.hget(KEY_CACHE_SUBJECT_RESULT, cache_key)
        if not value:
            value = 0
        value = int(value) + 1
        RedisCache.hset(KEY_CACHE_SUBJECT_RESULT, cache_key, value)
Esempio n. 3
0
    async def post(self, choice_rule_id):
        r_dict = {'code': 0}
        try:
            choice_rule = await SubjectChoiceRules.get_by_cid(choice_rule_id)
            if choice_rule:
                if RedisCache.hget(KEY_PREFIX_EXTRACTING_SUBJECT_RULE, choice_rule.cid) in [b'0', 0, None]:
                    times = int(self.get_argument('times', 1))

                    RedisCache.hset(KEY_EXTRACTING_SUBJECT_RULE, choice_rule.cid, 0)
                    RedisCache.hset(KEY_PREFIX_EXTRACTING_SUBJECT_RULE, choice_rule.cid, 1)

                    start_extract_subjects.delay(choice_rule, times)

                    r_dict['code'] = 1  # 任务已提交
                else:
                    r_dict['code'] = -1  # 任务在执行
        except RuntimeError:
            logger.error(traceback.format_exc())
        return r_dict
Esempio n. 4
0
            async def lottery():
                basic_setting = await RedPacketBasicSetting.find_one({'rule_cid': rule_cid, 'record_flag': 1},
                                                                     read_preference=ReadPreference.PRIMARY)

                prize_list, err_index = await get_lottery_table(rule_cid)

                history = await MemberCheckPointHistory.find_one(
                    {'member_cid': member.cid, 'check_point_cid': checkpoint_cid,
                     'status': STATUS_RESULT_CHECK_POINT_WIN}, read_preference=ReadPreference.PRIMARY)
                if not history:
                    r_dict['code'] = 1005
                    return r_dict

                redpkt_box = await RedPacketBox.find_one(
                    {'member_cid': member.cid, 'checkpoint_cid': checkpoint_cid, 'record_flag': 1})
                if redpkt_box:
                    if redpkt_box.award_cid:
                        item = await RedPacketItemSetting.find_one({'cid': redpkt_box.award_cid})  # 该奖励的配置信息

                        r_dict['result'] = RESULT_RACE_LOTTERY_WIN
                        r_dict['reward_msg'] = '您已经抽过奖了, 获得奖品为[ %s ].' % item.title
                        r_dict['reward_position'] = prize_list.index(item.title)
                        await clear_lottery_notice(member.cid, checkpoint_cid)

                    else:
                        r_dict['result'] = RESULT_RACE_LOTTERY_LOSE
                        r_dict['reward_msg'] = '您已经在此关抽过奖了, 上一次没有中奖'
                        r_dict['reward_position'] = err_index

                    await clear_lottery_notice(member.cid, checkpoint_cid)
                    return r_dict

                result = RedisCache.hget(KEY_RACE_LOTTERY_RESULT.format(checkpoint_cid), member.cid)
                if result is not None:
                    # 数据库被人为清除过
                    r_dict['code'] = 1004
                    return r_dict

                start_lottery_queuing.delay(member.cid, race_cid, rule, checkpoint_cid)
                t1 = time.time()
                while time.time() - t1 <= 5:
                    result = RedisCache.hget(KEY_RACE_LOTTERY_RESULT.format(checkpoint_cid), member.cid)
                    if result:
                        break
                    await asyncio.sleep(1)

                if result is None:
                    r_dict['result'] = RESULT_RACE_LOTTERY_OUT_OF_TIME
                    r_dict['reward_msg'] = '网络异常, 请稍后重新尝试抽奖.'
                    return r_dict

                result = int(result.decode('utf-8'))
                if result == RESULT_RACE_LOTTERY_LOSE:
                    r_dict['result'] = RESULT_RACE_LOTTERY_LOSE
                    r_dict['reward_msg'] = basic_setting.fail_msg
                    r_dict['reward_position'] = err_index

                    await clear_lottery_notice(member.cid, checkpoint_cid)
                    return r_dict

                if result == RESULT_RACE_LOTTERY_LOSE_LATE:
                    r_dict['result'] = RESULT_RACE_LOTTERY_LOSE
                    r_dict['reward_msg'] = basic_setting.over_msg
                    r_dict['reward_position'] = err_index

                    await clear_lottery_notice(member.cid, checkpoint_cid)
                    return r_dict

                if result == RESULT_RACE_LOTTERY_WIN:
                    r_dict['result'] = RESULT_RACE_LOTTERY_WIN
                    redpacket = await RedPacketBox.find_one(
                        {'member_cid': member.cid, 'checkpoint_cid': checkpoint_cid,
                         'draw_status': STATUS_REDPACKET_NOT_AWARDED}, read_preference=ReadPreference.PRIMARY)
                    redpacket_item = await RedPacketItemSetting.find_one({'cid': redpacket.award_cid})

                    r_dict['reward_msg'] = redpacket.award_msg
                    r_dict['reward_position'] = prize_list.index(redpacket_item.title)

                    await async_add_notice(member_cid=member.cid, race_cid=race_cid, checkpoint_cid=checkpoint_cid,
                                           redpkt_box=redpacket, msg_type=TYPE_MSG_DRAW)
                    await clear_lottery_notice(member.cid, checkpoint_cid)
                    return r_dict
Esempio n. 5
0
    async def post(self):
        res_code = {'code': 0, 'msg': ''}
        subject_dimension_list = await SubjectDimension.aggregate([
            MatchStage({'parent_cid': None}),
            SortStage([('ordered', ASC)]),
            LookupStage(SubjectDimension, 'cid', 'parent_cid', 'sub_list')
        ]).to_list(None)

        search_arguments = {}

        match_dict = {}
        m_province_code_list, m_city_code_list, _ = await do_different_administrative_division2(
            self.current_user.manage_region_code_list)
        if m_province_code_list:
            match_dict['province_code'] = {'$in': m_province_code_list}
        if m_city_code_list:
            match_dict['city_code'] = {'$in': m_city_code_list}

        # 维度信息
        dimension_dict = {}
        for dimension in subject_dimension_list:
            t_dimension = self.get_argument(dimension.cid, '')
            if t_dimension:
                dimension_dict['%s' % dimension.cid] = t_dimension
            search_arguments[dimension.cid] = t_dimension

        query_params = {}
        s_province = self.get_argument('province', '')
        if s_province:
            query_params['province_code'] = s_province
        search_arguments['province'] = s_province

        s_city = self.get_argument('city', '')
        if s_city:
            query_params['city_code'] = s_city
        search_arguments['city'] = s_city

        s_age_group = self.get_argument('age_group', '')
        if s_age_group:
            query_params['age_group'] = int(s_age_group)
        search_arguments['age_group'] = s_age_group

        s_gender = self.get_argument('gender', '')
        if s_gender:
            query_params['gender'] = int(s_gender)
        search_arguments['gender'] = s_gender

        s_education = self.get_argument('education', '')
        if s_education:
            query_params['education'] = int(s_education)
        search_arguments['education'] = s_education

        try:
            category = parse_search_params(query_params)
            category_list = await ReportSubjectStatisticsMiddle.distinct('category')

            result = RedisCache.hget(KEY_CACHE_REPORT_CONDITION, str(category))
            logger.info('CATEGORY: %s, result is %s' % (str(category), result))

            if result is None:
                res_code['msg'] = '数据截至日期为:当前时间'
                start_split_subject_stat_task.delay(category, datetime.datetime.now())
                param_dict = await self.do_paging_from_member_subject_statistics(match_dict, query_params,
                                                                                 dimension_dict,
                                                                                 search_arguments)
            else:
                result = int(result.decode())
                if result == STATUS_SUBJECT_STATISTICS_END:
                    param_dict = await self.do_paging_from_report_subject_statistics_middle(category, match_dict,
                                                                                            query_params,
                                                                                            dimension_dict,
                                                                                            search_arguments)

                    item = param_dict.get('paging').page_items
                    if item:
                        res_code['msg'] = '数据截至日期为:%s' % datetime2str(item[0].task_dt)
                else:
                    res_code['msg'] = '数据截至日期为:当前时间'
                    param_dict = await self.do_paging_from_member_subject_statistics(match_dict, query_params,
                                                                                     dimension_dict,
                                                                                     search_arguments)
            param_dict.update(locals())
            param_dict.pop('self')
            html = self.render_string('backoffice/reports/subject_analysis_table_list.html', **param_dict).decode(
                'utf-8')

            res_code['code'] = 1
            res_code['html'] = self.render_string('backoffice/reports/subject_analysis_table_list.html',
                                                  **param_dict).decode('utf-8')
        except Exception:
            logger.error(traceback.format_exc())
        return res_code
Esempio n. 6
0
async def do_rank_statistic(race_cid: str,
                            time_match: MatchStage,
                            group_id='district',
                            name_match=MatchStage({}),
                            district_title="",
                            name="",
                            time_num=""):
    """
    统计活动信息

    :param race_cid:
    :param time_match:
    :param group_id:
    :param name_match
    :param district_title
    :param
    :return:
    """
    if not race_cid:
        return

    cache_key = generate_cache_key(
        'member_times_{district}_{name}_{time_num}_{district_title}'.format(
            district=group_id,
            name=name,
            time_num=time_num,
            district_title=district_title))
    member_times_data = RedisCache.hget(race_cid, cache_key)
    if not member_times_data:
        race = await Race.get_by_cid(race_cid)

        city_list = await AdministrativeDivision.distinct(
            'code', {'parent_code': race.province_code})
        city_name_list = await AdministrativeDivision.distinct(
            'title', {'parent_code': race.province_code})
        dist_list = []
        for city in city_list:
            dist_list += await AdministrativeDivision.distinct(
                'title', {'parent_code': city})
        district_match = MatchStage({})
        all_match = {
            'race_cid': race_cid,
            'province': {
                '$ne': None
            },
            'district': {
                '$in': dist_list
            },
            'city': {
                '$in': city_name_list
            }
        }

        if district_title:
            district_match = MatchStage({'district': district_title})
            all_match['city'] = {'$in': city_name_list}
            all_match['town'] = {'$ne': None}
        cursor = RaceMemberEnterInfoStatistic.aggregate([
            MatchStage(all_match), district_match, time_match, name_match,
            GroupStage(group_id,
                       enter_times_sum={'$sum': '$enter_times'},
                       people_sum={'$sum': '$increase_enter_count'},
                       true_answer_times_sum={'$sum': '$true_answer_times'},
                       answer_times_sum={'$sum': '$answer_times'}),
            SortStage([('enter_times_sum', DESC)])
        ])
        times_data = await stat_data(cursor)
        logger_cache.info('cache_key: %s' % cache_key)
        RedisCache.hset(race_cid, cache_key, msgpack.packb(times_data))
        return times_data
    return msgpack.unpackb(member_times_data, raw=False)
Esempio n. 7
0
def do_init():
    """
    初始该活动数据
    :return:
    """
    cache_key = 'race_report_script'
    RedisCache.delete(cache_key)

    cp_map = get_all_race_checkpoint_map()
    last_map = get_all_last_checkpoint()

    with open('./chekpt_history.csv', encoding='utf-8') as f:
        csv_reader = csv.reader(f)

        for index, line in enumerate(csv_reader):
            try:
                if index == 0:
                    continue
                # member_cid,check_point_cid,status,created_dt
                his = MemberCheckPointHistory()
                his.member_cid = line[0]
                his.check_point_cid = line[1]
                his.status = line[2]

                line3 = line[3].replace('T', ' ').split('.')[0]
                c_dt = str2datetime(line3)
                his.created_dt = c_dt

                mapping = RaceMapping.sync_find_one({
                    'member_cid':
                    his.member_cid,
                    'race_cid':
                    cp_map.get(his.check_point_cid)
                })
                member = Member.sync_get_by_cid(his.member_cid)
                auth_address = mapping.auth_address if mapping else None
                if not auth_address:
                    continue

                race_cid = cp_map[his.check_point_cid]
                daily_code = __get_daily_code(his.created_dt)
                param = {
                    'race_cid': race_cid,
                    'province': auth_address.get('province'),
                    'city': auth_address.get('city'),
                    'district': auth_address.get('district'),
                    'town': auth_address.get('town'),
                    'sex': member.sex,
                    'education': member.education,
                    'category': member.category,
                    'daily_code': daily_code,
                    'company_cid': mapping.company_cid
                }
                stat = ReportRacePeopleStatisticsTemp.sync_find_one(
                    param, read_preference=ReadPreference.PRIMARY)

                if not stat:
                    stat = ReportRacePeopleStatisticsTemp(**param)
                    stat.created_dt = his.created_dt

                stat.total_num += 1
                # 初次通关
                if his.check_point_cid == last_map[
                        race_cid] and his.status == STATUS_RESULT_CHECK_POINT_WIN and RedisCache.hget(
                            cache_key, member.cid) is None:
                    stat.pass_num += 1
                    RedisCache.hset(cache_key, member.cid, 1)

                # 当日人数
                day_member_string = md5(daily_code + member.cid)
                if RedisCache.hget(cache_key, day_member_string) is None:
                    RedisCache.hset(cache_key, day_member_string, 1)
                    stat.people_num += 1

                # # 当日新增人数
                # old_his = MemberCheckPointHistory.sync_find_one({'member_cid': member.cid, 'created_dt': {
                #     '$lt': his.updated_dt.replace(hour=0, minute=0, second=0, microsecond=0)}})
                # if not old_his:
                #     stat.incre_people += 1

                stat.updated_dt = his.created_dt
                stat.sync_save()
                print('has exec %s' % index)

            except Exception:
                print(traceback.format_exc())
Esempio n. 8
0
def init_race_stat_data():
    """
    初始该活动数据
    :return:
    """

    cp_map = get_all_race_checkpoint_map()
    last_map = get_all_last_checkpoint()

    cursor = MemberCheckPointHistory.sync_find(
        {
            'check_point_cid': {
                '$in': list(cp_map.keys())
            }
        },
        read_preference=ReadPreference.PRIMARY).sort('created_dt').limit(
            600000)
    cache_key = 'race_report_script'
    RedisCache.delete(cache_key)

    index = 1
    while True:
        try:
            his = cursor.next()

            mapping = RaceMapping.sync_find_one({
                'member_cid':
                his.member_cid,
                'race_cid':
                cp_map.get(his.check_point_cid)
            })
            member = Member.sync_find_one({'cid': his.member_cid})

            auth_address = mapping.auth_address if mapping else None

            # if not auth_address:
            #     auth_address = member.auth_address
            #
            if not auth_address:
                continue

            race_cid = cp_map[his.check_point_cid]
            daily_code = __get_daily_code(his.created_dt)
            param = {
                'race_cid': race_cid,
                'province': auth_address.get('province'),
                'city': auth_address.get('city'),
                'district': auth_address.get('district'),
                'sex': member.sex,
                'education': member.education,
                'category': member.category,
                'daily_code': daily_code
            }
            stat = ReportRacePeopleStatistics.sync_find_one(
                param, read_preference=ReadPreference.PRIMARY)

            if not stat:
                stat = ReportRacePeopleStatistics(**param)
                stat.created_dt = his.created_dt

            stat.total_num += 1
            # 初次通关
            if his.check_point_cid == last_map[
                    race_cid] and his.status == STATUS_RESULT_CHECK_POINT_WIN and RedisCache.hget(
                        cache_key, member.cid) is None:
                stat.pass_num += 1
                RedisCache.hset(cache_key, member.cid, 1)

            # 当日人数
            day_member_string = md5(daily_code + member.cid)
            if RedisCache.hget(cache_key, day_member_string) is None:
                RedisCache.hset(cache_key, day_member_string, 1)
                stat.people_num += 1

            # # 当日新增人数
            # old_his = MemberCheckPointHistory.sync_find_one({'member_cid': member.cid, 'created_dt': {
            #     '$lt': his.updated_dt.replace(hour=0, minute=0, second=0, microsecond=0)}})
            # if not old_his:
            #     stat.incre_people += 1

            stat.updated_dt = his.updated_dt
            stat.sync_save()
            print('has exec %s' % index)
            index += 1
        except StopIteration:
            break
        except CursorNotFound:
            cursor = MemberCheckPointHistory.sync_find({'check_point_cid': {'$in': list(cp_map.keys())}},
                                                       read_preference=ReadPreference.PRIMARY). \
                sort('created_dt').skip(index).limit(600000 - index)

    RedisCache.delete(cache_key)