Skip to content

Commit 9916616

Browse files
committed
added changes for backup and retention
1 parent 0a9c5d8 commit 9916616

File tree

2 files changed

+221
-1
lines changed

2 files changed

+221
-1
lines changed

ads/model/datascience_model.py

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
ModelCustomMetadataItem,
3737
ModelProvenanceMetadata,
3838
ModelTaxonomyMetadata,
39+
ModelBackupSetting,
40+
ModelRetentionSetting,
41+
ModelRetentionOperationDetails,
42+
ModelBackupOperationDetails
3943
)
4044
from ads.model.service.oci_datascience_model import (
4145
ModelProvenanceNotFoundError,
@@ -120,6 +124,19 @@ class DataScienceModel(Builder):
120124
Model version id
121125
model_file_description: dict
122126
Contains object path details for models created by reference.
127+
backup_setting: ModelBackupSetting
128+
The value to assign to the backup_setting property of this CreateModelDetails.
129+
retention_setting: ModelRetentionSetting
130+
The value to assign to the retention_setting property of this CreateModelDetails.
131+
retention_operation_details: ModelRetentionOperationDetails
132+
The value to assign to the retention_operation_details property for the Model.
133+
backup_operation_details: ModelBackupOperationDetails
134+
The value to assign to the backup_operation_details property for the Model.
135+
136+
137+
138+
139+
123140
124141
Methods
125142
-------
@@ -178,7 +195,6 @@ class DataScienceModel(Builder):
178195
Sets path details for models created by reference. Input can be either a dict, string or json file and
179196
the schema is dictated by model_file_description_schema.json
180197
181-
182198
Examples
183199
--------
184200
>>> ds_model = (DataScienceModel()
@@ -217,7 +233,12 @@ class DataScienceModel(Builder):
217233
CONST_MODEL_VERSION_ID = "versionId"
218234
CONST_TIME_CREATED = "timeCreated"
219235
CONST_LIFECYCLE_STATE = "lifecycleState"
236+
CONST_LIFECYCLE_DETAILS = "lifecycleDetails"
220237
CONST_MODEL_FILE_DESCRIPTION = "modelDescription"
238+
CONST_BACKUP_SETTING = "backupSetting"
239+
CONST_RETENTION_SETTING = "retentionSetting"
240+
CONST_BACKUP_OPERATION_DETAILS = "backupOperationDetails"
241+
CONST_RETENTION_OPERATION_DETAILS = "retentionOperationDetails"
221242

222243
attribute_map = {
223244
CONST_ID: "id",
@@ -239,7 +260,12 @@ class DataScienceModel(Builder):
239260
CONST_MODEL_VERSION_ID: "version_id",
240261
CONST_TIME_CREATED: "time_created",
241262
CONST_LIFECYCLE_STATE: "lifecycle_state",
263+
CONST_LIFECYCLE_DETAILS: "lifecycle_details",
242264
CONST_MODEL_FILE_DESCRIPTION: "model_description",
265+
CONST_BACKUP_SETTING: "backup_setting",
266+
CONST_RETENTION_SETTING: "retention_setting",
267+
CONST_BACKUP_OPERATION_DETAILS: "backup_operation_details",
268+
CONST_RETENTION_OPERATION_DETAILS: "retention_operation_details"
243269
}
244270

245271
def __init__(self, spec: Dict = None, **kwargs) -> None:
@@ -685,6 +711,114 @@ def with_model_file_description(
685711

686712
return self.set_spec(self.CONST_MODEL_FILE_DESCRIPTION, json_data)
687713

714+
@property
715+
def retention_setting(self) -> ModelRetentionSetting:
716+
"""
717+
Gets the retention_setting of this model.
718+
719+
:return: The retention_setting of this model.
720+
:rtype: RetentionSetting
721+
"""
722+
return self.get_spec(self.CONST_RETENTION_SETTING)
723+
724+
def with_retention_setting(self, retention_setting: Union[Dict, ModelRetentionSetting]) -> "DataScienceModel":
725+
"""
726+
Sets the retention setting details for the model.
727+
728+
Parameters
729+
----------
730+
retention_setting : Union[Dict, RetentionSetting]
731+
The retention setting details for the model. Can be provided as either a dictionary or
732+
an instance of the `RetentionSetting` class.
733+
734+
Returns
735+
-------
736+
DataScienceModel
737+
The `DataScienceModel` instance (self) for method chaining.
738+
"""
739+
if retention_setting and isinstance(retention_setting, dict):
740+
try:
741+
retention_setting = ModelRetentionSetting.from_dict(retention_setting)
742+
except Exception as err:
743+
logger.warn(f"Failed to convert retention_setting from dict: {err}")
744+
745+
return self.set_spec(self.CONST_RETENTION_SETTING, retention_setting)
746+
747+
748+
749+
@property
750+
def backup_setting(self) -> ModelBackupSetting:
751+
"""
752+
Gets the backup_setting of this model.
753+
754+
:return: The backup_setting of this model.
755+
:rtype: BackupSetting
756+
"""
757+
return self.get_spec(self.CONST_BACKUP_SETTING)
758+
759+
def with_backup_setting(self, backup_setting: Union[Dict, ModelBackupSetting]) -> "DataScienceModel":
760+
"""
761+
Sets the model's backup setting details.
762+
763+
Parameters
764+
----------
765+
backup_setting : Union[Dict, BackupSetting]
766+
The backup setting details for the model. This can be passed as either a dictionary or
767+
an instance of the `BackupSetting` class.
768+
769+
Returns
770+
-------
771+
DataScienceModel
772+
The `DataScienceModel` instance (self) for method chaining.
773+
"""
774+
if backup_setting and isinstance(backup_setting, dict):
775+
try:
776+
backup_setting = ModelBackupSetting.from_dict(backup_setting)
777+
except Exception as err:
778+
logger.warn(f"Failed to convert backup_setting from dict: {err}")
779+
780+
return self.set_spec(self.CONST_BACKUP_SETTING, backup_setting)
781+
782+
@property
783+
def retention_operation_details(self) -> ModelRetentionOperationDetails:
784+
"""
785+
Gets the retention_operation_details of this Model using the spec constant.
786+
787+
:return: The retention_operation_details of this Model.
788+
:rtype: ModelRetentionOperationDetails
789+
"""
790+
return self.get_spec(self.CONST_RETENTION_OPERATION_DETAILS)
791+
792+
@retention_operation_details.setter
793+
def retention_operation_details(self, retention_operation_details: ModelRetentionOperationDetails) -> "DataScienceModel":
794+
"""
795+
Sets the retention_operation_details of this Model using the spec constant.
796+
797+
:param retention_operation_details: The retention_operation_details of this Model.
798+
:type: ModelRetentionOperationDetails
799+
"""
800+
return self.set_spec(self.CONST_RETENTION_OPERATION_DETAILS, retention_operation_details)
801+
802+
@property
803+
def backup_operation_details(self) -> "ModelBackupOperationDetails":
804+
"""
805+
Gets the backup_operation_details of this Model using the spec constant.
806+
807+
:return: The backup_operation_details of this Model.
808+
:rtype: ModelBackupOperationDetails
809+
"""
810+
return self.get_spec(self.CONST_BACKUP_OPERATION_DETAILS)
811+
812+
@backup_operation_details.setter
813+
def backup_operation_details(self, backup_operation_details: "ModelBackupOperationDetails") -> "DataScienceModel":
814+
"""
815+
Sets the backup_operation_details of this Model using the spec constant.
816+
817+
:param backup_operation_details: The backup_operation_details of this Model.
818+
:type: ModelBackupOperationDetails
819+
"""
820+
return self.set_spec(self.CONST_BACKUP_OPERATION_DETAILS, backup_operation_details)
821+
688822
def create(self, **kwargs) -> "DataScienceModel":
689823
"""Creates datascience model.
690824
@@ -900,6 +1034,8 @@ def upload_artifact(
9001034
artifact_uploader.upload()
9011035

9021036
self._remove_file_description_artifact()
1037+
1038+
9031039

9041040
def _remove_file_description_artifact(self):
9051041
"""Removes temporary model file description artifact for model by reference."""
@@ -1181,6 +1317,8 @@ def _to_oci_dsc_model(self, **kwargs):
11811317
self.CONST_CUSTOM_METADATA: "_to_oci_metadata",
11821318
self.CONST_DEFINED_METADATA: "_to_oci_metadata",
11831319
self.CONST_PROVENANCE_METADATA: "_to_oci_metadata",
1320+
self.CONST_BACKUP_SETTING: "to_json",
1321+
self.CONST_RETENTION_SETTING: "to_json"
11841322
}
11851323
dsc_spec = {}
11861324
for infra_attr, dsc_attr in self.attribute_map.items():
@@ -1219,6 +1357,8 @@ def _update_from_oci_dsc_model(
12191357
self.CONST_OUTPUT_SCHEMA: [Schema.from_json, json.loads],
12201358
self.CONST_CUSTOM_METADATA: ModelCustomMetadata._from_oci_metadata,
12211359
self.CONST_DEFINED_METADATA: ModelTaxonomyMetadata._from_oci_metadata,
1360+
self.CONST_BACKUP_SETTING: ModelBackupSetting.from_json,
1361+
self.CONST_RETENTION_SETTING: ModelRetentionSetting.from_json,
12221362
}
12231363

12241364
# Update the main properties

ads/model/model_metadata.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,3 +1761,83 @@ def __repr__(self):
17611761
Serialized version of object as a YAML string
17621762
"""
17631763
return self.to_yaml()
1764+
1765+
1766+
class CustomerNotificationType(str, metaclass=ExtendedEnumMeta):
1767+
NONE = "NONE"
1768+
ALL = "ALL"
1769+
ON_FAILURE = "ON_FAILURE"
1770+
ON_SUCCESS = "ON_SUCCESS"
1771+
1772+
1773+
class ModelBackupSettingDetailsMetadata:
1774+
"""
1775+
Class that represents Model Backup Setting Details Metadata.
1776+
1777+
Methods
1778+
-------
1779+
to_dict(self) -> Dict:
1780+
Serializes the backup settings into a dictionary.
1781+
from_dict(cls, data: Dict) -> 'ModelBackupSettingDetailsMetadata':
1782+
Constructs backup settings from a dictionary.
1783+
to_json(self) -> str:
1784+
Serializes the backup settings into a JSON string.
1785+
from_json(cls, json_str: str) -> 'ModelBackupSettingDetailsMetadata':
1786+
Constructs backup settings from a JSON string.
1787+
to_yaml(self) -> str:
1788+
Serializes the backup settings into a YAML string.
1789+
validate(self) -> bool:
1790+
Validates the backup settings details.
1791+
"""
1792+
1793+
def __init__(self, is_backup_enabled: Optional[bool] = None, backup_region: Optional[str] = None,
1794+
customer_notification_type: Optional[CustomerNotificationType] = None):
1795+
self.is_backup_enabled = is_backup_enabled if is_backup_enabled is not None else False
1796+
self.backup_region = backup_region
1797+
self.customer_notification_type = customer_notification_type if customer_notification_type is not None else CustomerNotificationType.NONE
1798+
1799+
def to_dict(self) -> Dict:
1800+
"""Serializes the backup settings into a dictionary."""
1801+
return {
1802+
"is_backup_enabled": self.is_backup_enabled,
1803+
"backup_region": self.backup_region,
1804+
"customer_notification_type": self.customer_notification_type.value
1805+
}
1806+
1807+
@classmethod
1808+
def from_dict(cls, data: Dict) -> 'ModelBackupSettingDetailsMetadata':
1809+
"""Constructs backup settings from a dictionary."""
1810+
return cls(
1811+
is_backup_enabled=data.get("is_backup_enabled"),
1812+
backup_region=data.get("backup_region"),
1813+
customer_notification_type=CustomerNotificationType(data.get("customer_notification_type", CustomerNotificationType.NONE.value))
1814+
)
1815+
1816+
def to_json(self) -> str:
1817+
"""Serializes the backup settings into a JSON string."""
1818+
return json.dumps(self.to_dict())
1819+
1820+
@classmethod
1821+
def from_json(cls, json_str: str) -> 'ModelBackupSettingDetailsMetadata':
1822+
"""Constructs backup settings from a JSON string."""
1823+
data = json.loads(json_str)
1824+
return cls.from_dict(data)
1825+
1826+
def to_yaml(self) -> str:
1827+
"""Serializes the backup settings into a YAML string."""
1828+
return yaml.dump(self.to_dict())
1829+
1830+
def validate(self) -> bool:
1831+
"""Validates the backup settings details. Returns True if valid, False otherwise."""
1832+
if not isinstance(self.is_backup_enabled, bool):
1833+
return False
1834+
if self.backup_region and not isinstance(self.backup_region, str):
1835+
return False
1836+
if not isinstance(self.customer_notification_type, CustomerNotificationType):
1837+
return False
1838+
return True
1839+
1840+
def __repr__(self):
1841+
return f"ModelBackupSettingDetailsMetadata(is_backup_enabled={self.is_backup_enabled}, backup_region={self.backup_region}, customer_notification_type={self.customer_notification_type})"
1842+
1843+

0 commit comments

Comments
 (0)