-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from rogers-obrien-rad/feature/no-ref/models
Feature/no ref/models
- Loading branch information
Showing
4 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from smartpm.client import SmartPMClient | ||
from smartpm.decorators import api_wrapper | ||
from smartpm.logging_config import logger | ||
|
||
from datetime import datetime | ||
|
||
class Models: | ||
def __init__(self, client: SmartPMClient): | ||
self.client = client | ||
|
||
@api_wrapper | ||
def get_models(self, project_id): | ||
""" | ||
Gets all models for a project: https://developers.smartpmtech.com/#operation/get-public-v1-projects-projectId-models | ||
Parameters | ||
---------- | ||
project_id : str | ||
The Project ID containing the scenario for which you would like to pull the delay table for | ||
Returns | ||
------- | ||
response : list of dict | ||
model for a schedule | ||
""" | ||
logger.debug(f"Fetching models for project_id: {project_id}") | ||
|
||
endpoint = f'v1/projects/{project_id}/models' | ||
response = self.client._get(endpoint=endpoint) | ||
|
||
return response | ||
|
||
@api_wrapper | ||
def find_baseline_model(self, project_id, find_original=True): | ||
""" | ||
Gets the baseline models from a project. | ||
Parameters | ||
---------- | ||
project_id : int | ||
The Project ID containing the scenario for which you would like to pull the delay table for. | ||
find_original : bool, default True | ||
If True, find the original baseline model. If False, find the latest baseline model. | ||
Returns | ||
------- | ||
dict | ||
The baseline model for a schedule. | ||
""" | ||
models = self.get_models(project_id=project_id) | ||
baseline_models = [model for model in models if model['projectId'] == project_id and model['modelType'] == 'BASELINE'] | ||
|
||
if not baseline_models: | ||
return None # Return None if no baseline models found | ||
|
||
if find_original: | ||
# Find the original baseline model (assuming there is only one original baseline) | ||
original_model = next((model for model in baseline_models if model['isOriginalModel']), None) | ||
|
||
return original_model | ||
else: | ||
# Find the latest baseline model without modifying the original data | ||
latest_model = max(baseline_models, key=lambda model: datetime.strptime(model['initialDataDate'], "%Y-%m-%dT%H:%M:%SZ")) | ||
|
||
return latest_model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import os | ||
import sys | ||
import json | ||
|
||
# Add the package root directory to the sys.path | ||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))) | ||
|
||
from dotenv import load_dotenv | ||
load_dotenv() # Load environment variables from .env file | ||
|
||
from smartpm.client import SmartPMClient | ||
from smartpm.endpoints.projects import Projects # import projects to get project IDs | ||
from smartpm.endpoints.models import Models | ||
|
||
API_KEY = os.getenv("API_KEY") | ||
COMPANY_ID = os.getenv("COMPANY_ID") | ||
|
||
def main(): | ||
# Setup SDK | ||
client = SmartPMClient(API_KEY, COMPANY_ID) | ||
projects_api = Projects(client) | ||
models_api = Models(client) | ||
|
||
# Get Models Data | ||
# --------------- | ||
# For all projects | ||
projects = projects_api.get_projects() | ||
''' | ||
for project in projects: | ||
project_id = project["id"] | ||
models = models_api.get_models( | ||
project_id=project_id | ||
) | ||
print(f"Models for project {project['name']}:") | ||
print(json.dumps(models, indent=4)) | ||
''' | ||
# --------------- | ||
|
||
# Get Models Data for Specific Project | ||
# ------------------------------------ | ||
# Find project by name | ||
name_to_find = "212096 - 401 FIRST STREET (College Station)" # replace with your project name | ||
project = projects_api.find_project_by_name(name=name_to_find) | ||
project_id = project["id"] | ||
|
||
print(f"Get Models data for {name_to_find}") | ||
models = models_api.get_models( | ||
project_id=project_id | ||
) | ||
print(json.dumps(models, indent=4)) | ||
# ------------------------------------ | ||
|
||
# Get Baseline Model for Specific Project | ||
# --------------------------------------- | ||
# Find project by name | ||
name_to_find = "231068 Databank DFW8 ACCEL RE-BL - CO PENDING" # replace with your project name | ||
project = projects_api.find_project_by_name(name=name_to_find) | ||
project_id = project["id"] | ||
|
||
print(f"Get Original Basline Model for {name_to_find}") | ||
model_baseline_original = models_api.find_baseline_model( | ||
project_id=project_id, | ||
find_original=True | ||
) | ||
print(json.dumps(model_baseline_original, indent=4)) | ||
|
||
print(f"Get Latest Basline Model for {name_to_find}") | ||
model_baseline_latest = models_api.find_baseline_model( | ||
project_id=project_id, | ||
find_original=False | ||
) | ||
print(json.dumps(model_baseline_latest, indent=4)) | ||
# --------------------------------------- | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters