Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hacktoberfest - Added a get all results function #1840

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion covalent/_results_manager/results_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import contextlib
import os
from pathlib import Path
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Union

from furl import furl
from requests.adapters import HTTPAdapter
Expand Down Expand Up @@ -93,21 +93,21 @@
FileNotFoundError: If the result file is not found.
"""

if results_dir is None:
results_dir = os.environ.get("COVALENT_DATA_DIR") or get_config("dispatcher.results_dir")

Check warning on line 97 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L96-L97

Added lines #L96 - L97 were not covered by tests

import shutil

Check warning on line 99 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L99

Added line #L99 was not covered by tests

result_folder_path = os.path.join(results_dir, f"{dispatch_id}")

Check warning on line 101 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L101

Added line #L101 was not covered by tests

if os.path.exists(result_folder_path):
shutil.rmtree(result_folder_path, ignore_errors=True)

Check warning on line 104 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L103-L104

Added lines #L103 - L104 were not covered by tests

with contextlib.suppress(OSError):
os.rmdir(results_dir)

Check warning on line 107 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L106-L107

Added lines #L106 - L107 were not covered by tests

if remove_parent_directory:
shutil.rmtree(results_dir, ignore_errors=True)

Check warning on line 110 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L109-L110

Added lines #L109 - L110 were not covered by tests


def cancel(dispatch_id: str, task_ids: List[int] = None, dispatcher_addr: str = None) -> str:
Expand All @@ -127,7 +127,7 @@
dispatcher_addr = format_server_url()

if task_ids is None:
task_ids = []

Check warning on line 130 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L130

Added line #L130 was not covered by tests

api_client = CovalentAPIClient(dispatcher_addr)
endpoint = f"/api/v2/dispatches/{dispatch_id}/status"
Expand Down Expand Up @@ -450,6 +450,74 @@

return rm.result_object

def get_all_results(
dispatch_ids: Optional[List[str]] = None,
statuses: Optional[List[str]] = None,
started_before: Optional[str] = None,
started_after: Optional[str] = None,
completed_before: Optional[str] = None,
completed_after: Optional[str] = None,
) -> List[Dict[str, Union[str, "Result"]]]:
"""
Retrieve all results.

Parameters:
dispatch_ids: Optional[List[str]]
A list of dispatch IDs to filter results by.

statuses: Optional[List[str]]
A list of statuses to filter results by.

started_before: Optional[str]
Only return results started before this timestamp.

started_after: Optional[str]
Only return results started after this timestamp.

completed_before: Optional[str]
Only return results completed before this timestamp.

completed_after: Optional[str]
Only return results completed after this timestamp.

Returns:
List[Dict[str, Union[str, "Result"]]]: A list of dictionaries, where each dictionary contains a dispatch ID and its corresponding result.
"""
config_results_dir = ct.get_config()["dispatcher"]["results_dir"]
all_dispatch_ids = os.listdir(config_results_dir)

Check warning on line 487 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L486-L487

Added lines #L486 - L487 were not covered by tests

# Populate the docs
docs = []
for d_id in all_dispatch_ids:
if dispatch_ids and d_id not in dispatch_ids:
continue

Check warning on line 493 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L490-L493

Added lines #L490 - L493 were not covered by tests

try:
result_obj = ct.get_result(d_id)
except MissingLatticeRecordError:
continue

Check warning on line 498 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L495-L498

Added lines #L495 - L498 were not covered by tests

if not result_obj:
continue

Check warning on line 501 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L500-L501

Added lines #L500 - L501 were not covered by tests

if statuses and result_obj.status not in statuses:
continue

Check warning on line 504 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L503-L504

Added lines #L503 - L504 were not covered by tests

if started_before and result_obj.started > started_before:
continue

Check warning on line 507 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L506-L507

Added lines #L506 - L507 were not covered by tests

if started_after and result_obj.started < started_after:
continue

Check warning on line 510 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L509-L510

Added lines #L509 - L510 were not covered by tests

if completed_before and result_obj.completed > completed_before:
continue

Check warning on line 513 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L512-L513

Added lines #L512 - L513 were not covered by tests

if completed_after and result_obj.completed < completed_after:
continue

Check warning on line 516 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L515-L516

Added lines #L515 - L516 were not covered by tests

docs.append({"dispatch_id": d_id, "result": result_obj})

Check warning on line 518 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L518

Added line #L518 was not covered by tests

return docs

Check warning on line 520 in covalent/_results_manager/results_manager.py

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L520

Added line #L520 was not covered by tests

def get_result(
dispatch_id: str,
Expand Down
Loading