Skip to content

Commit d84a2b3

Browse files
committed
Add information about requested task rank in the pipe
1 parent a833966 commit d84a2b3

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

dispatcher/backend/src/common/schemas/orms.py

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def get_schedule_name(task: dbm.RequestedTask) -> str:
121121
schedule_name = mf.Function(serialize=get_schedule_name) # override base
122122
worker = mf.Function(serialize=get_worker_name)
123123
notification = mf.Dict()
124+
rank = mf.Integer()
124125

125126

126127
class MostRecentTaskSchema(m.Schema):

dispatcher/backend/src/routes/requested_tasks/requested_task.py

+12
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ def get(
300300
)
301301
resp = RequestedTaskFullSchema().dump(requested_task)
302302

303+
# also fetch all requested tasks IDs to compute estimated task rank ; this is
304+
# only an estimation since logic to start tasks is more complex than simple
305+
# sorting but it gives a good indicator of when the requested task might
306+
# be expected to start (soon or in a long time) especially for zimit.kiwix.org
307+
stmt = sa.select(
308+
dbm.RequestedTask.id,
309+
).order_by(
310+
dbm.RequestedTask.priority.desc(), dbm.RequestedTask.updated_at.asc()
311+
)
312+
requested_task_ids = session.execute(stmt).scalars().all()
313+
resp["rank"] = requested_task_ids.index(requested_task_id)
314+
303315
# exclude notification to not expose private information (privacy)
304316
# on anonymous requests and requests for users without schedules_update
305317
if not token or not token.get_permission("schedules", "update"):

dispatcher/backend/src/tests/integration/routes/conftest.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ def _make_requested_task(
269269
status=TaskStatus.requested,
270270
requested_by="someone",
271271
priority=0,
272+
request_date=getnow(),
272273
):
273274
events = [TaskStatus.requested]
274-
now = getnow()
275-
timestamp = {event: now for event in events}
275+
timestamp = {event: request_date for event in events}
276276
events = [make_event(event, timestamp[event]) for event in events]
277277

278278
config = {
@@ -291,7 +291,7 @@ def _make_requested_task(
291291
requested_task = dbm.RequestedTask(
292292
status=status,
293293
timestamp=timestamp,
294-
updated_at=now,
294+
updated_at=request_date,
295295
events=events,
296296
requested_by=requested_by,
297297
priority=priority,
@@ -336,6 +336,33 @@ def requested_tasks(make_requested_task):
336336
return tasks
337337

338338

339+
@pytest.fixture(scope="module")
340+
def requested_tasks_2(make_requested_task):
341+
return [
342+
make_requested_task(
343+
schedule_name="recipe1",
344+
request_date=getnow() - datetime.timedelta(minutes=5),
345+
),
346+
make_requested_task(
347+
schedule_name="recipe2",
348+
request_date=getnow() - datetime.timedelta(minutes=4),
349+
),
350+
make_requested_task(
351+
schedule_name="recipe3",
352+
request_date=getnow() - datetime.timedelta(minutes=10),
353+
),
354+
make_requested_task(
355+
schedule_name="recipe4",
356+
request_date=getnow() - datetime.timedelta(minutes=3),
357+
priority=5,
358+
),
359+
make_requested_task(
360+
schedule_name="recipe5",
361+
request_date=getnow() - datetime.timedelta(minutes=1),
362+
),
363+
]
364+
365+
339366
@pytest.fixture(scope="module")
340367
def requested_task(make_requested_task):
341368
return make_requested_task()

dispatcher/backend/src/tests/integration/routes/requested_tasks/test_requested_task.py

+26
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ def test_get(self, client, requested_task, access_token, authenticated):
179179
else:
180180
assert data["notification"] is None
181181

182+
@pytest.mark.parametrize(
183+
"recipename, expected_rank",
184+
[
185+
pytest.param("recipe1", 2, id="recipe1"),
186+
pytest.param("recipe2", 3, id="recipe2"),
187+
pytest.param("recipe3", 1, id="recipe3"),
188+
pytest.param("recipe4", 0, id="recipe4"),
189+
pytest.param("recipe5", 4, id="recipe5"),
190+
],
191+
)
192+
def test_get_requested_task_rank_ok(
193+
self, client, requested_tasks_2, recipename, expected_rank
194+
):
195+
requested_task = [
196+
requested_task
197+
for requested_task in requested_tasks_2
198+
if requested_task["schedule_name"] == recipename
199+
][0]
200+
url = f'/requested-tasks/{requested_task["_id"]}'
201+
headers = {"Content-Type": "application/json"}
202+
response = client.get(url, headers=headers)
203+
assert response.status_code == 200
204+
205+
data = json.loads(response.data)
206+
assert data["rank"] == expected_rank
207+
182208

183209
class TestRequestedTaskCreate:
184210
@pytest.fixture()

0 commit comments

Comments
 (0)