Esempio n. 1
0
    def package_info(self):
        """Get dependents_count and relative_usage."""
        s = Session.object_session(self)
        if s:
            # to avoid cyclic import
            from f8a_worker.utils import (get_package_dependents_count,
                                          get_component_percentile_rank, usage_rank2str)

            count = get_package_dependents_count(self.version.package.ecosystem._backend,
                                                 self.version.package.name, s)
            # TODO: This obviously doesn't belong here. It's getting crowded
            # and unorganized at the top-level, refactoring is needed.
            rank = get_component_percentile_rank(self.version.package.ecosystem._backend,
                                                 self.version.package.name,
                                                 self.version.identifier,
                                                 s)
            return {'dependents_count': count,
                    'relative_usage': usage_rank2str(rank)}
        return {}
Esempio n. 2
0
    def get(self, external_request_id):
        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)
                        rank = get_component_percentile_rank(
                            component["ecosystem"],
                            component["name"],
                            component["version"],
                            rdb.session
                        )
                        component["relative_usage"] = usage_rank2str(rank)
                    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", "") != "":
                                uri = "{analytics_baseurl}/api/v1.0/appstack/{appstack_id}"\
                                      .format(analytics_baseurl=url,
                                              appstack_id=recommendation["input_stack"]["appstack_id"])
                                recommendation["input_stack"]["uri"] = uri

                            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))