Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Sorting tasks returned by comp.tasks RPC endpoint (#4920)
Browse files Browse the repository at this point in the history
Changes the order of tasks returned by comp.tasks RPC endpoint.
Tasks are now sorted by their `time_started` field.
  • Loading branch information
kmazurek authored Nov 22, 2019
1 parent a822cf7 commit e18581b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
11 changes: 8 additions & 3 deletions golem/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time
import uuid
from copy import copy, deepcopy
from datetime import timedelta
from datetime import datetime, timedelta
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -871,7 +871,11 @@ def get_task(self, task_id: str) -> Optional[dict]:
if not task:
return None
subtask_ids = rtm.get_requested_task_subtask_ids(task_id)
task_dict = {'id': task.task_id, 'status': task.status.value}
task_dict = {
'id': task.task_id,
'status': task.status.value,
'time_started': task.start_time or datetime.now(),
}
else:
# OLD taskmanager
logger.debug('get_task(task_id=%r) - OLD', task_id)
Expand Down Expand Up @@ -935,7 +939,8 @@ def get_tasks(
if return_created_tasks_only:
filter_fn = self._filter_task_created_status

return list(filter(filter_fn, tasks))
filtered_tasks = list(filter(filter_fn, tasks))
return sorted(filtered_tasks, key=lambda task: task['time_started'])

@staticmethod
def _filter_task_created_status(task: Dict) -> bool:
Expand Down
47 changes: 39 additions & 8 deletions tests/golem/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
)

from ethereum.utils import denoms
from faker import Faker
from faker.providers import date_time as fake_date_time
from freezegun import freeze_time
from golem_messages.factories.datastructures import p2p as dt_p2p_factory
from pydispatch import dispatcher
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.internet.defer import Deferred

from golem import model
from golem import testutils
Expand All @@ -30,7 +32,8 @@
TaskCleanerService
from golem.clientconfigdescriptor import ClientConfigDescriptor
from golem.config.active import EthereumConfig
from golem.core.common import timeout_to_string
from golem.core.common import datetime_to_timestamp_utc, timeout_to_string, \
timestamp_to_datetime
from golem.core.deferred import sync_wait
from golem.core.variables import CONCENT_CHOICES
from golem.hardware.presets import HardwarePresets
Expand Down Expand Up @@ -406,15 +409,37 @@ class TestGetTasks(TestClientBase):

def setUp(self):
super().setUp()

fake = Faker()
fake.add_provider(fake_date_time)

tm_tasks = {
'task_1': {'status': TaskStatus.creating.value},
'task_2': {'status': TaskStatus.errorCreating.value},
'task_3': {'status': TaskStatus.aborted.value},
'task_1': {
'status': TaskStatus.creating.value,
'time_started': datetime_to_timestamp_utc(fake.date_time())
},
'task_2': {
'status': TaskStatus.errorCreating.value,
'time_started': datetime_to_timestamp_utc(fake.date_time())
},
'task_3': {
'status': TaskStatus.aborted.value,
'time_started': datetime_to_timestamp_utc(fake.date_time())
},
}
rtm_tasks = {
'task_4': {'status': TaskStatus.computing.value},
'task_5': {'status': TaskStatus.finished.value},
'task_6': {'status': TaskStatus.creatingDeposit.value},
'task_4': {
'status': TaskStatus.computing.value,
'time_started': datetime_to_timestamp_utc(fake.date_time())
},
'task_5': {
'status': TaskStatus.finished.value,
'time_started': datetime_to_timestamp_utc(fake.date_time())
},
'task_6': {
'status': TaskStatus.creatingDeposit.value,
'time_started': datetime_to_timestamp_utc(fake.date_time())
},
}

self.tasks = dict()
Expand All @@ -431,6 +456,12 @@ def test_get_tasks(self):
retrieved_tasks = self.client.get_tasks()
assert isinstance(retrieved_tasks, list)
assert len(retrieved_tasks) == 6
# Check that task start times are sorted in ascending order
for i in range(len(retrieved_tasks) - 1):
date = timestamp_to_datetime(retrieved_tasks[i]['time_started'])
next_date = timestamp_to_datetime(
retrieved_tasks[i + 1]['time_started'])
assert date < next_date

def test_get_single_task(self):
self.client.get_task = lambda task_id: self.tasks[task_id]
Expand Down

0 comments on commit e18581b

Please sign in to comment.