def test_random_run(self):
     print("Boost model folder: ", self.boost_model_folder)
     decision_maker = BoostingDecisionMaker(self.boost_model_folder)
     test_data_size = 5
     random_data = np.random.rand(test_data_size,
                                  len(decision_maker.get_feature_ids()))
     result, result_probability = decision_maker.predict(random_data)
     result.should.have.length_of(test_data_size)
     result_probability.should.have.length_of(test_data_size)
 def test_full_data_check(self):
     print("Boost model folder: ", self.boost_model_folder)
     decision_maker = BoostingDecisionMaker(self.boost_model_folder)
     boost_model_results = self.get_fixture(self.boost_model_results)
     tests = [
         {
             "elastic_results":
             [(self.get_fixture(self.log_message),
               self.get_fixture(self.one_hit_search_rs_explained))],
             "config":
             TestBoostingModel.get_default_config(),
         },
         {
             "elastic_results":
             [(self.get_fixture(self.log_message),
               self.get_fixture(self.two_hits_search_rs_explained))],
             "config":
             TestBoostingModel.get_default_config(),
         },
         {
             "elastic_results":
             [(self.get_fixture(self.log_message),
               self.get_fixture(self.two_hits_search_rs_explained)),
              (self.get_fixture(self.log_message),
               self.get_fixture(self.one_hit_search_rs_explained))],
             "config":
             TestBoostingModel.get_default_config(),
         },
         {
             "elastic_results":
             [(self.get_fixture(self.log_message_only_small_logs),
               self.get_fixture(self.two_hits_search_rs_small_logs))],
             "config":
             TestBoostingModel.get_default_config(),
         },
     ]
     for idx, test in enumerate(tests):
         _boosting_featurizer = BoostingFeaturizer(
             test["elastic_results"], test["config"],
             decision_maker.get_feature_ids())
         with sure.ensure('Error in the test case number: {0}', idx):
             gathered_data, issue_type_names = _boosting_featurizer.gather_features_info(
             )
             gathered_data.should.equal(boost_model_results[str(idx)][0],
                                        epsilon=self.epsilon)
             predict_label, predict_probability = decision_maker.predict(
                 gathered_data)
             predict_label.tolist().should.equal(
                 boost_model_results[str(idx)][1], epsilon=self.epsilon)
             predict_probability.tolist().should.equal(
                 boost_model_results[str(idx)][2], epsilon=self.epsilon)
    def test_full_data_check(self):
        print("Boost model folder : ", self.boost_model_folder)
        print("Weights model folder : ", self.weights_folder)
        print("Global defect type model folder : ",
              self.global_defect_type_model_folder)
        decision_maker = BoostingDecisionMaker(folder=self.boost_model_folder)
        boost_model_results = utils.get_fixture(self.boost_model_results,
                                                to_json=True)
        tests = []
        for log_lines, filter_fields, _decision_maker in [
            (-1, ["detected_message", "stacktrace"], decision_maker),
            (2, ["message"], decision_maker)
        ]:
            tests.extend([
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message, to_json=True),
                      utils.get_fixture(self.one_hit_search_rs_explained,
                                        to_json=True))],
                    "config":
                    self.get_default_config(number_of_log_lines=log_lines,
                                            filter_fields=filter_fields),
                    "decision_maker":
                    _decision_maker
                },
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message, to_json=True),
                      utils.get_fixture(self.two_hits_search_rs_explained,
                                        to_json=True))],
                    "config":
                    self.get_default_config(number_of_log_lines=log_lines,
                                            filter_fields=filter_fields),
                    "decision_maker":
                    _decision_maker
                },
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message, to_json=True),
                      utils.get_fixture(self.two_hits_search_rs_explained,
                                        to_json=True)),
                     (utils.get_fixture(self.log_message, to_json=True),
                      utils.get_fixture(self.one_hit_search_rs_explained,
                                        to_json=True))],
                    "config":
                    self.get_default_config(number_of_log_lines=log_lines,
                                            filter_fields=filter_fields),
                    "decision_maker":
                    _decision_maker
                },
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message_only_small_logs,
                                        to_json=True),
                      utils.get_fixture(self.two_hits_search_rs_small_logs,
                                        to_json=True))],
                    "config":
                    self.get_default_config(number_of_log_lines=log_lines,
                                            filter_fields=filter_fields),
                    "decision_maker":
                    _decision_maker
                },
            ])

        for idx, test in enumerate(tests):
            feature_ids = test["decision_maker"].get_feature_ids()
            weight_log_sim = None
            if self.weights_folder.strip():
                weight_log_sim = weighted_similarity_calculator.\
                    WeightedSimilarityCalculator(folder=self.weights_folder)
            _boosting_featurizer = BoostingFeaturizer(
                test["elastic_results"],
                test["config"],
                feature_ids,
                weighted_log_similarity_calculator=weight_log_sim)
            if self.global_defect_type_model_folder.strip():
                _boosting_featurizer.set_defect_type_model(
                    defect_type_model.DefectTypeModel(
                        folder=self.global_defect_type_model_folder))
            with sure.ensure('Error in the test case number: {0}', idx):
                gathered_data, issue_type_names = _boosting_featurizer.gather_features_info(
                )
                gathered_data.should.equal(boost_model_results[str(idx)][0],
                                           epsilon=self.epsilon)
                predict_label, predict_probability = test[
                    "decision_maker"].predict(gathered_data)
                predict_label.tolist().should.equal(
                    boost_model_results[str(idx)][1], epsilon=self.epsilon)
                predict_probability.tolist().should.equal(
                    boost_model_results[str(idx)][2], epsilon=self.epsilon)
    def test_suggest_items(self):
        """Test suggesting test items"""
        tests = [
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(testItemId=1,
                                            uniqueId="341",
                                            testCaseHash=123,
                                            launchId=1,
                                            launchName="Launch",
                                            project=1,
                                            logs=[]),
                "expected_result": [],
                "boost_predict": ([], [])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/2",
                        "status": HTTPStatus.NOT_FOUND,
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(testItemId=1,
                                            uniqueId="341",
                                            testCaseHash=123,
                                            launchId=1,
                                            launchName="Launch",
                                            project=2,
                                            logs=[
                                                launch_objects.Log(
                                                    logId=1,
                                                    message="error found",
                                                    logLevel=40000)
                                            ]),
                "expected_result": [],
                "boost_predict": ([], [])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(testItemId=1,
                                            uniqueId="341",
                                            testCaseHash=123,
                                            launchId=1,
                                            launchName="Launch",
                                            project=1,
                                            logs=[
                                                launch_objects.Log(
                                                    logId=1,
                                                    message=" ",
                                                    logLevel=40000)
                                            ]),
                "expected_result": [],
                "boost_predict": ([], [])
            },
            {
                "test_calls": [{
                    "method": httpretty.GET,
                    "uri": "/1",
                    "status": HTTPStatus.OK,
                }, {
                    "method": httpretty.GET,
                    "uri": "/1/_search",
                    "status": HTTPStatus.OK,
                    "content_type": "application/json",
                    "rq": utils.get_fixture(self.search_rq_first),
                    "rs": utils.get_fixture(self.no_hits_search_rs),
                }, {
                    "method": httpretty.GET,
                    "uri": "/1/_search",
                    "status": HTTPStatus.OK,
                    "content_type": "application/json",
                    "rq": utils.get_fixture(self.search_rq_second),
                    "rs": utils.get_fixture(self.no_hits_search_rs),
                }, {
                    "method": httpretty.GET,
                    "uri": "/1/_search",
                    "status": HTTPStatus.OK,
                    "content_type": "application/json",
                    "rq": utils.get_fixture(self.search_rq_third),
                    "rs": utils.get_fixture(self.no_hits_search_rs),
                }],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [],
                "boost_predict": ([], [])
            },
            {
                "test_calls": [{
                    "method": httpretty.GET,
                    "uri": "/1",
                    "status": HTTPStatus.OK,
                }, {
                    "method": httpretty.GET,
                    "uri": "/1/_search",
                    "status": HTTPStatus.OK,
                    "content_type": "application/json",
                    "rq": utils.get_fixture(self.search_rq_first),
                    "rs": utils.get_fixture(self.no_hits_search_rs),
                }, {
                    "method": httpretty.GET,
                    "uri": "/1/_search",
                    "status": HTTPStatus.OK,
                    "content_type": "application/json",
                    "rq": utils.get_fixture(self.search_rq_second),
                    "rs": utils.get_fixture(self.no_hits_search_rs),
                }, {
                    "method": httpretty.GET,
                    "uri": "/1/_search",
                    "status": HTTPStatus.OK,
                    "content_type": "application/json",
                    "rq": utils.get_fixture(self.search_rq_third),
                    "rs": utils.get_fixture(self.no_hits_search_rs),
                }],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [],
                "boost_predict": ([], [])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_first),
                        "rs": utils.get_fixture(self.no_hits_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_second),
                        "rs": utils.get_fixture(self.one_hit_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_third),
                        "rs": utils.get_fixture(self.one_hit_search_rs),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='AB001',
                        relevantItem=1,
                        relevantLogId=1,
                        matchScore=80.0,
                        esScore=10.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=0,
                        usedLogLines=-1,
                        minShouldMatch=80)
                ],
                "boost_predict": ([1], [[0.2, 0.8]])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_first),
                        "rs": utils.get_fixture(self.one_hit_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_second),
                        "rs": utils.get_fixture(self.one_hit_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_third),
                        "rs": utils.get_fixture(self.one_hit_search_rs),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='AB001',
                        relevantItem=1,
                        relevantLogId=1,
                        matchScore=70.0,
                        esScore=10.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=0,
                        usedLogLines=-1,
                        minShouldMatch=80)
                ],
                "boost_predict": ([1], [[0.3, 0.7]])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_first),
                        "rs": utils.get_fixture(self.one_hit_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_second),
                        "rs": utils.get_fixture(self.two_hits_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_third),
                        "rs": utils.get_fixture(self.two_hits_search_rs),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='AB001',
                        relevantItem=1,
                        relevantLogId=1,
                        matchScore=70.0,
                        esScore=15.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=0,
                        usedLogLines=-1,
                        minShouldMatch=80)
                ],
                "boost_predict": ([1, 0], [[0.3, 0.7], [0.9, 0.1]])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_first),
                        "rs": utils.get_fixture(self.one_hit_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_second),
                        "rs": utils.get_fixture(self.two_hits_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_third),
                        "rs": utils.get_fixture(self.no_hits_search_rs),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='AB001',
                        relevantItem=1,
                        relevantLogId=1,
                        matchScore=70.0,
                        esScore=15.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=0,
                        usedLogLines=-1,
                        minShouldMatch=80),
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='PB001',
                        relevantItem=2,
                        relevantLogId=2,
                        matchScore=45.0,
                        esScore=10.0,
                        esPosition=1,
                        modelFeatureNames='0',
                        modelFeatureValues='0.67',
                        modelInfo='',
                        resultPosition=1,
                        usedLogLines=-1,
                        minShouldMatch=80)
                ],
                "boost_predict": ([1, 0], [[0.3, 0.7], [0.55, 0.45]])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_first),
                        "rs": utils.get_fixture(self.two_hits_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_second),
                        "rs": utils.get_fixture(self.three_hits_search_rs),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_third),
                        "rs": utils.get_fixture(self.no_hits_search_rs),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='PB001',
                        relevantItem=3,
                        relevantLogId=3,
                        matchScore=80.0,
                        esScore=10.0,
                        esPosition=2,
                        modelFeatureNames='0',
                        modelFeatureValues='0.67',
                        modelInfo='',
                        resultPosition=0,
                        usedLogLines=-1,
                        minShouldMatch=80),
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='AB001',
                        relevantItem=1,
                        relevantLogId=1,
                        matchScore=70.0,
                        esScore=15.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=1,
                        usedLogLines=-1,
                        minShouldMatch=80),
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='PB001',
                        relevantItem=2,
                        relevantLogId=2,
                        matchScore=45.0,
                        esScore=10.0,
                        esPosition=1,
                        modelFeatureNames='0',
                        modelFeatureValues='0.67',
                        modelInfo='',
                        resultPosition=2,
                        usedLogLines=-1,
                        minShouldMatch=80)
                ],
                "boost_predict": ([1, 0, 1], [[0.3, 0.7], [0.55, 0.45],
                                              [0.2, 0.8]])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_first),
                        "rs": utils.get_fixture(self.two_hits_search_rs),
                    },
                    {
                        "method":
                        httpretty.GET,
                        "uri":
                        "/1/_search",
                        "status":
                        HTTPStatus.OK,
                        "content_type":
                        "application/json",
                        "rq":
                        utils.get_fixture(self.search_rq_second),
                        "rs":
                        utils.get_fixture(
                            self.three_hits_search_rs_with_duplicate),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_third),
                        "rs": utils.get_fixture(self.no_hits_search_rs),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_logs, to_json=True)),
                "expected_result": [
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='AB001',
                        relevantItem=3,
                        relevantLogId=3,
                        matchScore=70.0,
                        esScore=15.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=0,
                        usedLogLines=-1,
                        minShouldMatch=80),
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='AB001',
                        relevantItem=1,
                        relevantLogId=1,
                        matchScore=70.0,
                        esScore=15.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=1,
                        usedLogLines=-1,
                        minShouldMatch=80),
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=178,
                        issueType='PB001',
                        relevantItem=2,
                        relevantLogId=2,
                        matchScore=70.0,
                        esScore=10.0,
                        esPosition=1,
                        modelFeatureNames='0',
                        modelFeatureValues='0.67',
                        modelInfo='',
                        resultPosition=2,
                        usedLogLines=-1,
                        minShouldMatch=80)
                ],
                "boost_predict": ([1, 1, 1], [[0.3, 0.7], [0.3, 0.7],
                                              [0.3, 0.7]])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_merged_first),
                        "rs": utils.get_fixture(self.one_hit_search_rs_merged),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_merged_second),
                        "rs": utils.get_fixture(self.one_hit_search_rs_merged),
                    },
                    {
                        "method": httpretty.GET,
                        "uri": "/1/_search",
                        "status": HTTPStatus.OK,
                        "content_type": "application/json",
                        "rq": utils.get_fixture(self.search_rq_merged_third),
                        "rs": utils.get_fixture(self.one_hit_search_rs_merged),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_merged_logs, to_json=True)),
                "expected_result": [
                    launch_objects.SuggestAnalysisResult(
                        testItem=123,
                        testItemLogId=-1,
                        issueType='AB001',
                        relevantItem=1,
                        relevantLogId=1,
                        matchScore=90.0,
                        esScore=10.0,
                        esPosition=0,
                        modelFeatureNames='0',
                        modelFeatureValues='1.0',
                        modelInfo='',
                        resultPosition=0,
                        usedLogLines=-1,
                        minShouldMatch=80)
                ],
                "boost_predict": ([1], [[0.1, 0.9]])
            },
            {
                "test_calls": [
                    {
                        "method": httpretty.GET,
                        "uri": "/1",
                        "status": HTTPStatus.OK,
                    },
                    {
                        "method":
                        httpretty.GET,
                        "uri":
                        "/1/_search",
                        "status":
                        HTTPStatus.OK,
                        "content_type":
                        "application/json",
                        "rq":
                        utils.get_fixture(self.search_rq_merged_first),
                        "rs":
                        utils.get_fixture(self.one_hit_search_rs_merged_wrong),
                    },
                    {
                        "method":
                        httpretty.GET,
                        "uri":
                        "/1/_search",
                        "status":
                        HTTPStatus.OK,
                        "content_type":
                        "application/json",
                        "rq":
                        utils.get_fixture(self.search_rq_merged_second),
                        "rs":
                        utils.get_fixture(self.one_hit_search_rs_merged_wrong),
                    },
                    {
                        "method":
                        httpretty.GET,
                        "uri":
                        "/1/_search",
                        "status":
                        HTTPStatus.OK,
                        "content_type":
                        "application/json",
                        "rq":
                        utils.get_fixture(self.search_rq_merged_third),
                        "rs":
                        utils.get_fixture(self.one_hit_search_rs_merged_wrong),
                    },
                ],
                "test_item_info":
                launch_objects.TestItemInfo(**utils.get_fixture(
                    self.suggest_test_item_info_w_merged_logs, to_json=True)),
                "expected_result": [],
                "boost_predict": ([], [])
            },
        ]

        for idx, test in enumerate(tests):
            with sure.ensure('Error in the test case number: {0}', idx):
                self._start_server(test["test_calls"])
                config = self.get_default_search_config()
                suggest_service = SuggestService(app_config=self.app_config,
                                                 search_cfg=config)
                _boosting_decision_maker = BoostingDecisionMaker()
                _boosting_decision_maker.get_feature_ids = MagicMock(
                    return_value=[0])
                _boosting_decision_maker.predict = MagicMock(
                    return_value=test["boost_predict"])
                suggest_service.suggest_decision_maker = _boosting_decision_maker
                response = suggest_service.suggest_items(
                    test["test_item_info"])

                response.should.have.length_of(len(test["expected_result"]))
                for real_resp, expected_resp in zip(response,
                                                    test["expected_result"]):
                    real_resp.should.equal(expected_resp)

                TestSuggestService.shutdown_server(test["test_calls"])
    def test_full_data_check_suggests(self):
        print("Boost model folder suggests: ", self.suggest_boost_model_folder)
        print("Weights model folder suggests: ", self.weights_folder)
        decision_maker = BoostingDecisionMaker(
            folder=self.suggest_boost_model_folder)
        boost_model_results = utils.get_fixture(
            self.suggest_boost_model_results, to_json=True)
        tests = []
        all_configs = [(-1, [
            "detected_message_extended",
            "detected_message_without_params_extended",
            "detected_message_without_params_and_brackets"
        ], decision_maker),
                       (2, [
                           "message_extended",
                           "message_without_params_extended",
                           "message_without_params_and_brackets"
                       ], decision_maker)]
        for log_lines, filter_fields_any, _decision_maker in all_configs:
            tests.extend([
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message_suggest,
                                        to_json=True),
                      utils.get_fixture(self.one_hit_search_rs_explained,
                                        to_json=True))],
                    "config":
                    TestBoostingModel.get_default_config(
                        number_of_log_lines=log_lines,
                        filter_fields=[],
                        filter_fields_any=filter_fields_any,
                        min_should_match=0.4),
                    "decision_maker":
                    _decision_maker
                },
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message_suggest,
                                        to_json=True),
                      utils.get_fixture(self.two_hits_search_rs_explained,
                                        to_json=True))],
                    "config":
                    TestBoostingModel.get_default_config(
                        number_of_log_lines=log_lines,
                        filter_fields=[],
                        filter_fields_any=filter_fields_any,
                        min_should_match=0.4),
                    "decision_maker":
                    _decision_maker
                },
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message_suggest,
                                        to_json=True),
                      utils.get_fixture(self.two_hits_search_rs_explained,
                                        to_json=True)),
                     (utils.get_fixture(self.log_message_suggest,
                                        to_json=True),
                      utils.get_fixture(self.one_hit_search_rs_explained,
                                        to_json=True))],
                    "config":
                    TestBoostingModel.get_default_config(
                        number_of_log_lines=log_lines,
                        filter_fields=[],
                        filter_fields_any=filter_fields_any,
                        min_should_match=0.4),
                    "decision_maker":
                    _decision_maker
                },
                {
                    "elastic_results":
                    [(utils.get_fixture(self.log_message_only_small_logs,
                                        to_json=True),
                      utils.get_fixture(self.two_hits_search_rs_small_logs,
                                        to_json=True))],
                    "config":
                    TestBoostingModel.get_default_config(
                        number_of_log_lines=log_lines,
                        filter_fields=[],
                        filter_fields_any=filter_fields_any,
                        min_should_match=0.4),
                    "decision_maker":
                    _decision_maker
                },
            ])
        for idx, test in enumerate(tests):
            feature_ids = test["decision_maker"].get_feature_ids()
            weight_log_sim = None
            if self.weights_folder.strip():
                weight_log_sim = weighted_similarity_calculator.\
                    WeightedSimilarityCalculator(folder=self.weights_folder)
            _boosting_featurizer = SuggestBoostingFeaturizer(
                test["elastic_results"],
                test["config"],
                feature_ids,
                weighted_log_similarity_calculator=weight_log_sim)
            with sure.ensure('Error in the test case number: {0}', idx):
                gathered_data, test_item_ids = _boosting_featurizer.gather_features_info(
                )
                predict_label, predict_probability = test[
                    "decision_maker"].predict(gathered_data)
                gathered_data.should.equal(boost_model_results[str(idx)][0],
                                           epsilon=self.epsilon)

                predict_label.tolist().should.equal(
                    boost_model_results[str(idx)][1], epsilon=self.epsilon)
                predict_probability.tolist().should.equal(
                    boost_model_results[str(idx)][2], epsilon=self.epsilon)
Ejemplo n.º 6
0
    def test_analyze_logs(self):
        """Test analyzing logs"""
        tests = [
            {
                "test_calls":          [{"method":         httpretty.GET,
                                         "uri":            "/1",
                                         "status":         HTTPStatus.OK,
                                         }, ],
                "index_rq":            utils.get_fixture(self.launch_wo_test_items),
                "expected_count":      0,
                "expected_issue_type": "",
                "boost_predict":       ([], [])
            },
            {
                "test_calls":          [{"method":         httpretty.GET,
                                         "uri":            "/1",
                                         "status":         HTTPStatus.OK,
                                         }, ],
                "index_rq":            utils.get_fixture(
                    self.launch_w_test_items_wo_logs),
                "expected_count":      0,
                "expected_issue_type": "",
                "boost_predict":       ([], [])
            },
            {
                "test_calls":          [{"method":         httpretty.GET,
                                         "uri":            "/2",
                                         "status":         HTTPStatus.OK,
                                         }, ],
                "index_rq":            utils.get_fixture(
                    self.launch_w_test_items_w_empty_logs),
                "expected_count":      0,
                "expected_issue_type": "",
                "boost_predict":       ([], [])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }, ],
                "msearch_results": [utils.get_fixture(self.no_hits_search_rs, to_json=True),
                                    utils.get_fixture(self.no_hits_search_rs, to_json=True)],
                "index_rq":       utils.get_fixture(self.launch_w_test_items_w_logs),
                "expected_count":      0,
                "expected_issue_type": "",
                "boost_predict":       ([], [])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.NOT_FOUND,
                                    }, ],
                "index_rq":       utils.get_fixture(self.launch_w_test_items_w_logs),
                "expected_count":      0,
                "expected_issue_type": "",
                "boost_predict":       ([], [])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }],
                "msearch_results": [utils.get_fixture(self.no_hits_search_rs, to_json=True),
                                    utils.get_fixture(self.one_hit_search_rs, to_json=True)],
                "index_rq":       utils.get_fixture(self.launch_w_test_items_w_logs),
                "expected_count": 1,
                "expected_issue_type": "AB001",
                "boost_predict":       ([1], [[0.2, 0.8]])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }],
                "msearch_results": [utils.get_fixture(self.one_hit_search_rs, to_json=True),
                                    utils.get_fixture(self.two_hits_search_rs, to_json=True)],
                "index_rq":       utils.get_fixture(self.launch_w_test_items_w_logs),
                "expected_count": 1,
                "expected_issue_type": "AB001",
                "boost_predict":       ([1, 0], [[0.2, 0.8], [0.7, 0.3]])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }],
                "msearch_results": [utils.get_fixture(self.two_hits_search_rs, to_json=True),
                                    utils.get_fixture(self.three_hits_search_rs, to_json=True)],
                "index_rq":       utils.get_fixture(self.launch_w_test_items_w_logs),
                "expected_count": 1,
                "expected_issue_type": "AB001",
                "boost_predict":       ([1, 1], [[0.2, 0.8], [0.3, 0.7]])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }],
                "msearch_results": [utils.get_fixture(self.no_hits_search_rs, to_json=True),
                                    utils.get_fixture(self.three_hits_search_rs, to_json=True)],
                "index_rq":       utils.get_fixture(self.launch_w_test_items_w_logs),
                "expected_count": 1,
                "expected_issue_type": "PB001",
                "boost_predict":       ([0, 1], [[0.8, 0.2], [0.3, 0.7]])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }],
                "msearch_results": [utils.get_fixture(self.no_hits_search_rs, to_json=True),
                                    utils.get_fixture(self.three_hits_search_rs, to_json=True)],
                "index_rq":       utils.get_fixture(self.launch_w_test_items_w_logs),
                "expected_count": 1,
                "expected_issue_type": "AB001",
                "boost_predict":       ([1, 0], [[0.2, 0.8], [0.7, 0.3]])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }],
                "msearch_results": [utils.get_fixture(self.two_hits_search_rs, to_json=True)],
                "index_rq":       utils.get_fixture(
                    self.launch_w_test_items_w_logs_to_be_merged),
                "expected_count": 0,
                "expected_issue_type": "",
                "boost_predict":       ([], [])
            },
            {
                "test_calls":     [{"method":         httpretty.GET,
                                    "uri":            "/2",
                                    "status":         HTTPStatus.OK,
                                    }],
                "msearch_results": [
                    utils.get_fixture(self.no_hits_search_rs, to_json=True),
                    utils.get_fixture(self.three_hits_search_rs_with_one_unique_id, to_json=True)],
                "index_rq":       utils.get_fixture(
                    self.launch_w_test_items_w_logs),
                "expected_count": 1,
                "expected_issue_type": "AB001",
                "boost_predict":       ([1], [[0.2, 0.8]])
            }
        ]

        for idx, test in enumerate(tests):
            with sure.ensure('Error in the test case number: {0}', idx):
                self._start_server(test["test_calls"])
                config = self.get_default_search_config()
                analyzer_service = AutoAnalyzerService(app_config=self.app_config,
                                                       search_cfg=config)
                _boosting_decision_maker = BoostingDecisionMaker()
                _boosting_decision_maker.get_feature_ids = MagicMock(return_value=[0])
                _boosting_decision_maker.predict = MagicMock(return_value=test["boost_predict"])
                if "msearch_results" in test:
                    analyzer_service.es_client.es_client.msearch = MagicMock(
                        return_value={"responses": test["msearch_results"]})
                analyzer_service.boosting_decision_maker = _boosting_decision_maker

                launches = [launch_objects.Launch(**launch)
                            for launch in json.loads(test["index_rq"])]
                response = analyzer_service.analyze_logs(launches)

                response.should.have.length_of(test["expected_count"])

                if test["expected_issue_type"] != "":
                    test["expected_issue_type"].should.equal(response[0].issueType)

                if "expected_id" in test:
                    test["expected_id"].should.equal(response[0].relevantItem)

                TestAutoAnalyzerService.shutdown_server(test["test_calls"])