Skip to content

Commit 0090e47

Browse files
model versioning files
1 parent e093418 commit 0090e47

File tree

2 files changed

+421
-6
lines changed

2 files changed

+421
-6
lines changed

src/sasctl/_services/model_repository.py

Lines changed: 128 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
import datetime
1010
from warnings import warn
11+
import requests
12+
from requests.exceptions import HTTPError
1113

12-
from ..core import HTTPError, current_session, delete, get, sasctl_command
14+
from ..core import current_session, delete, get, sasctl_command
1315
from .service import Service
1416

1517
FUNCTIONS = {
@@ -612,14 +614,134 @@ def list_model_versions(cls, model):
612614
613615
Returns
614616
-------
615-
list
617+
RestObj
616618
617619
"""
618-
model = cls.get_model(model)
619-
if cls.get_model_link(model, "modelVersions") is None:
620-
raise ValueError("Unable to retrieve versions for model '%s'" % model)
621620

622-
return cls.request_link(model, "modelVersions")
621+
link = cls.get_model_link(model, "modelHistory")
622+
if link is None:
623+
raise ValueError(
624+
"Cannot find link for version history for model '%s'" % model
625+
)
626+
627+
modelHistory = cls.request_link(
628+
link,
629+
"modelHistory",
630+
headers={"Accept": "application/vnd.sas.models.model.version"},
631+
)
632+
if modelHistory is None:
633+
return {}
634+
635+
return modelHistory
636+
637+
@classmethod
638+
def get_model_version(cls, model, version_id):
639+
640+
model_history = cls.list_model_versions(model)
641+
model_history_items = model_history.get("items")
642+
643+
for i, item in enumerate(model_history_items):
644+
if item.get("id") == version_id:
645+
return cls.request_link(
646+
item,
647+
"self",
648+
headers={"Accept": "application/vnd.sas.models.model.version"},
649+
)
650+
651+
raise ValueError("The version id specified could not be found.")
652+
653+
@classmethod
654+
def get_model_with_versions(cls, model):
655+
if cls.is_uuid(model):
656+
model_id = model
657+
elif isinstance(model, dict) and "id" in model:
658+
model_id = model["id"]
659+
else:
660+
model = cls.get_model(model)
661+
if not model:
662+
raise HTTPError(
663+
"This model may not exist in a project or the model may not exist at all."
664+
)
665+
model_id = model["id"]
666+
667+
versions_uri = f"/modelRepository/models/{model_id}/versions"
668+
version_history = cls.get(
669+
versions_uri,
670+
headers={"Accept": "application/vnd.sas.models.model.version"},
671+
)
672+
if version_history is None:
673+
return {}
674+
return version_history
675+
676+
@classmethod
677+
def get_model_or_version(cls, model, version_id):
678+
679+
if cls.is_uuid(model):
680+
model_id = model
681+
elif isinstance(model, dict) and "id" in model:
682+
model_id = model["id"]
683+
else:
684+
model = cls.get_model(model)
685+
if not model:
686+
raise HTTPError(
687+
"This model may not exist in a project or the model may not exist at all."
688+
)
689+
model_id = model["id"]
690+
691+
if model_id == version_id:
692+
return cls.get_model(model)
693+
694+
version_history = cls.get_model_with_versions(model)
695+
model_versions = version_history.get("modelVersions")
696+
for i, item in enumerate(model_versions):
697+
if item.get("id") == version_id:
698+
return cls.request_link(
699+
item,
700+
"self",
701+
headers={"Accept": "application/vnd.sas.models.model.version"},
702+
)
703+
704+
raise ValueError("The version id specified could not be found.")
705+
706+
@classmethod
707+
def get_model_version_contents(cls, model, version_id):
708+
model_version = cls.get_model_version(model, version_id)
709+
version_contents = cls.request_link(
710+
model_version,
711+
"contents",
712+
headers={"Accept": "application/vnd.sas.models.model.content"},
713+
)
714+
715+
if version_contents is None:
716+
return {}
717+
return version_contents
718+
719+
@classmethod
720+
def get_model_version_content_metadata(cls, model, version_id, content_id):
721+
model_version_contents = cls.get_model_version_contents(model, version_id)
722+
723+
model_version_contents_items = model_version_contents.get("items")
724+
for i, item in enumerate(model_version_contents_items):
725+
if item.get("id") == content_id:
726+
return cls.request_link(
727+
item,
728+
"self",
729+
headers={"Accept": "application/vnd.sas.models.model.content"},
730+
)
731+
732+
raise ValueError("The content id specified could not be found.")
733+
734+
@classmethod
735+
def get_model_version_content(cls, model, version_id, content_id):
736+
737+
metadata = cls.get_model_version_content_metadata(model, version_id, content_id)
738+
version_content_file = cls.request_link(
739+
metadata, "content", headers={"Accept": "text/plain"}
740+
)
741+
742+
if version_content_file is None:
743+
raise HTTPError("Something went wrong while accessing the metadata file.")
744+
return version_content_file
623745

624746
@classmethod
625747
def copy_analytic_store(cls, model):

0 commit comments

Comments
 (0)