-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feature: except / only fields for task and tasks endpoints #1436
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -493,6 +493,8 @@ def get(self): | |||||||||||||||||
| :query state: filter tasks by state | ||||||||||||||||||
| :query received_start: filter tasks by received date (must be greater than) format %Y-%m-%d %H:%M | ||||||||||||||||||
| :query received_end: filter tasks by received date (must be less than) format %Y-%m-%d %H:%M | ||||||||||||||||||
| :query only_fields: returns only selected fields for tasks (comma-separated) | ||||||||||||||||||
| :query except_fields: returns all but selected fields for tasks (comma-separated) | ||||||||||||||||||
| :reqheader Authorization: optional OAuth token to authenticate | ||||||||||||||||||
| :statuscode 200: no error | ||||||||||||||||||
| :statuscode 401: unauthorized request | ||||||||||||||||||
|
|
@@ -507,6 +509,8 @@ def get(self): | |||||||||||||||||
| received_end = self.get_argument('received_end', None) | ||||||||||||||||||
| sort_by = self.get_argument('sort_by', None) | ||||||||||||||||||
| search = self.get_argument('search', None) | ||||||||||||||||||
| only_fields = self.get_argument('only_fields', None) | ||||||||||||||||||
| except_fields = self.get_argument('except_fields', None) | ||||||||||||||||||
|
|
||||||||||||||||||
| limit = limit and int(limit) | ||||||||||||||||||
| offset = max(offset, 0) | ||||||||||||||||||
|
|
@@ -522,7 +526,7 @@ def get(self): | |||||||||||||||||
| received_end=received_end, | ||||||||||||||||||
| search=search | ||||||||||||||||||
| ): | ||||||||||||||||||
| task = tasks.as_dict(task) | ||||||||||||||||||
| task = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields) | ||||||||||||||||||
| worker = task.pop('worker', None) | ||||||||||||||||||
| if worker is not None: | ||||||||||||||||||
| task['worker'] = worker.hostname | ||||||||||||||||||
|
|
@@ -621,18 +625,22 @@ def get(self, taskid): | |||||||||||||||||
| "worker": "celery@worker1" | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| :query only_fields: returns only selected fields for task (comma-separated) | ||||||||||||||||||
| :query except_fields: returns all but selected fields for task (comma-separated) | ||||||||||||||||||
| :reqheader Authorization: optional OAuth token to authenticate | ||||||||||||||||||
| :statuscode 200: no error | ||||||||||||||||||
| :statuscode 401: unauthorized request | ||||||||||||||||||
| :statuscode 404: unknown task | ||||||||||||||||||
| """ | ||||||||||||||||||
| only_fields = self.get_argument('only_fields', None) | ||||||||||||||||||
| except_fields = self.get_argument('except_fields', None) | ||||||||||||||||||
|
|
||||||||||||||||||
| task = tasks.get_task_by_id(self.application.events, taskid) | ||||||||||||||||||
| if not task: | ||||||||||||||||||
| raise HTTPError(404, f"Unknown task '{taskid}'") | ||||||||||||||||||
|
|
||||||||||||||||||
| response = task.as_dict() | ||||||||||||||||||
| if task.worker is not None: | ||||||||||||||||||
| response = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields) | ||||||||||||||||||
| if task.worker is not None and 'worker' in response: | ||||||||||||||||||
| response['worker'] = task.worker.hostname | ||||||||||||||||||
|
|
||||||||||||||||||
|
Comment on lines
+642
to
645
|
||||||||||||||||||
| response = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields) | |
| if task.worker is not None and 'worker' in response: | |
| response['worker'] = task.worker.hostname | |
| # Ensure the worker field is set to the hostname before filtering | |
| if task.worker is not None: | |
| task.worker = task.worker.hostname | |
| response = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -66,5 +66,34 @@ def get_task_by_id(events, task_id): | |||||||||||||||||||
| return events.state.tasks.get(task_id) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| def as_dict(task): | ||||||||||||||||||||
| return task.as_dict() | ||||||||||||||||||||
| def filter_dict(task_dict, only_fields=None, except_fields=None): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Filter a dictionary based on only_fields or except_fields parameters. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Args: | ||||||||||||||||||||
| task_dict (dict): The dictionary to filter | ||||||||||||||||||||
| only_fields (str or list): Fields to include (excludes all others) | ||||||||||||||||||||
| except_fields (str or list): Fields to exclude (includes all others) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Returns: | ||||||||||||||||||||
| dict: The filtered dictionary | ||||||||||||||||||||
| """ | ||||||||||||||||||||
|
Comment on lines
+79
to
+80
|
||||||||||||||||||||
| dict: The filtered dictionary | |
| """ | |
| dict: The filtered dictionary | |
| Raises: | |
| ValueError: If both only_fields and except_fields are provided. | |
| """ | |
| if only_fields and except_fields: | |
| raise ValueError("Cannot specify both only_fields and except_fields. Please provide only one.") |
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.
The worker hostname transformation on lines 530-532 happens after filtering, which means if 'worker' is included in only_fields or excluded from except_fields, the transformation may not work as expected. The worker field should be transformed before applying field filtering.