Skip to content

Commit

Permalink
feat: add latest current and reference job status to modelout (#59)
Browse files Browse the repository at this point in the history
* feat: add latest current and reference job status to modelOut

* fix: align with main

* fix: remove unused model definition fields into the sdk
  • Loading branch information
dtria91 authored Jul 2, 2024
1 parent 8ec4149 commit ace44a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
30 changes: 28 additions & 2 deletions app/models/model_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
from pydantic import BaseModel, ConfigDict, model_validator
from pydantic.alias_generators import to_camel

from app.db.dao.current_dataset_dao import CurrentDataset
from app.db.dao.model_dao import Model
from app.db.dao.reference_dataset_dao import ReferenceDataset
from app.models.inferred_schema_dto import SupportedTypes
from app.models.job_status import JobStatus
from app.models.utils import is_none, is_number, is_number_or_string, is_optional_float


Expand Down Expand Up @@ -42,6 +45,7 @@ class OutputType(BaseModel, validate_assignment=True):
prediction: ColumnDefinition
prediction_proba: Optional[ColumnDefinition] = None
output: List[ColumnDefinition]

model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)

def to_dict(self):
Expand Down Expand Up @@ -175,6 +179,8 @@ class ModelOut(BaseModel):
updated_at: str
latest_reference_uuid: Optional[UUID]
latest_current_uuid: Optional[UUID]
latest_reference_job_status: JobStatus
latest_current_job_status: JobStatus

model_config = ConfigDict(
populate_by_name=True, alias_generator=to_camel, protected_namespaces=()
Expand All @@ -183,9 +189,27 @@ class ModelOut(BaseModel):
@staticmethod
def from_model(
model: Model,
latest_reference_uuid: Optional[UUID] = None,
latest_current_uuid: Optional[UUID] = None,
latest_reference_dataset: Optional[ReferenceDataset] = None,
latest_current_dataset: Optional[CurrentDataset] = None,
):
latest_reference_uuid = (
latest_reference_dataset.uuid if latest_reference_dataset else None
)
latest_current_uuid = (
latest_current_dataset.uuid if latest_current_dataset else None
)

latest_reference_job_status = (
latest_reference_dataset.status
if latest_reference_dataset
else JobStatus.MISSING_REFERENCE
)
latest_current_job_status = (
latest_current_dataset.status
if latest_current_dataset
else JobStatus.MISSING_CURRENT
)

return ModelOut(
uuid=model.uuid,
name=model.name,
Expand All @@ -203,4 +227,6 @@ def from_model(
updated_at=str(model.updated_at),
latest_reference_uuid=latest_reference_uuid,
latest_current_uuid=latest_current_uuid,
latest_reference_job_status=latest_reference_job_status,
latest_current_job_status=latest_current_job_status,
)
33 changes: 14 additions & 19 deletions app/services/model_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from app.db.dao.current_dataset_dao import CurrentDatasetDAO
from app.db.dao.model_dao import ModelDAO
from app.db.dao.reference_dataset_dao import ReferenceDatasetDAO
from app.db.tables.current_dataset_table import CurrentDataset
from app.db.tables.model_table import Model
from app.db.tables.reference_dataset_table import ReferenceDataset
from app.models.exceptions import ModelInternalError, ModelNotFoundError
from app.models.model_dto import ModelIn, ModelOut
from app.models.model_order import OrderType
Expand Down Expand Up @@ -35,13 +37,13 @@ def create_model(self, model_in: ModelIn) -> ModelOut:

def get_model_by_uuid(self, model_uuid: UUID) -> Optional[ModelOut]:
model = self.check_and_get_model(model_uuid)
latest_reference_uuid, latest_current_uuid = self.get_latest_dataset_uuids(
latest_reference_dataset, latest_current_dataset = self.get_latest_datasets(
model_uuid
)
return ModelOut.from_model(
model=model,
latest_reference_uuid=latest_reference_uuid,
latest_current_uuid=latest_current_uuid,
latest_reference_dataset=latest_reference_dataset,
latest_current_dataset=latest_current_dataset,
)

def delete_model(self, model_uuid: UUID) -> Optional[ModelOut]:
Expand All @@ -55,13 +57,13 @@ def get_all_models(
models = self.model_dao.get_all()
model_out_list = []
for model in models:
latest_reference_uuid, latest_current_uuid = self.get_latest_dataset_uuids(
latest_reference_dataset, latest_current_dataset = self.get_latest_datasets(
model.uuid
)
model_out = ModelOut.from_model(
model=model,
latest_reference_uuid=latest_reference_uuid,
latest_current_uuid=latest_current_uuid,
latest_reference_dataset=latest_reference_dataset,
latest_current_dataset=latest_current_dataset,
)
model_out_list.append(model_out)
return model_out_list
Expand All @@ -78,13 +80,13 @@ def get_all_models_paginated(

_items = []
for model in models.items:
latest_reference_uuid, latest_current_uuid = self.get_latest_dataset_uuids(
latest_reference_dataset, latest_current_dataset = self.get_latest_datasets(
model.uuid
)
model_out = ModelOut.from_model(
model=model,
latest_reference_uuid=latest_reference_uuid,
latest_current_uuid=latest_current_uuid,
latest_reference_dataset=latest_reference_dataset,
latest_current_dataset=latest_current_dataset,
)
_items.append(model_out)

Expand All @@ -96,21 +98,14 @@ def check_and_get_model(self, model_uuid: UUID) -> Model:
raise ModelNotFoundError(f'Model {model_uuid} not found')
return model

def get_latest_dataset_uuids(
def get_latest_datasets(
self, model_uuid: UUID
) -> (Optional[UUID], Optional[UUID]):
) -> (Optional[ReferenceDataset], Optional[CurrentDataset]):
latest_reference_dataset = (
self.rd_dao.get_latest_reference_dataset_by_model_uuid(model_uuid)
)
latest_current_dataset = self.cd_dao.get_latest_current_dataset_by_model_uuid(
model_uuid
)

latest_reference_uuid = (
latest_reference_dataset.uuid if latest_reference_dataset else None
)
latest_current_uuid = (
latest_current_dataset.uuid if latest_current_dataset else None
)

return latest_reference_uuid, latest_current_uuid
return latest_reference_dataset, latest_current_dataset
4 changes: 2 additions & 2 deletions tests/services/model_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def test_get_model_by_uuid_ok(self):

assert res == ModelOut.from_model(
model=model,
latest_reference_uuid=reference_dataset.uuid,
latest_current_uuid=current_dataset.uuid,
latest_reference_dataset=reference_dataset,
latest_current_dataset=current_dataset,
)

def test_get_model_by_uuid_not_found(self):
Expand Down

0 comments on commit ace44a9

Please sign in to comment.