Skip to content

Commit bfa54a4

Browse files
committed
feat: find_tasks_iter
1 parent 2f9e5ed commit bfa54a4

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

examples/task_example.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@
2525
task_request = TaskCreateRequest(flux=flux, org=org, description="Task Description", status="active")
2626
task = tasks_api.create_task(task_create_request=task_request)
2727
print(task)
28+
29+
tasks = tasks_api.find_tasks_iter()
30+
31+
# print all tasks id
32+
for task in tasks:
33+
print(task.id)

influxdb_client/client/tasks_api.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010
from influxdb_client import TasksService, Task, TaskCreateRequest, TaskUpdateRequest, LabelResponse, LabelMapping, \
1111
AddResourceMemberRequestBody, RunManually, Run, LogEvent
1212

13+
class TasksIterator:
14+
def __init__(self, values, next) -> None:
15+
self.values = values
16+
self.next = next
17+
self.no_values = False if values else True
18+
19+
def __iter__(self):
20+
return self
21+
22+
def __next__(self):
23+
if self.no_values:
24+
raise StopIteration
25+
if not self.values:
26+
self.values, self.next = self.next()
27+
if not self.values:
28+
raise StopIteration
29+
return self.values.pop(0)
30+
1331

1432
class TasksApi(object):
1533
"""Implementation for '/api/v2/tasks' endpoint."""
@@ -25,7 +43,7 @@ def find_task_by_id(self, task_id) -> Task:
2543
return task
2644

2745
def find_tasks(self, **kwargs):
28-
"""List all tasks.
46+
"""List all tasks up to set limit (max 500).
2947
3048
:key str name: only returns tasks with the specified name
3149
:key str after: returns tasks after specified ID
@@ -37,6 +55,45 @@ def find_tasks(self, **kwargs):
3755
"""
3856
return self._service.get_tasks(**kwargs).tasks
3957

58+
def _find_tasks_paged(self, **kwargs):
59+
"""List all tasks with ability to list next tasks after limit.
60+
61+
:key str name: only returns tasks with the specified name
62+
:key str after: returns tasks after specified ID
63+
:key str user: filter tasks to a specific user ID
64+
:key str org: filter tasks to a specific organization name
65+
:key str org_id: filter tasks to a specific organization ID
66+
:key int limit: the number of tasks to return in one page
67+
:return: Tasks, Next
68+
"""
69+
tasks = self._service.get_tasks(**kwargs).tasks
70+
71+
last_id = tasks[-1].id if tasks else None
72+
def next():
73+
if last_id is not None:
74+
return self._find_tasks_paged(**{**kwargs, 'after': last_id})
75+
else:
76+
def func():
77+
raise Exception("There are no additional pages remaining for tasks.")
78+
return [], func
79+
80+
return tasks, next
81+
82+
def find_tasks_iter(self, **kwargs):
83+
"""Iterate over all tasks with pagination.
84+
85+
:key str name: only returns tasks with the specified name
86+
:key str after: returns tasks after specified ID
87+
:key str user: filter tasks to a specific user ID
88+
:key str org: filter tasks to a specific organization name
89+
:key str org_id: filter tasks to a specific organization ID
90+
:key int limit: the number of tasks in one page
91+
:return: Tasks iterator
92+
"""
93+
tasks, next = self._find_tasks_paged(**kwargs)
94+
95+
return iter(TasksIterator(tasks, next))
96+
4097
def create_task(self, task: Task = None, task_create_request: TaskCreateRequest = None) -> Task:
4198
"""Create a new task."""
4299
if task_create_request is not None:

0 commit comments

Comments
 (0)