Example #1
0
def test_check_events_request_len_ng_errlen():
    """
    イベント情報チェック
    ※異常系(データなし)
    """

    req = {
        'eventinfo' : ['pytest1', 'pytest2']
    }

    result = EventsRequestCommon.check_events_request_len(req, 1)

    assert result == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH
Example #2
0
def test_check_events_request_len_ng_notlist():
    """
    イベント情報チェック
    ※異常系(データなし)
    """

    req = {
        'eventinfo' : 'pytest1'
    }

    result = EventsRequestCommon.check_events_request_len(req, 1)

    assert result == EventsRequestCommon.REQUEST_ERR_EVINFO_TYPE
Example #3
0
def test_check_events_request_len_ng_lenzero():
    """
    イベント情報チェック
    ※異常系(データなし)
    """

    req = {
        'eventinfo' : []
    }

    result = EventsRequestCommon.check_events_request_len(req, 0)

    assert result == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH
Example #4
0
def test_check_events_request_len_ok():
    """
    イベント情報チェック
    ※正常系
    """

    req = {
        'eventinfo' : ['pytest1', 'pytest2']
    }

    result = EventsRequestCommon.check_events_request_len(req, 2)

    assert result == EventsRequestCommon.REQUEST_OK
Example #5
0
def check_evinfo_error(trace_id, json_str, ruletypeid, evinfo_length):
    """
    [メソッド概要]
      イベントリクエストのイベント情報の正常性チェック
      ※try の中で呼び出すこと
    """

    # イベント情報のチェック
    err_code = EventsRequestCommon.check_events_request_len(
        json_str, evinfo_length)
    if err_code != EventsRequestCommon.REQUEST_OK:
        if err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_TYPE:
            logger.user_log('LOSM22002', trace_id, ruletypeid, 0,
                            evinfo_length)
            raise Exception()

        elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH:
            logger.user_log('LOSM22002', trace_id, ruletypeid,
                            len(json_str[EventsRequestCommon.KEY_EVENTINFO]),
                            evinfo_length)
            raise Exception()

        raise Exception()
Example #6
0
def eventsrequest(request):
    """
    [メソッド概要]
      テストリクエスト実行時のリクエストを処理する
    """

    now = datetime.datetime.now(pytz.timezone('UTC'))
    trace_id = EventsRequestCommon.generate_trace_id()
    resp_json = {}
    result = False
    msg = ''

    logger.system_log('LOSI13001', trace_id)

    try:
        #########################################
        # リクエストのチェック
        #########################################
        # メソッドのチェック
        if not request or request.method == 'GET':
            msg = 'Invalid request. Must be POST. Not GET.'
            logger.user_log('LOSM13001', trace_id)
            raise Exception(msg)

        # フォーマットのチェック
        try:
            json_str = json.loads(request.body.decode('UTF-8'))
        except json.JSONDecodeError:
            msg = 'Invalid request format. Must be JSON.'
            logger.user_log('LOSM13005', trace_id)
            raise Exception(msg)

        if json_str[EventsRequestCommon.KEY_REQTYPE] in [1,
                                                         '1']:  # 1:プロダクション環境

            # プロダクション環境
            json_str['traceid'] = trace_id
            json_str = json.dumps(json_str)

            # 初回の設定読込で落ちるのめんどくさいのでここに置いとく
            _rabbitMQ_conf()

            # RabbitMQへ送信
            mq_lock.acquire()
            _produce(json_str)
            mq_lock.release()

            result = True
            msg = 'Accept request.'

        elif json_str[EventsRequestCommon.KEY_REQTYPE] in [2,
                                                           '2']:  # 2:ステージング環境

            # ステージング環境
            user = User.objects.get(user_id=1)

            # キーのチェック
            err_code = EventsRequestCommon.check_events_request_key(json_str)
            if err_code != EventsRequestCommon.REQUEST_OK:
                err_keyname = ''
                if err_code == EventsRequestCommon.REQUEST_ERR_RULETYPE_KEY:
                    err_keyname = EventsRequestCommon.KEY_RULETYPE

                elif err_code == EventsRequestCommon.REQUEST_ERR_REQTYPE_KEY:
                    err_keyname = EventsRequestCommon.KEY_REQTYPE

                elif err_code == EventsRequestCommon.REQUEST_ERR_DATETIME_KEY:
                    err_keyname = EventsRequestCommon.KEY_EVENTTIME

                elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_KEY:
                    err_keyname = EventsRequestCommon.KEY_EVENTINFO

                msg = 'Invalid request.'
                logger.user_log('LOSM13002', err_keyname, trace_id)
                raise Exception(msg)

            # ルール情報の取得
            reqtypeid = json_str[EventsRequestCommon.KEY_REQTYPE]
            ruletablename = json_str[EventsRequestCommon.KEY_RULETYPE]
            ruletypeid = RuleType.objects.get(
                rule_type_name=ruletablename).rule_type_id
            evinfo_length = DataObject.objects.filter(
                rule_type_id=ruletypeid).values('label').distinct().count()

            # イベント情報のチェック
            err_code = EventsRequestCommon.check_events_request_len(
                json_str, evinfo_length)
            if err_code != EventsRequestCommon.REQUEST_OK:
                if err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_TYPE:
                    msg = 'Unmatch, Number of event information elements.'
                    logger.user_log('LOSM13003', trace_id, ruletypeid, 0,
                                    evinfo_length)
                    raise Exception(msg)

                elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH:
                    msg = 'Unmatch, Number of event information elements.'
                    logger.user_log(
                        'LOSM13003', trace_id, ruletypeid,
                        len(json_str[EventsRequestCommon.KEY_EVENTINFO]),
                        evinfo_length)
                    raise Exception(msg)

                raise Exception()

            #########################################
            # リクエストをDBに保存
            #########################################
            # DB登録用に成形
            evinfo_str = ''
            rset = DataObject.objects.filter(
                rule_type_id=ruletypeid).order_by('data_object_id')

            label_list = []
            conditional_expression_id_list = []

            for a in rset:
                if a.label not in label_list:
                    label_list.append(a.label)
                    conditional_expression_id_list.append(
                        a.conditional_expression_id)

            for rs, v in zip(conditional_expression_id_list,
                             json_str[EventsRequestCommon.KEY_EVENTINFO]):
                if evinfo_str:
                    evinfo_str += ','

                # 条件式がリストの場合
                if rs in (13, 14):
                    if not isinstance(v, list):
                        evinfo_str += '%s' % (v)

                    else:
                        temp_val = '['
                        for i, val in enumerate(v):
                            if i > 0:
                                temp_val += ','

                            temp_val += '"%s"' % (val)

                        temp_val += ']'
                        evinfo_str += '%s' % (temp_val)

                # 条件式がリスト以外の場合
                else:
                    evinfo_str += '"%s"' % (v)

            evinfo_str = '{"EVENT_INFO":[%s]}' % (evinfo_str)
            event_dt = json_str[EventsRequestCommon.KEY_EVENTTIME]
            event_dt = TimeConversion.get_time_conversion_utc(
                event_dt, 'Asia/Tokyo')

            json_data = {
                'trace_id': trace_id,
                'request_type_id': reqtypeid,
                'rule_type_id': ruletypeid,
                'request_reception_time': now,
                'request_user': '******',
                'request_server': 'OASE Web',
                'event_to_time': event_dt,
                'event_info': evinfo_str,
                'status': defs.UNPROCESS,
                'status_update_id': '',
                'retry_cnt': 0,
                'last_update_timestamp': now,
                'last_update_user': user.user_name,
            }

            # バリデーションチェック
            oters = EventsRequestSerializer(data=json_data)
            result_valid = oters.is_valid()

            # バリデーションエラー
            if result_valid == False:
                msg = '%s' % oters.errors
                logger.user_log('LOSM13004', trace_id, msg)

            # 正常の場合はDB保存
            else:
                oters.save()
                result = True
                msg = 'Accept request.'

        else:  # 不明なリクエスト種別

            msg = 'Invalid request type.'
            logger.user_log('LOSM13023', trace_id,
                            json_str[EventsRequestCommon.KEY_REQTYPE])

    except Exception as e:
        if not msg:
            msg = 'Unexpected error.'

        logger.system_log('LOSM13013', trace_id, traceback.format_exc())

    # レスポンス情報の作成
    resp_json = {
        'result': result,
        'msg': msg,
        'trace_id': trace_id,
    }

    resp_json = json.dumps(resp_json, ensure_ascii=False)

    logger.system_log('LOSI13002', trace_id, result, msg)

    # 応答
    return HttpResponse(resp_json)
Example #7
0
def bulk_eventsrequest(request):
    """
    [メソッド概要]
      一括用のリクエストを処理する
    """

    resp_json = {}
    result = False
    rule_type_id_list = {}
    label_count_list = {}

    logger.system_log('LOSI13023')

    try:
        #########################################
        # リクエストのチェック
        #########################################
        # メソッドのチェック
        if not request or request.method == 'GET':
            logger.system_log('LOSM13025')
            raise Exception()

        # フォーマットのチェック
        try:
            json_str = json.loads(request.body.decode('UTF-8'))
        except json.JSONDecodeError:
            logger.system_log('LOSM13026')
            raise Exception()

        for data in json_str['request']:

            # キーのチェック
            err_code = EventsRequestCommon.check_events_request_key(data)
            if err_code != EventsRequestCommon.REQUEST_OK:
                err_keyname = ''
                if err_code == EventsRequestCommon.REQUEST_ERR_RULETYPE_KEY:
                    err_keyname = EventsRequestCommon.KEY_RULETYPE

                elif err_code == EventsRequestCommon.REQUEST_ERR_REQTYPE_KEY:
                    err_keyname = EventsRequestCommon.KEY_REQTYPE

                elif err_code == EventsRequestCommon.REQUEST_ERR_DATETIME_KEY:
                    err_keyname = EventsRequestCommon.KEY_EVENTTIME

                elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_KEY:
                    err_keyname = EventsRequestCommon.KEY_EVENTINFO

                logger.system_log('LOSM13027', err_keyname)
                raise Exception()

            # ルール情報の取得
            reqtypeid = data[EventsRequestCommon.KEY_REQTYPE]
            ruletablename = data[EventsRequestCommon.KEY_RULETYPE]

            if ruletablename not in rule_type_id_list:
                rule_type_id_list.update({ruletablename: 0})
                label_count_list.update({ruletablename: 0})

                rset = RuleType.objects.filter(
                    rule_type_name=ruletablename).values(
                        'rule_type_id', 'rule_type_name', 'label_count')
                for rs in rset:
                    rule_type_id_list.update(
                        {rs['rule_type_name']: rs['rule_type_id']})
                    label_count_list.update(
                        {rs['rule_type_name']: rs['label_count']})

            if ruletablename in rule_type_id_list:
                ruletypeid = rule_type_id_list[ruletablename]
                evinfo_length = label_count_list[ruletablename]

            # イベント情報のチェック
            err_code = EventsRequestCommon.check_events_request_len(
                data, evinfo_length)
            if err_code != EventsRequestCommon.REQUEST_OK:
                if err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_TYPE:
                    logger.system_log('LOSM13028', ruletypeid, 0,
                                      evinfo_length)
                    raise Exception()

                elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH:
                    logger.system_log(
                        'LOSM13028', ruletypeid,
                        len(data[EventsRequestCommon.KEY_EVENTINFO]),
                        evinfo_length)
                    raise Exception()

                raise Exception()

            trace_id = EventsRequestCommon.generate_trace_id()
            data['traceid'] = trace_id
            data = json.dumps(data)

            _rabbitMQ_conf()

            # RabbitMQへ送信
            mq_lock.acquire()
            _produce(data)
            mq_lock.release()

        result = True

    except Exception as e:
        logger.system_log('LOSM13029', traceback.format_exc())

    # レスポンス情報の作成
    resp_json = {
        'result': result,
    }

    resp_json = json.dumps(resp_json, ensure_ascii=False)

    logger.system_log('LOSI13024', result)

    # 応答
    return HttpResponse(resp_json)