Skip to content

Commit

Permalink
feat: added percentages to api for sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
SteZamboni committed Oct 28, 2024
1 parent 0a92250 commit 3db228e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
23 changes: 23 additions & 0 deletions api/app/routes/metrics_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.models.metrics.data_quality_dto import DataQualityDTO
from app.models.metrics.drift_dto import DriftDTO
from app.models.metrics.model_quality_dto import ModelQualityDTO
from app.models.metrics.percentages_dto import PercentagesDTO
from app.models.metrics.statistics_dto import StatisticsDTO
from app.services.metrics_service import MetricsService

Expand Down Expand Up @@ -128,4 +129,26 @@ def get_current_data_quality_by_model_by_uuid(
model_uuid, current_uuid
)

@router.get(
'/{model_uuid}/current/latest/percentages',
status_code=200,
response_model=PercentagesDTO,
)
def get_latest_current_percentages_by_model_by_uuid(model_uuid: UUID):
return metrics_service.get_current_percentages_by_model_by_uuid(
model_uuid, None
)

@router.get(
'/{model_uuid}/current/{current_uuid}/percentages',
status_code=200,
response_model=PercentagesDTO,
)
def get_current_percentages_by_model_by_uuid(
model_uuid: UUID, current_uuid: UUID
):
return metrics_service.get_current_percentages_by_model_by_uuid(
model_uuid, current_uuid
)

return router
52 changes: 52 additions & 0 deletions api/app/services/metrics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from app.models.metrics.data_quality_dto import DataQualityDTO
from app.models.metrics.drift_dto import DriftDTO
from app.models.metrics.model_quality_dto import ModelQualityDTO
from app.models.metrics.percentages_dto import PercentagesDTO
from app.models.metrics.statistics_dto import StatisticsDTO
from app.models.model_dto import ModelType
from app.services.model_service import ModelService
Expand Down Expand Up @@ -104,6 +105,18 @@ def get_current_data_quality_by_model_by_uuid(
missing_status=JobStatus.MISSING_CURRENT,
)

def get_current_percentages_by_model_by_uuid(
self, model_uuid: UUID, current_uuid: Optional[UUID]
) -> PercentagesDTO:
"""Retrieve current data quality for a model by its UUID and an optional current dataset UUID."""
return self._get_percentages_by_model_uuid(
model_uuid=model_uuid,
dataset_and_metrics_getter=lambda uuid: self.check_and_get_current_dataset_and_metrics(
uuid, current_uuid
),
missing_status=JobStatus.MISSING_CURRENT,
)

def get_current_drift(
self, model_uuid: UUID, current_uuid: Optional[UUID]
) -> DriftDTO:
Expand Down Expand Up @@ -222,6 +235,22 @@ def _get_data_quality_by_model_uuid(
missing_status=missing_status,
)

def _get_percentages_by_model_uuid(
self,
model_uuid: UUID,
dataset_and_metrics_getter,
missing_status,
) -> PercentagesDTO:
"""Retrieve data quality for a model by its UUID."""
model = self.model_service.get_model_by_uuid(model_uuid)
dataset, metrics = dataset_and_metrics_getter(model_uuid)
return self._create_percentages_dto(
model_type=model.model_type,
dataset=dataset,
metrics=metrics,
missing_status=missing_status,
)

def _get_drift_by_model_uuid(
self,
model_uuid: UUID,
Expand Down Expand Up @@ -317,6 +346,29 @@ def _create_data_quality_dto(
data_quality_data=metrics.data_quality,
)

@staticmethod
def _create_percentages_dto(
model_type: ModelType,
dataset: Optional[ReferenceDataset | CurrentDataset],
metrics: Optional[ReferenceDatasetMetrics | CurrentDatasetMetrics],
missing_status,
) -> PercentagesDTO:
"""Create a PercentagesDTO from the provided dataset and metrics."""
if not dataset:
return PercentagesDTO.from_dict(
job_status=missing_status,
percentages_data=None,
)
if not metrics:
return PercentagesDTO.from_dict(
job_status=dataset.status,
percentages_data=None,
)
return PercentagesDTO.from_dict(
job_status=dataset.status,
percentages_data=metrics.percentages,
)

@staticmethod
def _create_drift_dto(
dataset: Optional[ReferenceDataset | CurrentDataset],
Expand Down
1 change: 0 additions & 1 deletion sdk/radicalbit_platform_sdk/models/dataset_percentages.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ class Percentages(BaseModel):
drift: MetricPercentage

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

0 comments on commit 3db228e

Please sign in to comment.