diff --git a/lean/commands/cloud/status.py b/lean/commands/cloud/status.py index 7dd8c340..d33d7927 100644 --- a/lean/commands/cloud/status.py +++ b/lean/commands/cloud/status.py @@ -31,7 +31,7 @@ def status(project: str) -> None: cloud_project_manager = container.cloud_project_manager cloud_project = cloud_project_manager.get_cloud_project(project, False) - live_algorithm = next((d for d in api_client.live.get_all() if d.projectId == cloud_project.projectId), None) + live_algorithm = api_client.live.get_project_by_id(cloud_project.projectId) logger.info(f"Project id: {cloud_project.projectId}") logger.info(f"Project name: {cloud_project.name}") diff --git a/lean/components/api/live_client.py b/lean/components/api/live_client.py index c3590467..f5872e6f 100644 --- a/lean/components/api/live_client.py +++ b/lean/components/api/live_client.py @@ -28,29 +28,28 @@ def __init__(self, api_client: 'APIClient') -> None: """ self._api = api_client - def get_all(self, - status: Optional[QCLiveAlgorithmStatus] = None, - # Values less than 86400 cause errors on Windows: https://bugs.python.org/issue37527 - start: datetime = datetime.fromtimestamp(86400), - end: datetime = datetime.now()) -> List[QCFullLiveAlgorithm]: + def get_project_by_id(self, + project_id: str, + status: Optional[QCLiveAlgorithmStatus] = None) -> QCFullLiveAlgorithm: """Retrieves all live algorithms. :param status: the status to filter by or None if no status filter should be applied - :param start: the earliest launch time the returned algorithms should have - :param end: the latest launch time the returned algorithms should have - :return: a list of live algorithms which match the given filters + :param project_id: the project id + :return: a live algorithm which match the given filters """ - from math import floor - parameters = { - "start": floor(start.timestamp()), - "end": floor(end.timestamp()) - } + from lean.container import container + + parameters = {"projectId": project_id} if status is not None: parameters["status"] = status.value - data = self._api.get("live/read", parameters) - return [QCFullLiveAlgorithm(**algorithm) for algorithm in data["live"]] + response = self._api.get("live/read", parameters) + + if response: + return QCFullLiveAlgorithm(data=response) + + return None def start(self, project_id: int, diff --git a/lean/components/util/live_utils.py b/lean/components/util/live_utils.py index 311b8935..3b7ad8e3 100644 --- a/lean/components/util/live_utils.py +++ b/lean/components/util/live_utils.py @@ -26,12 +26,13 @@ def _get_last_portfolio(api_client: APIClient, project_id: str, project_name: Pa from datetime import datetime from lean.container import container - cloud_deployment_list = api_client.get("live/read", { "projectId": project_id }) - container.logger.info(f'----- After cloud_deployment_list: {cloud_deployment_list}') - - if "live" not in cloud_deployment_list: + if not project_id: + # Project is not initialized in the cloud, hence project_id is None return None + cloud_deployment_list = api_client.get("live/read", {"projectId": project_id}) + container.logger.info(f'----- After cloud_deployment_list: {cloud_deployment_list}') + cloud_deployment_time = [datetime.strptime(instance["launched"], "%Y-%m-%d %H:%M:%S").astimezone(UTC) for instance in cloud_deployment_list["live"] if instance["projectId"] == project_id] cloud_last_time = sorted(cloud_deployment_time, reverse = True)[0] if cloud_deployment_time else utc.localize(datetime.min) diff --git a/lean/models/api.py b/lean/models/api.py index 9edb4ddd..9e3e02ee 100644 --- a/lean/models/api.py +++ b/lean/models/api.py @@ -279,7 +279,7 @@ class QCMinimalLiveAlgorithm(WrappedBaseModel): def get_url(self) -> str: """Returns the url of the live deployment in the cloud. - :return: a url which when visited opens an Algorithm Lab tab containing the live deployment + :return: an url which when visited opens an Algorithm Lab tab containing the live deployment """ return f"https://www.quantconnect.com/project/{self.projectId}/live"