def test_anitya_error(self):
        flexmock(utils).should_receive('get_latest_upstream_details').\
            with_args('foo', 'bar').and_raise(requests.exceptions.RequestException())
        assert safe_get_latest_version('foo', 'bar') is None

        flexmock(utils).should_receive('get_latest_upstream_details').\
            with_args('foo', 'bar').and_return({'versions': []})
        assert safe_get_latest_version('foo', 'bar') is None
 def latest_version(self):
     # prevent import loop
     from cucoslib.utils import safe_get_latest_version
     return safe_get_latest_version(self.version.package.ecosystem.name,
                                    self.version.package.name)
 def test_basic(self):
     flexmock(utils).should_receive('get_latest_upstream_details').\
         with_args('foo', 'bar').and_return({'versions': ['1']})
     assert safe_get_latest_version('foo', 'bar') == '1'
Exemple #4
0
    def get(self, external_request_id):
        submitted_at = ""
        manifest_appstackid_map = {}
        try:
            results = rdb.session.query(StackAnalysisRequest)\
                                 .filter(StackAnalysisRequest.id == external_request_id)
            if results.count() <= 0:
                raise HTTPError(
                    404, "Invalid request ID '{id}' received".format(
                        id=external_request_id))

            row = results.first().to_dict()
            submitted_at = row["submitTime"]
            request_json = json.loads(row["requestJson"])

            for manifest in request_json["manifest"]:
                if manifest.get('appstack_id', 0):
                    manifest_appstackid_map[
                        manifest["filename"]] = manifest["appstack_id"]

        except SQLAlchemyError:
            raise HTTPError(
                500, "Error fetching data for request ID '{id}'".format(
                    id=external_request_id))

        try:
            results = rdb.session.query(WorkerResult)\
                                 .filter(WorkerResult.external_request_id == external_request_id,
                                         WorkerResult.worker == "dependency_aggregator")
            if results.count() <= 0:
                raise HTTPError(
                    202, "Analysis for request ID '{t}' is in progress".format(
                        t=external_request_id))
        except SQLAlchemyError:
            raise HTTPError(
                500,
                "Worker result for request ID '{t}' doesn't exist yet".format(
                    t=external_request_id))

        try:
            if results.count() > 0:
                result = results.first().to_dict()
                audit = result["task_result"]["_audit"]
                manifest_response = []

                # TODO: this will probably need some refactoring

                for manifest in result["task_result"]["result"]:
                    for component in manifest["components"]:
                        component["latest_version"] = safe_get_latest_version(
                            component["ecosystem"], component["name"])
                        component["dependents_count"] = get_dependents_count(
                            component["ecosystem"], component["name"],
                            component["version"], rdb.session)
                        component["relative_usage"] = usage_rank2str(
                            get_component_percentile_rank(
                                component["ecosystem"], component["name"],
                                component["version"], rdb.session))
                    manifest_appstack_id = manifest_appstackid_map.get(
                        manifest["manifest_name"], '')
                    if manifest_appstack_id != '':
                        url = current_app.config['BAYESIAN_ANALYTICS_URL']
                        endpoint = "{analytics_baseurl}/api/v1.0/recommendation/{appstack_id}".format(
                            analytics_baseurl=url,
                            appstack_id=manifest_appstack_id)
                        resp = requests.get(endpoint)
                        if resp.status_code == 200:
                            recommendation = resp.json()

                            # Adding URI of the stacks to the recommendation
                            if recommendation.get("input_stack", {}).get(
                                    "appstack_id", "") != "":
                                recommendation["input_stack"][
                                    "uri"] = "{analytics_baseurl}/api/v1.0/appstack/{appstack_id}".format(
                                        analytics_baseurl=url,
                                        appstack_id=recommendation[
                                            "input_stack"]["appstack_id"])

                            if recommendation.get("recommendations", {}).get(
                                    "similar_stacks", "") != "":
                                for r in recommendation["recommendations"][
                                        "similar_stacks"]:
                                    if r["stack_id"] != "":
                                        r["uri"] = "{analytics_baseurl}/api/v1.0/appstack/{appstack_id}".format(
                                            analytics_baseurl=url,
                                            appstack_id=r["stack_id"])
                            manifest["recommendation"] = recommendation
                        else:
                            current_app.logger.warn("{status}: {error}".format(
                                status=resp.status_code, error=resp.content))

                    manifest_response.append(manifest)
                response = {
                    "status": result["task_result"]["status"],
                    "submitted_at": submitted_at,
                    "started_at": audit["started_at"],
                    "finished_at": audit["ended_at"],
                    "request_id": result["external_request_id"],
                    "result": manifest_response
                }
                return response
        except:
            raise HTTPError(
                500, "Error creating response for request {t}".format(
                    t=external_request_id))