-
Notifications
You must be signed in to change notification settings - Fork 44
Sync data from S3 directly in GraphImporterTask #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fridex
wants to merge
2
commits into
fabric8-analytics:master
Choose a base branch
from
fridex:gremlin-sync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import os | ||
| import requests | ||
|
|
||
|
|
||
| class GremlinHttpBase(object): | ||
| def __init__(self, gremlin_http_host=None, gremlin_http_port=None): | ||
| self.gremlin_http_host = os.getenv('GREMLIN_HTTP_HOST', gremlin_http_host) | ||
| self.gremlin_http_port = os.getenv('GREMLIN_HTTP_PORT', gremlin_http_port) | ||
|
|
||
|
|
||
| class GremlinHttp(GremlinHttpBase): | ||
| def store_task_result(self, ecosystem, name, version, task_name, task_result): | ||
| # TODO: implement | ||
| print("#"*80) | ||
| print("Storing result for {ecosystem}/{name}/{version}, task {task_name}".format(**locals())) | ||
| print("#"*80) | ||
|
|
||
|
|
||
| class PackageGremlinHttp(GremlinHttpBase): | ||
| def store_task_result(self, ecosystem, name, task_name, task_result): | ||
| # TODO: implement | ||
| print("#"*80) | ||
| print("Storing result for {ecosystem}/{name}/{version}, task {task_name}".format(**locals())) | ||
| print("#"*80) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,42 @@ | ||
| from f8a_worker.base import BaseTask | ||
| import requests | ||
| from os import environ | ||
| from selinon import StoragePool | ||
| from selinon import FatalTaskError | ||
|
|
||
|
|
||
| class GraphImporterTask(BaseTask): | ||
| _SERVICE_HOST = environ.get("BAYESIAN_DATA_IMPORTER_SERVICE_HOST", "bayesian-data-importer") | ||
| _SERVICE_PORT = environ.get("BAYESIAN_DATA_IMPORTER_SERVICE_PORT", "9192") | ||
| _INGEST_SERVICE_ENDPOINT = "api/v1/ingest_to_graph" | ||
| _SELECTIVE_SERVICE_ENDPOINT = "api/v1/selective_ingest" | ||
| _INGEST_API_URL = "http://{host}:{port}/{endpoint}".format(host=_SERVICE_HOST, | ||
| port=_SERVICE_PORT, | ||
| endpoint=_INGEST_SERVICE_ENDPOINT) | ||
|
|
||
| _SELECTIVE_API_URL = "http://{host}:{port}/{endpoint}".format( | ||
| host=_SERVICE_HOST, | ||
| port=_SERVICE_PORT, | ||
| endpoint=_SELECTIVE_SERVICE_ENDPOINT) | ||
| """Sync data from S3 to graph database.""" | ||
|
|
||
| add_audit_info = False | ||
|
|
||
| def execute(self, arguments): | ||
| self._strict_assert(arguments.get('ecosystem')) | ||
| self._strict_assert(arguments.get('name')) | ||
| self._strict_assert(arguments.get('document_id')) | ||
|
|
||
| package_list = [ | ||
| { | ||
| 'ecosystem': arguments['ecosystem'], | ||
| 'name': arguments['name'], | ||
| 'version': arguments.get('version') | ||
| } | ||
| ] | ||
|
|
||
| # If we force graph sync, sync all task results, otherwise only | ||
| # finished in this analysis run | ||
| if not arguments.get('force_graph_sync'): | ||
| # Tasks that need sync to graph start lowercase. | ||
| param = { | ||
| 'select_ingest': [task_name | ||
| for task_name in self.storage.get_finished_task_names( | ||
| arguments['document_id']) | ||
| if task_name[0].islower()], | ||
| 'package_list': package_list | ||
| } | ||
| endpoint = self._SELECTIVE_API_URL | ||
| else: | ||
| param = package_list | ||
| endpoint = self._INGEST_API_URL | ||
| if self.task_name not in ('PackageGraphImporterTask', 'GraphImporterTask'): | ||
| raise FatalTaskError("Unknown task name - cannot distinguish package vs. version level data") | ||
|
|
||
| version_level = self.task_name == 'GraphImporterTask' | ||
|
|
||
| self.log.info("Invoke graph importer at url: '%s' for %s", endpoint, param) | ||
| response = requests.post(endpoint, json=param) | ||
| postgres = StoragePool.get_connected_storage('BayesianPostgres' if version_level else 'PackagePostgres') | ||
| s3 = StoragePool.get_connected_storage('S3Data' if version_level else 'S3PackageData') | ||
| gremlin = StoragePool.get_connected_storage('GremlinHttp' if version_level else 'PackageGremlinHttp') | ||
|
|
||
| if response.status_code != 200: | ||
| raise RuntimeError("Failed to invoke graph import at '%s' for %s" % (endpoint, param)) | ||
| adapter_kwargs = { | ||
| 'ecosystem': arguments['ecosystem'], | ||
| 'name': arguments['name'] | ||
| } | ||
| if version_level: | ||
| self._strict_assert(arguments.get('version')) | ||
| adapter_kwargs['version'] = arguments['version'] | ||
|
|
||
| if arguments.get('force_graph_sync'): | ||
| tasks_to_sync = s3.list_available_task_results(arguments) | ||
| self.log.info("Force sync of all task results available on S3: %s", tasks_to_sync) | ||
| else: | ||
| tasks_to_sync = postgres.get_finished_task_names(arguments['document_id']) | ||
| self.log.info("Syncing results of tasks in the current run: %s", tasks_to_sync) | ||
|
|
||
| self.log.info("Graph import succeeded with response: %s", response.text) | ||
| for task_name in tasks_to_sync: | ||
| task_result = s3.retrieve_task_result(task_name=task_name, **adapter_kwargs) | ||
| gremlin.store_task_result(task_name=task_name, task_result=task_result, **adapter_kwargs) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miteshvp This will probably list task results with the whole object key ("path"). If you don't want to do it that way, feel free to adjust to return only task name.
EDIT: it will be probably needed anyway as this is handled by adapter.