示例#1
0
def generate_chart_url(body):  #noga: E501
    response = None

    if connexion.request.is_json:
        content = connexion.request.get_json()
        app.logger.info("Generate Chart: {}".format(content))
        request = PrioritizedRecommendationsRequest.from_dict(content)

        if request.unique_key() in CACHED_CHART_URLs:
            chart_url = CACHED_CHART_URLs[request.unique_key()]
            chart_key = chart_url.split("/")[-1]
            if chart_key in CHART_REQUESTs:
                return ChartResponse(False, None, CACHED_CHART_URLs[request.unique_key()])

        chart_key = "".join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(16))
        chart_url = "http://{}:{}/prioritizer/chart/c/{}".format(helper.app_host(), helper.app_port(), chart_key)

        limit_bugs = 0  #800
        bugzilla_fetcher = bugzillafetcher.BugzillaFetcher("https://bugs.eclipse.org/bugs/rest/bug")
        bugs = bugzilla_fetcher.fetch_bugs(request.assignee, request.products, request.components,
                                           "RESOLVED", limit=limit_bugs)
        requirements = list(map(lambda b: Requirement.from_bug(b), bugs))
        ASSIGNED_RESOLVED_REQUIREMENTS_OF_STAKEHOLDER[request.unique_key()] = requirements
        CACHED_CHART_URLs[request.unique_key()] = chart_url
        CHART_REQUESTs[chart_key] = request
        response = ChartResponse(False, None, chart_url)

    return response
示例#2
0
def rank_position_of_issue(requirement_id: int, agent_id: str, assignee: str, components: List[str],
                           products: List[str], keywords: List[str]):
    unique_key = PrioritizedRecommendationsRequest(agent_id=agent_id, assignee=assignee, components=components,
                                                   products=products, keywords=keywords).unique_key()

    if unique_key not in CACHED_PRIORITIZATIONS:
        return None, None

    sorted_requirements, _, _ = CACHED_PRIORITIZATIONS[unique_key]
    filtered_issues = list(filter(lambda r: r.id == requirement_id, sorted_requirements))
    if len(filtered_issues) != 1:
        return None, None

    ranked_position = list(map(lambda r: r.id, sorted_requirements)).index(requirement_id)
    return ranked_position, filtered_issues[0].computed_priority
    def test_compute_prioritization(self):
        expected_components = ["UI", "IDE"]
        expected_products = ["Platform"]
        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee="*****@*****.**",
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        self.assertTrue(
            len(ranked_bugs) > 0, "List of prioritizes requirements is empty!")
        self.assertTrue(
            all(map(lambda r: 0.1 <= r["priority"] <= 100.0, ranked_bugs)))
        for rb in ranked_bugs:
            self.assertIn(rb["component"], expected_components,
                          "Unexpected component: {}".format(rb["component"]))
            self.assertIn(rb["product"], expected_products,
                          "Unexpected product: {}".format(rb["product"]))
            self.assertIsInstance(rb["id"], int)
            self.assertIsInstance(rb["keywords"], list)
            self.assertIsInstance(rb["milestone"], str)
            self.assertIsInstance(rb["numberOfCC"], int)
            self.assertIsInstance(rb["priority"], int)
            # make sure that no requirements are included with priority 1
            self.assertGreaterEqual(rb["priority"], 1)
            self.assertIsInstance(rb["summary"], str)

        # make sure that requirement summaries contain uppercase letters (i.e., are not lowercased by the prioritizer)
        self.assertTrue(
            any(c.isupper() for rb in ranked_bugs for c in rb["summary"]))
示例#4
0
def recommend_prioritized_issues(body):  # noqa: E501
    """Retrieve an ordered list of recommended requirements.

     # noqa: E501

    :param body: Requirement objects for which the social popularity should be measured
    :type body: list | bytes

    :rtype: List[Requirement]
    """

    response = None
    prioritizer = requirementprioritizer.RequirementPrioritizer(db)

    if connexion.request.is_json:
        content = connexion.request.get_json()
        request = PrioritizedRecommendationsRequest.from_dict(content)
        refetch_threshold_limit = LIMIT - 5
        """
        reserved_aids = {
            'a1a1a1a1a': 0,
            'b2b2b2b2b': 1
        }

        version_key = "VERSION_{}".format(request.agent_id)
        version = db.get(version_key) if request.agent_id not in reserved_aids else reserved_aids[request.agent_id]

        if version is False:
            version = bernoulli.rvs(0.5)
            db.set(version_key, version)
            db.dump()
        """
        version = 0
        fetch = True
        is_version_redirect = False

        if request.unique_key() in CACHED_PRIORITIZATIONS:
            requirements, user_profile, _ = CACHED_PRIORITIZATIONS[request.unique_key()]
            sorted_requirements, is_version_redirect = prioritizer.prioritize(
                agent_id=request.agent_id, requirements=requirements,
                user_profile=user_profile, preferred_keywords=request.keywords,
                max_age_years=MAX_AGE_YEARS, version=version)
            fetch = len(sorted_requirements) < refetch_threshold_limit

        if fetch:
            try:
                result = prioritizer.fetch_and_prioritize(agent_id=request.agent_id, assignee=request.assignee,
                                                          components=request.components, products=request.products,
                                                          preferred_keywords=request.keywords, limit=LIMIT,
                                                          max_age_years=MAX_AGE_YEARS, version=version)
                sorted_requirements, user_profile, is_version_redirect = result
                CACHED_PRIORITIZATIONS[request.unique_key()] = result
            except Exception as e:
                app.logger.error(e, exc_info=True)
                return PrioritizedRecommendationsResponse(True, "An error occurred! Please try again later.")

        ranked_bugs_list = []
        app.logger.info("Prioritize: version={}; version_redirect={}; request=({})".format(version, is_version_redirect,
                                                                                           content))

        for r in sorted_requirements:
            ranked_bugs_list += [{
                "id": r.id,
                "summary": r.summary,
                "product": r.product,
                "component": r.component,
                "priority":  int("{0:.0f}".format(r.computed_priority)),
                "numberOfCC": len(r.cc),
                "milestone": r.target_milestone,
                "keywords": r.summary_tokens,
                "creation_time": r.creation_time,
                "liked": db.exists("LIKE_{}_{}".format(request.agent_id, r.id)),
                "url": "/prioritizer/view/i/{}/k/{}".format(r.id, urllib.parse.quote(request.unique_key(), safe=''))
            }]

        response = PrioritizedRecommendationsResponse(False, None, ranked_bugs=ranked_bugs_list)

    return response
    def test_compute_prioritization_for_assignee_with_keywords(self):
        expected_components = ["UI", "IDE"]
        expected_products = ["Platform"]
        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee="*****@*****.**",
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        self.assertTrue(
            len(ranked_bugs) > 0, "List of prioritizes requirements is empty!")
        self.assertTrue(
            all(map(lambda r: 0.1 <= r["priority"] <= 100.0, ranked_bugs)))
        rb = ranked_bugs[-1]
        id_of_last_requirement = rb["id"]
        self.assertIsInstance(rb["keywords"], list)
        keywords_of_last_requirement = rb["keywords"]

        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee="*****@*****.**",
            components=expected_components,
            products=expected_products,
            keywords=keywords_of_last_requirement)
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        self.assertTrue(
            len(ranked_bugs) > 0, "List of prioritizes requirements is empty!")
        self.assertTrue(
            all(map(lambda r: 0.1 <= r["priority"] <= 100.0, ranked_bugs)))
        for idx, rb in enumerate(ranked_bugs):
            if rb["id"] != id_of_last_requirement:
                continue
            self.assertIsInstance(rb["keywords"], list)
            self.assertEquals(rb["keywords"], keywords_of_last_requirement)
            # make sure that the requirement is not the last requirement any more
            self.assertGreater(len(ranked_bugs), idx)
    def test_delete_user_profile_and_check_cache(self):
        assignee = "*****@*****.**"
        expected_components = ["UI", "IDE"]
        expected_products = ["Platform"]
        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee=assignee,
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")

        body = DeleteProfileRequest(agent_id=self.agent_id)
        response = self.client.open("/prioritizer/profile/delete",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")

        db = pickledb.load(os.path.join(helper.DATA_PATH, "storage.db"), False)
        all_keys = db.getall()
        remaining_keys_containing_agent_id = list(
            filter(lambda k: self.agent_id in k, all_keys))
        self.assertEqual(len(remaining_keys_containing_agent_id), 0,
                         "No or not all keys were deleted!")
        self.assertEqual(
            len(
                list(
                    filter(
                        lambda k: k.startswith("{}_".format(self.agent_id)),
                        recommendation_controller.CACHED_PRIORITIZATIONS.keys(
                        )))), 0)
        self.assertEqual(
            len(
                list(
                    filter(
                        lambda k: k.startswith("{}_".format(self.agent_id)),
                        recommendation_controller.CACHED_CHART_URLs.keys()))),
            0)
        self.assertEqual(
            len(
                list(
                    filter(lambda k: k.startswith("{}_".format(self.agent_id)),
                           recommendation_controller.CHART_REQUESTs.keys()))),
            0)
        return response
    def test_defer_requirement(self):
        assignee = "*****@*****.**"
        expected_components = ["UI", "IDE"]
        expected_products = ["Platform"]
        expected_interval = 0.00002314815 * 2  # 4 seconds (expressed in days)
        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee=assignee,
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        deferred_requirement_id = ranked_bugs[0]["id"]
        body = DeferRequirementRequest(id=deferred_requirement_id,
                                       agent_id=self.agent_id,
                                       interval=expected_interval,
                                       assignee=assignee,
                                       components=expected_components,
                                       products=expected_products,
                                       keywords=[])
        response = self.client.open("/prioritizer/defer",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")

        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee=assignee,
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        self.assertTrue(
            len(ranked_bugs) > 0, "List of prioritizes requirements is empty!")
        self.assertTrue(
            all(map(lambda r: 0.1 <= r["priority"] <= 100.0, ranked_bugs)))
        self.assertNotIn(
            deferred_requirement_id, map(lambda rb: rb["id"], ranked_bugs),
            "The deferred requirement is still part of the ranked list!")

        expected_interval_in_s = expected_interval * 1.5 * 24 * 60 * 60
        time.sleep(expected_interval_in_s)

        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee=assignee,
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        self.assertTrue(
            len(ranked_bugs) > 0, "List of prioritizes requirements is empty!")
        self.assertTrue(
            all(map(lambda r: 0.1 <= r["priority"] <= 100.0, ranked_bugs)))
        self.assertIn(
            deferred_requirement_id, map(lambda rb: rb["id"], ranked_bugs),
            "The deferred requirement has not been re-included in the ranked list!"
        )
    def test_unlike_requirement(self):
        assignee = "*****@*****.**"
        expected_components = ["UI"]
        expected_products = ["Platform"]
        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee=assignee,
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        liked_requirement_id = ranked_bugs[-1]["id"]
        body = LikeRequirementRequest(id=liked_requirement_id,
                                      agent_id=self.agent_id,
                                      assignee=assignee,
                                      components=expected_components,
                                      products=expected_products,
                                      keywords=[])
        response = self.client.open("/prioritizer/like",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")

        body = LikeRequirementRequest(id=liked_requirement_id,
                                      agent_id=self.agent_id,
                                      assignee=assignee,
                                      components=expected_components,
                                      products=expected_products,
                                      keywords=[])
        response = self.client.open("/prioritizer/unlike",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")

        body = PrioritizedRecommendationsRequest(
            agent_id=self.agent_id,
            assignee=assignee,
            components=expected_components,
            products=expected_products,
            keywords=[])
        response = self.client.open("/prioritizer/compute",
                                    method="POST",
                                    data=json.dumps(body),
                                    content_type="application/json")
        self.assert200(response,
                       'Response body is: ' + response.data.decode('utf-8'))
        response = response.json
        self.assertFalse(response["error"],
                         "An error occurred while processing the request!")
        if "errorMessage" in response:
            self.assertIsNone(response["errorMessage"],
                              "Error message is not empty!")
        ranked_bugs = response["rankedBugs"]
        self.assertIsInstance(ranked_bugs, list)
        self.assertTrue(
            len(ranked_bugs) > 0, "List of prioritizes requirements is empty!")
        self.assertIn(
            liked_requirement_id, map(lambda r: r["id"], ranked_bugs),
            "The liked requirement is not part of the ranked list any more!")
        for idx, rb in enumerate(ranked_bugs):
            if rb["id"] == liked_requirement_id:
                self.assertGreater(idx, 10)