Skip to content

Commit

Permalink
refactor(#1044): include last updated field for sort (#1093)
Browse files Browse the repository at this point in the history
* refactor(API): sort by last_updated field

* test: add tests

* test: fix tests

(cherry picked from commit 60dca5e)
  • Loading branch information
Francisco Aranda committed Feb 7, 2022
1 parent 780d54a commit 1da7d5a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/rubrix/server/tasks/commons/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EsRecordDataFieldNames(str, Enum):
score = "score"
words = "words"
event_timestamp = "event_timestamp"
last_updated = "last_updated"

def __str__(self):
return self.value
Expand Down
3 changes: 1 addition & 2 deletions src/rubrix/server/tasks/text2text/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ def search(
sort_by,
valid_fields=[
"metadata",
EsRecordDataFieldNames.score,
EsRecordDataFieldNames.predicted,
EsRecordDataFieldNames.predicted_as,
EsRecordDataFieldNames.predicted_by,
EsRecordDataFieldNames.annotated_as,
EsRecordDataFieldNames.annotated_by,
EsRecordDataFieldNames.status,
EsRecordDataFieldNames.last_updated,
EsRecordDataFieldNames.event_timestamp,
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def search(
sort_by,
valid_fields=[
"metadata",
EsRecordDataFieldNames.last_updated,
EsRecordDataFieldNames.score,
EsRecordDataFieldNames.predicted,
EsRecordDataFieldNames.predicted_as,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def search(
sort_by,
valid_fields=[
"metadata",
EsRecordDataFieldNames.last_updated,
EsRecordDataFieldNames.score,
EsRecordDataFieldNames.predicted,
EsRecordDataFieldNames.predicted_as,
Expand Down
34 changes: 31 additions & 3 deletions tests/server/text_classification/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,33 @@ def test_partial_record_update():
)


def test_sort_by_last_updated():
dataset = "test_sort_by_last_updated"
assert client.delete(f"/api/datasets/{dataset}").status_code == 200
for i in range(0, 10):
client.post(
f"/api/datasets/{dataset}/TextClassification:bulk",
json=TextClassificationBulkData(
records=[
TextClassificationRecord(
**{
"id": i,
"inputs": {"data": "my data"},
"metadata": {"s": "value"},
}
)
],
).dict(by_alias=True),
)

response = client.post(
f"/api/datasets/{dataset}/TextClassification:search?from=0&limit=10",
json={"sort": [{"id": "last_updated", "order": "asc"}]},
)

assert [r["id"] for r in response.json()["records"]] == list(range(0, 10))


def test_sort_by_id_as_default():
dataset = "test_sort_by_id_as_default"
assert client.delete(f"/api/datasets/{dataset}").status_code == 200
Expand Down Expand Up @@ -341,9 +368,10 @@ def test_some_sort_by():
"code": "rubrix.api.errors::BadRequestError",
"params": {
"message": "Wrong sort id wrong_field. Valid values "
"are: ['metadata', 'score', 'predicted', "
"'predicted_as', 'predicted_by', "
"'annotated_as', 'annotated_by', 'status', "
"are: ['metadata', 'last_updated', 'score', "
"'predicted', 'predicted_as', "
"'predicted_by', 'annotated_as', "
"'annotated_by', 'status', "
"'event_timestamp']"
},
}
Expand Down
7 changes: 4 additions & 3 deletions tests/server/token_classification/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ def test_some_sort():
"code": "rubrix.api.errors::BadRequestError",
"params": {
"message": "Wrong sort id babba. Valid values are: "
"['metadata', 'score', 'predicted', "
"'predicted_as', 'predicted_by', "
"'annotated_as', 'annotated_by', 'status', "
"['metadata', 'last_updated', 'score', "
"'predicted', 'predicted_as', "
"'predicted_by', 'annotated_as', "
"'annotated_by', 'status', "
"'event_timestamp']"
},
}
Expand Down

0 comments on commit 1da7d5a

Please sign in to comment.