Skip to content
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

fix: query filter in list task endpoint #178

Merged
merged 8 commits into from
Jul 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions pro_tes/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class DbDocumentConnector:
"""

def __init__(
self,
collection: Collection,
worker_id: str,
self,
collection: Collection, # type: ignore
worker_id: str,
uniqueg marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
"""Construct object instance."""
self.collection: Collection = collection
self.collection: Collection = collection # type: ignore
uniqueg marked this conversation as resolved.
Show resolved Hide resolved
self.worker_id: str = worker_id

def get_document(
self,
projection: Optional[Mapping] = None,
self,
projection: Optional[Mapping] = None,
) -> DbDocument:
"""Get document associated with task.

Expand All @@ -51,7 +51,7 @@ def get_document(
"""
if projection is None:
projection = {"_id": False}
document_unvalidated = self.collection.find_one(
document_unvalidated = self.collection.find_one( # type: ignore
uniqueg marked this conversation as resolved.
Show resolved Hide resolved
filter={"worker_id": self.worker_id},
projection=projection,
)
Expand All @@ -65,8 +65,8 @@ def get_document(
return document

def update_task_state(
self,
state: str = "UNKNOWN",
self,
state: str = "UNKNOWN",
) -> None:
"""Update task status.

Expand All @@ -80,17 +80,17 @@ def update_task_state(
TesState(state)
except Exception as exc:
raise ValueError(f"Unknown state: {state}") from exc
self.collection.find_one_and_update(
self.collection.find_one_and_update( # type: ignore
{"worker_id": self.worker_id},
{"$set": {"task.state": state}},
)
logger.info(f"[{self.worker_id}] {state}")

def upsert_fields_in_root_object(
self,
root: str,
projection: Optional[Mapping] = None,
**kwargs: object,
self,
root: str,
projection: Optional[Mapping] = None,
**kwargs: object,
) -> DbDocument:
"""Insert or update fields in(to) the same root (object) field.

Expand All @@ -106,17 +106,18 @@ def upsert_fields_in_root_object(
"""
if projection is None:
projection = {"_id": False}
document_unvalidated = self.collection.find_one_and_update(
{"worker_id": self.worker_id},
{
"$set": {
".".join([root, key]): value
for (key, value) in kwargs.items()
}
},
projection=projection,
return_document=ReturnDocument.AFTER,
)
document_unvalidated = (
self.collection.find_one_and_update( # type: ignore
{"worker_id": self.worker_id},
{
"$set": {
".".join([root, key]): value
for (key, value) in kwargs.items()
}
},
projection=projection,
return_document=ReturnDocument.AFTER,
))
uniqueg marked this conversation as resolved.
Show resolved Hide resolved
try:
document: DbDocument = DbDocument(**document_unvalidated)
except Exception as exc:
Expand Down
Loading