Skip to content

Commit

Permalink
Merge pull request #36 from glrs/feature/couchdb
Browse files Browse the repository at this point in the history
Feature/couchdb
  • Loading branch information
glrs authored Jan 15, 2025
2 parents e5d098a + 9f2f93e commit b4ce994
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
66 changes: 49 additions & 17 deletions lib/couchdb/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def from_dict(cls, data: Dict[str, Any]) -> "YggdrasilDocument":
)
instance.end_date = data.get("end_date", "")
instance.samples = data.get("samples", [])
instance.delivery_info = data.get("delivery_info", {})
return instance

def __init__(self, project_id: str, projects_reference: str, method: str) -> None:
Expand All @@ -59,6 +60,7 @@ def __init__(self, project_id: str, projects_reference: str, method: str) -> Non
self.start_date: str = datetime.datetime.now().isoformat()
self.end_date: str = ""
self.samples: List[Dict[str, Any]] = []
self.delivery_info: Dict[str, Any] = {}

def to_dict(self) -> Dict[str, Any]:
"""Converts the YggdrasilDocument to a dictionary.
Expand All @@ -75,6 +77,7 @@ def to_dict(self) -> Dict[str, Any]:
"start_date": self.start_date,
"end_date": self.end_date,
"samples": self.samples,
"delivery_info": self.delivery_info,
}

def add_sample(
Expand Down Expand Up @@ -122,6 +125,7 @@ def add_sample(
"start_time": start_time or "",
"end_time": end_time or "",
"flowcell_ids_processed_for": flowcell_ids_processed_for or [],
# "QC": ""
}
self.samples.append(sample)
# logging.debug(f"Added sample: {sample}")
Expand Down Expand Up @@ -183,25 +187,53 @@ def update_project_status(self, status: str) -> None:
self.end_date = ""

def check_project_completion(self) -> None:
"""Checks if all samples are completed and updates the project status.
"""
Determines the project status based on its samples.
Logic:
1) If any sample is active (e.g. 'processing', 'running', etc.),
project -> 'processing'
2) Else if every sample is finished ('completed' or 'aborted'),
project -> 'completed'
3) Else if every sample is not_yet_started ('pending', 'unsequenced', etc.),
project -> 'pending'
4) Otherwise, project -> 'partially_completed'
"""
# You may adjust these sets to match your real usage
active_statuses = {
"initialized",
"processing",
"pre_processing",
"post_processing",
"requires_manual_submission",
}
finished_statuses = {"completed", "aborted"}
not_yet_started_statuses = {"pending", "unsequenced"}

Samples with status "completed" or "aborted" are considered finished.
# Collect all sample statuses into a set for quick membership checks
sample_statuses = [sample["status"] for sample in self.samples]
unique_statuses = set(sample_statuses)

Note:
There will be cases where samples are "aborted". These samples
should be considered "completed" for the project status.
"""
# List of statuses indicating a sample is finished
finished_statuses = [
"completed",
"aborted",
] # , "processing_failed", "post_processing_failed"]
# 1) If any sample is "active" => project is 'processing'
if any(status in active_statuses for status in unique_statuses):
self.status = "processing"
self.end_date = "" # not fully completed
return

if all(sample["status"] in finished_statuses for sample in self.samples):
# 2) If ALL samples are "finished" => 'completed'
if all(status in finished_statuses for status in unique_statuses):
self.status = "completed"
self.end_date = datetime.datetime.now().isoformat()
else:
# If any sample is not completed (or aborted), set the project status to "processing"
self.status = "processing"
# Clear the end date since the project is not completed
if not self.end_date:
self.end_date = datetime.datetime.now().isoformat()
return

# 3) If ALL samples are "not_yet_started" => 'pending'
if all(status in not_yet_started_statuses for status in unique_statuses):
self.status = "pending"
self.end_date = ""
return

# 4) Otherwise => 'partially_completed'
# means no sample is actively running, but at least one is neither finished nor not_yet_started
self.status = "partially_completed"
self.end_date = ""
6 changes: 4 additions & 2 deletions lib/couchdb/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,11 @@ def add_sample(
ygg_doc = YggdrasilDocument.from_dict(document_dict)
ygg_doc.add_sample(sample_id, status)
self.save_document(ygg_doc)
logging.info(f"Updated project {project_id} with sample.")
logging.info(
f"Updated project '{project_id}' with sample '{sample_id}'"
)
else:
logging.error(f"Project {project_id} does not exist in YggdrasilDB.")
logging.error(f"Project '{project_id}' does not exist in YggdrasilDB.")
except Exception as e:
logging.error(f"Error adding sample: {e}")

Expand Down

0 comments on commit b4ce994

Please sign in to comment.