-
Notifications
You must be signed in to change notification settings - Fork 25
Add Tag field to Run table and controller #140
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
Draft
amathuria
wants to merge
4
commits into
ceph:main
Choose a base branch
from
amathuria:wip-amat-add-metadata-tag-74166
base: main
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.
+240
−11
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
80e2e76
models/runs.py: add tag field to Run table
amathuria 562bbaf
controllers/runs.py: add tag filtering and TagsController
amathuria 08b5092
tests: add tests for run tag feature
amathuria c3bb330
runs: Store tags[] and allow updating runs
amathuria 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import logging | ||
| import datetime | ||
| from sqlalchemy import Date, cast | ||
| from sqlalchemy import Date, cast, UnicodeText | ||
| from sqlalchemy.exc import InvalidRequestError, OperationalError | ||
|
|
||
| from pecan import abort, conf, expose, request | ||
|
|
@@ -15,8 +15,17 @@ | |
| date_format = '%Y-%m-%d' | ||
|
|
||
|
|
||
| def latest_runs(fields=None, count=conf.default_latest_runs_count, page=1): | ||
| query = Run.query.order_by(Run.posted.desc()) | ||
| def latest_runs(fields=None, count=conf.default_latest_runs_count, page=1, tag=None): | ||
| query = Run.query | ||
| if tag: | ||
| # JSONType uses process_bind_param (json.dumps) for bind params and | ||
| # process_result_value (json.loads) when reading rows. The read path | ||
| # is fine; the filter RHS here is a bind param, so it would be | ||
| # JSON-encoded unless we compare as plain text: | ||
| tags_text = cast(Run.tags, UnicodeText) | ||
| for t in tag.split(','): | ||
| query = query.filter(tags_text.contains('"%s"' % t.strip())) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (minor) so if tags_text is json "[abc,xyz]" the contains('bc') matches?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| query = query.order_by(Run.posted.desc()) | ||
| query = offset_query(query, page_size=count, page=page) | ||
| runs = list(query) | ||
| if fields: | ||
|
|
@@ -71,6 +80,26 @@ def index(self): | |
| json_run['jobs'] = self.run.get_jobs() | ||
| return json_run | ||
|
|
||
| @index.when(method='PUT', template='json') | ||
| def index_put(self): | ||
| if not self.run: | ||
| error('/errors/not_found/', | ||
| 'attempted to update a non-existent run') | ||
| try: | ||
| data = request.json | ||
| except ValueError: | ||
| rollback() | ||
| error('/errors/invalid/', 'could not decode JSON body') | ||
|
|
||
| if 'tags' in data: | ||
| tags = data['tags'] | ||
| if not isinstance(tags, list): | ||
| error('/errors/invalid/', "'tags' must be a list of strings") | ||
| self.run.tags = tags | ||
|
|
||
| log.info("Updated run: %r", self.run) | ||
| return {} | ||
|
|
||
| @index.when(method='DELETE', template='json') | ||
| def index_delete(self): | ||
| if not self.run: | ||
|
|
@@ -150,6 +179,25 @@ def get_lookup_controller(self): | |
| return SuiteController | ||
|
|
||
|
|
||
| class TagsController(RunFilterIndexController): | ||
| def get_subquery(self, query): | ||
| return query.values(Run.tags) | ||
|
|
||
| @expose('json') | ||
| @retryOperation | ||
| def index(self): | ||
| query = request.context.get('query', Run.query) | ||
| all_tags = set() | ||
| for (tags_value,) in self.get_subquery(query): | ||
| if tags_value: | ||
| for tag in tags_value: | ||
| all_tags.add(tag) | ||
| return sorted(all_tags) | ||
|
|
||
| def get_lookup_controller(self): | ||
| return TagController | ||
|
|
||
|
|
||
| class UsersController(RunFilterIndexController): | ||
| def get_subquery(self, query): | ||
| return query.values(Run.user) | ||
|
|
@@ -202,6 +250,8 @@ def get_lookup_controller(self, field): | |
| return StatusesController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'user': | ||
| return UsersController() | ||
| if field == 'flavor': | ||
|
|
@@ -232,6 +282,8 @@ def get_lookup_controller(self, field): | |
| return Sha1sController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'user': | ||
| return UsersController() | ||
| if field == 'flavor': | ||
|
|
@@ -253,6 +305,8 @@ def get_lookup_controller(self, field): | |
| return Sha1sController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'user': | ||
| return UsersController() | ||
| if field == 'flavor': | ||
|
|
@@ -274,6 +328,8 @@ def get_lookup_controller(self, field): | |
| return Sha1sController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'user': | ||
| return UsersController() | ||
| if field == 'flavor': | ||
|
|
@@ -295,6 +351,34 @@ def get_lookup_controller(self, field): | |
| return Sha1sController() | ||
| if field == 'status': | ||
| return StatusesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'user': | ||
| return UsersController() | ||
| if field == 'flavor': | ||
| return FlavorsController() | ||
|
|
||
|
|
||
| class TagController(RunFilterController): | ||
| def get_subquery(self, query): | ||
| # Same as latest_runs: compare as UnicodeText so the filter bind param | ||
| # is not run through JSONType.process_bind_param (json.dumps). | ||
| return query.filter( | ||
| cast(Run.tags, UnicodeText).contains('"%s"' % self.value)) | ||
|
|
||
| def get_lookup_controller(self, field): | ||
| if field == 'branch': | ||
| return BranchesController() | ||
| if field == 'date': | ||
| return DatesController() | ||
| if field == 'machine_type': | ||
| return MachineTypesController() | ||
| if field == 'sha1': | ||
| return Sha1sController() | ||
| if field == 'status': | ||
| return StatusesController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'user': | ||
| return UsersController() | ||
| if field == 'flavor': | ||
|
|
@@ -318,6 +402,8 @@ def get_lookup_controller(self, field): | |
| return StatusesController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'flavor': | ||
| return FlavorsController() | ||
|
|
||
|
|
@@ -339,6 +425,8 @@ def get_lookup_controller(self, field): | |
| return StatusesController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'user': | ||
| return UsersController() | ||
|
|
||
|
|
@@ -393,6 +481,8 @@ def get_lookup_controller(self, field): | |
| return StatusesController() | ||
| if field == 'suite': | ||
| return SuitesController() | ||
| if field == 'tag': | ||
| return TagsController() | ||
| if field == 'user': | ||
| return UsersController() | ||
| if field == 'flavor': | ||
|
|
@@ -402,31 +492,36 @@ def get_lookup_controller(self, field): | |
|
|
||
| class RunsController(object): | ||
| @expose(generic=True, template='json') | ||
| def index(self, fields='', count=conf.default_latest_runs_count, page=1): | ||
| return latest_runs(fields=fields, count=count, page=page) | ||
| def index(self, fields='', count=conf.default_latest_runs_count, page=1, tag=None): | ||
| return latest_runs(fields=fields, count=count, page=page, tag=tag) | ||
|
|
||
| @index.when(method='POST', template='json') | ||
| def index_post(self): | ||
| # save to DB here | ||
| try: | ||
| name = request.json.get('name') | ||
| tags = request.json.get('tags') | ||
| except ValueError: | ||
| rollback() | ||
| error('/errors/invalid/', 'could not decode JSON body') | ||
|
|
||
| if not name: | ||
| error('/errors/invalid/', "could not find required key: 'name'") | ||
|
|
||
| if tags is not None and not isinstance(tags, list): | ||
| error('/errors/invalid/', "'tags' must be a list of strings") | ||
|
|
||
| if not Run.query.filter_by(name=name).first(): | ||
| self._create_run(name) | ||
| self._create_run(name, tags=tags) | ||
| return dict() | ||
| else: | ||
| error('/errors/invalid/', "run with name %s already exists" % name) | ||
|
|
||
| @classmethod | ||
| @retryOperation | ||
| def _create_run(cls, name): | ||
| log.info("Creating run: %s", name) | ||
| def _create_run(cls, name, tags=None): | ||
| log.info("Creating run: %s with tags: %r", name, tags) | ||
| Session.flush() | ||
| return Run(name) | ||
| return Run(name, tags=tags) | ||
|
|
||
| branch = BranchesController() | ||
|
|
||
|
|
@@ -438,6 +533,8 @@ def _create_run(cls, name): | |
|
|
||
| suite = SuitesController() | ||
|
|
||
| tag = TagsController() | ||
|
|
||
| queued = QueuedRunsController() | ||
|
|
||
| sha1 = Sha1sController() | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
don't we want
tags=None?