Exemplo n.º 1
0
def get_dimension(dimension_code):
    """学习效果-答题正确率趋势-{维度}

    :param dimension_code: 形如'CSK001,1'
    :return:
    """
    if dimension_code:
        dimension_code, sub_dimension_code = dimension_code.split(',')
        dimension = SubjectDimension.sync_find_one({
            'code':
            dimension_code.strip(),
            'status':
            STATUS_SUBJECT_DIMENSION_ACTIVE,
            'parent_cid':
            None
        })

        sub_dimension = SubjectDimension.sync_find_one({
            'code':
            sub_dimension_code.strip(),
            'status':
            STATUS_SUBJECT_DIMENSION_ACTIVE,
            'parent_cid':
            dimension.cid
        })

        return sub_dimension.parent_cid, sub_dimension.cid
    return None, None
Exemplo n.º 2
0
def get_choice_dimension_list(race_choice_rule: RaceSubjectChoiceRules):
    """
    获取规则维度信息
    :param choice_rule:
    :return:
    """
    result_list = []
    try:
        if race_choice_rule and race_choice_rule.dimension_rules:
            dimension_cid_list = list(race_choice_rule.dimension_rules.keys())
            if dimension_cid_list:
                match_stage = MatchStage({
                    'cid': {
                        '$in': dimension_cid_list
                    },
                    'parent_cid': None
                })
                lookup_stage = LookupStage(SubjectDimension, 'cid',
                                           'parent_cid', 'dimension_list')
                subject_dimension_list = SubjectDimension.sync_aggregate(
                    [match_stage, lookup_stage]).to_list(None)
                for subject_dimension in subject_dimension_list:
                    # 获取抽样条件
                    dimension_rule = get_sampling_condition(
                        subject_dimension,
                        race_choice_rule.dimension_rules.get(
                            subject_dimension.cid), race_choice_rule.quantity)
                    if dimension_rule:
                        result_list.append(dimension_rule)
    except Exception:
        logger.error(traceback.format_exc())
    return result_list
Exemplo n.º 3
0
def get_dimension_info(dimension_cid, answered_correct=False):
    if dimension_cid:
        dimension = SubjectDimension.sync_get_by_cid(dimension_cid)
        if dimension:
            return {
                'cid': dimension.cid,
                'code': dimension.code,
                'title': dimension.title,
                'correct': 1 if answered_correct else 0
            }
    return None
Exemplo n.º 4
0
 async def post(self, dimension_id):
     p_subject_dimension = await SubjectDimension.find_one(
         {'_id': ObjectId(dimension_id)})
     r_dict = {'code': 0}
     sub_dimension_id = None
     try:
         title = self.get_argument('title', None)
         status = self.get_argument('status', None)  # 状态
         ordered = self.get_argument('ordered', None)
         code = self.get_argument('code', None)
         comment = self.get_argument('comment', None)
         if title and ordered and p_subject_dimension and code:
             t_count = await SubjectDimension.count(
                 dict(title=title, parent_cid=p_subject_dimension.cid))
             r_count = await SubjectDimension.count(
                 dict(ordered=int(ordered),
                      parent_cid=p_subject_dimension.cid))
             c_count = await SubjectDimension.count(
                 dict(code=code, parent_cid=p_subject_dimension.cid))
             if c_count > 0:
                 r_dict['code'] = -2
             elif t_count > 0:
                 r_dict['code'] = -6
             elif r_count > 0:
                 r_dict['code'] = -5
             else:
                 if status == 'on':
                     status = STATUS_SUBJECT_DIMENSION_ACTIVE
                 else:
                     status = STATUS_SUBJECT_DIMENSION_INACTIVE
                 subject_dimension = SubjectDimension(title=title,
                                                      ordered=int(ordered),
                                                      code=code)
                 subject_dimension.status = status
                 subject_dimension.comment = comment if comment else None
                 subject_dimension.created_id = self.current_user.oid
                 subject_dimension.updated_id = self.current_user.oid
                 subject_dimension.parent_cid = p_subject_dimension.cid
                 sub_dimension_id = await subject_dimension.save()
                 await do_subject_dimension(p_subject_dimension.cid)
                 r_dict['code'] = 1
         else:
             if not title:
                 r_dict['code'] = -1
             if not ordered:
                 r_dict['code'] = -4
     except RuntimeError:
         if sub_dimension_id:
             await SubjectDimension.delete_by_ids(sub_dimension_id)
         logger.error(traceback.format_exc())
     return r_dict
Exemplo n.º 5
0
async def init_member_learning_day_dimension_statistics_indexes():
    """
    创建学习日会员维度统计索引
    :return:
    """
    dimension_cursor = SubjectDimension.find(
        dict(status=STATUS_SUBJECT_DIMENSION_ACTIVE,
             parent_cid=None)).sort([('ordered', ASC)])
    indexes = []
    while await dimension_cursor.fetch_next:
        dimension = dimension_cursor.next_object()
        if dimension:
            cid = dimension.cid
            indexes.append(IndexModel([('dimension.%s' % cid, ASC)]))
    if indexes:
        await MemberLearningDayDimensionStatistics().get_async_collection(
        ).create_indexes(indexes)
Exemplo n.º 6
0
 async def post(self):
     r_dict = {'code': 0}
     subject_dimension_id = None
     try:
         code = self.get_argument('code', None)
         title = self.get_argument('title', None)
         category = self.get_argument('category', None)
         status = self.get_argument('status', None)  # 状态
         ordered = self.get_argument('ordered', None)
         comment = self.get_argument('comment', None)
         if code and title and category and ordered:
             c_count = await SubjectDimension.count(
                 dict(code=code, parent_cid={'$in': [None, '']}))
             t_count = await SubjectDimension.count(
                 dict(title=title, parent_cid={'$in': [None, '']}))
             r_count = await SubjectDimension.count(
                 dict(ordered=int(ordered), parent_cid={'$in': [None, '']}))
             if c_count > 0:
                 r_dict['code'] = -7
             elif t_count > 0:
                 r_dict['code'] = -6
             elif r_count > 0:
                 r_dict['code'] = -5
             else:
                 if status == 'on':
                     status = STATUS_SUBJECT_DIMENSION_ACTIVE
                 else:
                     status = STATUS_SUBJECT_DIMENSION_INACTIVE
                 subject_dimension = SubjectDimension(
                     code=code,
                     title=title,
                     category=int(category),
                     ordered=int(ordered))
                 subject_dimension.status = status
                 subject_dimension.comment = comment if comment else None
                 subject_dimension.created_id = self.current_user.oid
                 subject_dimension.updated_id = self.current_user.oid
                 subject_dimension_id = await subject_dimension.save()
                 r_dict['code'] = 1
         else:
             if not title:
                 r_dict['code'] = -1
             if not category:
                 r_dict['code'] = -2
             if not ordered:
                 r_dict['code'] = -4
     except RuntimeError:
         if subject_dimension_id:
             await SubjectDimension.delete_by_ids(subject_dimension_id)
         logger.error(traceback.format_exc())
     return r_dict
Exemplo n.º 7
0
 async def assign_color(conditions: dict, module):
     subject_category_cid = '18114DC7C31B17AE35841CAE833161A2'
     all_next_condition = []
     if conditions:
         used_color = {}
         for index, item in enumerate(conditions.values()):
             if item is not None:
                 name = item['name']
                 if name and index < len(CHART_COLOR_LIST):
                     item['color'] = CHART_COLOR_LIST[index]
                     used_color[md5(name)] = CHART_COLOR_LIST[index]
         if module == 803:
             for i in range(8):
                 next_condition = copy.deepcopy(conditions)
                 for key, val in next_condition.items():
                     if 'condition' in val.keys(
                     ) and 'dimension' in val['condition']:
                         dimension_str = val['condition']['dimension']
                         dimension = json.loads(dimension_str)
                         if dimension and subject_category_cid in dimension.keys(
                         ):
                             dimension_cursor = SubjectDimension.find(
                                 dict(parent_cid=subject_category_cid))
                             while await dimension_cursor.fetch_next:
                                 dimension_o = dimension_cursor.next_object(
                                 )
                                 if dimension_o:
                                     c_name: str = val['name']
                                     c_name = c_name.replace(
                                         '%s,' % dimension_o.title,
                                         '').replace(dimension_o.title, '')
                                     val['name'] = c_name
                             del dimension[subject_category_cid]
                             val['condition']['dimension'] = json.dumps(
                                 dimension, ensure_ascii=False)
                 all_next_condition.append(next_condition)
     return conditions, all_next_condition
Exemplo n.º 8
0
def do_statistics_subject_radar(cache_key, root_dimension_code,
                                m_city_code_list, province_code_list,
                                city_code_list, gender_list, age_group_list,
                                education_list):
    """

    :param cache_key:
    :param root_dimension_code:
    :param m_city_code_list:
    :param province_code_list:
    :param city_code_list:
    :param gender_list:
    :param age_group_list:
    :param education_list:
    :return:
    """
    RedisCache.set(cache_key, KEY_CACHE_REPORT_DOING_NOW, 5 * 60)
    data = []
    dimension = SubjectDimension.sync_find_one(
        dict(code=root_dimension_code, status=STATUS_SUBJECT_DIMENSION_ACTIVE))
    if not dimension:
        raise ValueError(
            'can not find dimension by `root_dimension_code`(%s)' %
            root_dimension_code)

    stage_list = []
    #  取前一天凌晨12点之前的数据
    time_match = get_yesterday()
    stage_list.append(MatchStage({'updated_dt': {'$lt': time_match}}))
    if m_city_code_list:
        stage_list.append(MatchStage({'city_code': {'$in': m_city_code_list}}))

    query_dict = {}
    if province_code_list:
        query_dict['province_code'] = {'$in': province_code_list}
    if city_code_list:
        query_dict['city_code'] = {'$in': city_code_list}
    if gender_list:
        query_dict['gender'] = {
            '$in': [int(s_gender) for s_gender in gender_list]
        }
    if age_group_list:
        query_dict['age_group'] = {
            '$in': [int(s_age_group) for s_age_group in age_group_list]
        }
    if education_list:
        query_dict['education'] = {
            '$in': [int(s_education) for s_education in education_list]
        }

    if query_dict:
        stage_list.append(MatchStage(query_dict))

    stage_list.append(
        GroupStage('dimension.%s' % dimension.cid,
                   total={'$sum': '$total'},
                   correct={'$sum': '$correct'}))
    stage_list.append(
        LookupStage(SubjectDimension, '_id', 'cid', 'dimension_list'))
    stat_result = MemberSubjectStatistics.sync_aggregate(stage_list)
    while True:
        try:
            mds = stat_result.next()
            if mds:
                code, title, ordered = '', '', 0
                if hasattr(mds, 'dimension_list') and mds.dimension_list:
                    dimension = mds.dimension_list[0]
                    if dimension:
                        code = dimension.code
                        title = dimension.title
                        ordered = dimension.ordered
                data.append(
                    dict(code=code,
                         title=title,
                         ordered=ordered,
                         correct=mds.correct,
                         total=mds.total))
        except StopIteration:
            break
    if not data:
        early_warning_empty("start_statistics_subject_parameter_radar",
                            cache_key, locals(), '获取维度正确率雷达图统计数据为空,请检查!')
    RedisCache.set(cache_key, msgpack.packb(data))
Exemplo n.º 9
0
def do_statistics_subject_cross(cache_key, main_dimension_code, second_dimension_code, m_city_code_list,
                                province_code_list, city_code_list, gender_list, age_group_list,
                                education_list):
    """

    :param cache_key:
    :param main_dimension_code:
    :param second_dimension_code:
    :param m_city_code_list:
    :param province_code_list:
    :param city_code_list:
    :param gender_list:
    :param age_group_list:
    :param education_list:
    :return:
    """
    RedisCache.set(cache_key, KEY_CACHE_REPORT_DOING_NOW, 5 * 60)
    main_dimension = SubjectDimension.sync_find_one(
        dict(code=main_dimension_code, status=STATUS_SUBJECT_DIMENSION_ACTIVE))
    main_sub_dimension_list = SubjectDimension.sync_find(dict(parent_cid=main_dimension.cid)).sort(
        [('ordered', ASC)]).to_list(None)

    second_dimension = SubjectDimension.sync_find_one(
        dict(code=second_dimension_code, status=STATUS_SUBJECT_DIMENSION_ACTIVE))
    second_sub_dimension_list = SubjectDimension.sync_find(dict(parent_cid=second_dimension.cid)).sort(
        [('ordered', ASC)]).to_list(None)

    data = []
    for index, m_dimen in enumerate(main_sub_dimension_list):
        sub_data_list = []
        for s_dimen in second_sub_dimension_list:
            stage_list = []
            #  取前一天凌晨12点之前的数据
            time_match = get_yesterday()
            stage_list.append(MatchStage({'updated_dt': {'$lt': time_match}}))
            match_dict = {'dimension.%s' % main_dimension.cid: m_dimen.cid,
                          'dimension.%s' % second_dimension.cid: s_dimen.cid}
            if m_city_code_list:
                match_dict['city_code'] = {'$in': m_city_code_list}
            stage_list.append(MatchStage(match_dict))

            query_dict = {}
            if province_code_list:
                query_dict['province_code'] = {'$in': province_code_list}
            if city_code_list:
                query_dict['city_code'] = {'$in': city_code_list}
            if gender_list:
                query_dict['gender'] = {'$in': [int(s_gender) for s_gender in gender_list]}
            if age_group_list:
                query_dict['age_group'] = {'$in': [int(s_age_group) for s_age_group in age_group_list]}
            if education_list:
                query_dict['education'] = {'$in': [int(s_education) for s_education in education_list]}

            if query_dict:
                stage_list.append(MatchStage(query_dict))
            # 分组
            group_params = {
                'total': {'$sum': '$total'},
                'correct': {'$sum': '$correct'}
            }
            stage_list.append(GroupStage(None, **group_params))

            stat_result = MemberSubjectStatistics.sync_aggregate(
                stage_list).to_list(None)
            tmp_data = {
                'code': s_dimen.code,
                'title': s_dimen.title,
                'ordered': s_dimen.ordered,
                'correct': stat_result[0].correct if stat_result else 0,
                'total': stat_result[0].total if stat_result else 0
            }
            sub_data_list.append(tmp_data)
        main_data = {
            'code': str(index + 1),
            'title': m_dimen.title,
            'ordered': index + 1,
            'sub': sub_data_list
        }
        data.append(main_data)

    if data:
        data.sort(key=lambda x: x.get('ordered', 0))
    if not data:
        early_warning_empty("start_statistics_subject_parameter_cross", cache_key, locals(), '获取维度正确率统计数据为空,请检查!')
    RedisCache.set(cache_key, msgpack.packb(data))
def do_count_member_subject_accuracy(accuracy_stat: MemberAccuracyStatistics, history_model, member: Member):
    """
    统计会员答题正确率
    :param accuracy_stat: 统计结果Model
    :param history_model: 游戏历史Model
    :param member: 会员Model
    :return:
    """
    if accuracy_stat and history_model and member:
        answer_results = history_model.result
        if answer_results:
            for answer in answer_results:
                if answer:
                    selected_option_cid = answer.get('selected_option_cid')
                    if selected_option_cid:
                        subject_cid = answer.get('subject_cid')
                        correct = answer.get('true_answer')
                        if subject_cid:
                            accuracy_stat.total_quantity += 1
                        if correct:
                            accuracy_stat.correct_quantity += 1
                        subject = Subject.sync_get_by_cid(subject_cid)
                        if subject:
                            dimension_dict = subject.dimension_dict
                            if dimension_dict:
                                for p_dimension_cid, dimension_cid in dimension_dict.items():
                                    dimension = accuracy_stat.dimension
                                    if dimension is None:
                                        accuracy_stat.dimension = {}
                                    # 冗余根维度信息
                                    if dimension.get(p_dimension_cid) is None:
                                        dimension[p_dimension_cid] = {}
                                        p_dimension: SubjectDimension = SubjectDimension.sync_get_by_cid(
                                            p_dimension_cid)
                                        if p_dimension:
                                            if p_dimension.code:
                                                dimension[p_dimension_cid]['code'] = p_dimension.code
                                            if p_dimension.title:
                                                dimension[p_dimension_cid]['title'] = p_dimension.title
                                            if p_dimension.ordered:
                                                dimension[p_dimension_cid]['ordered'] = p_dimension.ordered
                                            dimension[p_dimension_cid]['children'] = {}
                                    # 根维度统计
                                    if dimension[p_dimension_cid].get('total') is None:
                                        dimension[p_dimension_cid]['total'] = 0
                                    if dimension[p_dimension_cid].get('correct') is None:
                                        dimension[p_dimension_cid]['correct'] = 0
                                    dimension[p_dimension_cid]['total'] += 1  # 总量
                                    if correct:
                                        dimension[p_dimension_cid]['correct'] += 1  # 正确量

                                    children = dimension[p_dimension_cid]['children']
                                    # 冗余子维度信息
                                    if children.get(dimension_cid) is None:
                                        children[dimension_cid] = {}
                                        c_dimension: SubjectDimension = SubjectDimension.sync_get_by_cid(dimension_cid)
                                        if c_dimension:
                                            if c_dimension.code:
                                                children[dimension_cid]['code'] = c_dimension.code
                                            if c_dimension.title:
                                                children[dimension_cid]['title'] = c_dimension.title
                                            if c_dimension.ordered:
                                                children[dimension_cid]['ordered'] = c_dimension.ordered
                                    # 子维度统计
                                    if children[dimension_cid].get('total') is None:
                                        children[dimension_cid]['total'] = 0
                                    if children[dimension_cid].get('correct') is None:
                                        children[dimension_cid]['correct'] = 0
                                    children[dimension_cid]['total'] += 1  # 总量
                                    if correct:
                                        children[dimension_cid]['correct'] += 1  # 正确量
def do_count_dimension_accuracy(answer: dict, accuracy_stat: SubjectAccuracyStatistics):
    """
    计算维度正确率
    :param answer: 答案
    :param accuracy_stat: 统计Model
    :return:
    """
    if answer and accuracy_stat:
        selected_option_cid = answer.get('selected_option_cid')
        if selected_option_cid:
            correct = answer.get('true_answer')
            subject_cid = answer.get('subject_cid')
            # 统计维度
            if subject_cid:
                subject = Subject.sync_get_by_cid(subject_cid)
                if subject:
                    dimension_dict = subject.dimension_dict
                    if dimension_dict:
                        for p_dimension_cid, dimension_cid in dimension_dict.items():
                            dimension = accuracy_stat.dimension
                            if dimension is None:
                                accuracy_stat.dimension = {}
                            # 冗余根维度信息
                            if dimension.get(p_dimension_cid) is None:
                                dimension[p_dimension_cid] = {}
                                p_dimension: SubjectDimension = SubjectDimension.sync_get_by_cid(p_dimension_cid)
                                if p_dimension:
                                    if p_dimension.code:
                                        dimension[p_dimension_cid]['code'] = p_dimension.code
                                    if p_dimension.title:
                                        dimension[p_dimension_cid]['title'] = p_dimension.title
                                    if p_dimension.ordered:
                                        dimension[p_dimension_cid]['ordered'] = p_dimension.ordered
                                    dimension[p_dimension_cid]['children'] = {}
                            # 根维度统计
                            if dimension[p_dimension_cid].get('total') is None:
                                dimension[p_dimension_cid]['total'] = 0
                            if dimension[p_dimension_cid].get('correct') is None:
                                dimension[p_dimension_cid]['correct'] = 0
                            dimension[p_dimension_cid]['total'] += 1  # 总量
                            if correct:
                                dimension[p_dimension_cid]['correct'] += 1  # 正确量

                            children = dimension[p_dimension_cid]['children']
                            # 冗余子维度信息
                            if children.get(dimension_cid) is None:
                                children[dimension_cid] = {}
                                c_dimension: SubjectDimension = SubjectDimension.sync_get_by_cid(dimension_cid)
                                if c_dimension:
                                    if c_dimension.code:
                                        children[dimension_cid]['code'] = c_dimension.code
                                    if c_dimension.title:
                                        children[dimension_cid]['title'] = c_dimension.title
                                    if c_dimension.ordered:
                                        children[dimension_cid]['ordered'] = c_dimension.ordered
                            # 子维度统计
                            if children[dimension_cid].get('total') is None:
                                children[dimension_cid]['total'] = 0
                            if children[dimension_cid].get('correct') is None:
                                children[dimension_cid]['correct'] = 0
                            children[dimension_cid]['total'] += 1  # 总量
                            if correct:
                                children[dimension_cid]['correct'] += 1  # 正确量