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

Adding a ct.get_all_results() implementation #1901

Open
wants to merge 6 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
109 changes: 109 additions & 0 deletions covalent/_results_manager/results_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import contextlib
import os
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional

Expand Down Expand Up @@ -516,3 +517,111 @@
app_log.error(re)
num_attempts += 1
raise RuntimeError("Timed out waiting for result. Please retry or check dispatch.")

def get_all_results(dispatch_id_lst: list[str] = [],
wait: bool = False,
dispatcher_addr: str = None,
status_only: bool = False,
*,
results_dir: Optional[str] = None,
workflow_output: bool = True,
intermediate_outputs: bool = True,
sublattice_results: bool = True,
qelectron_db: bool = False,
completed_check : Optional[bool] = False,
started_before : Optional[datetime] = None,
started_after : Optional[datetime] = None,
completed_before : Optional[datetime] = None,
completed_after: Optional[datetime] = None
) -> list(Result):
"""
Get all the results from a list of dispatch ids.

Args:
dispatch_id_lst: A list of dispatch ids.
wait: Controls how long the method waits for the server to return a
result. If False, the method will not wait and will return the
current status of the workflow. If True, the method will wait for
the result to finish and keep retrying for sys.maxsize.
dispatcher_addr: Dispatcher server address. Defaults to the address set
in Covalent's config.
status_only: If true, only returns result status, not the full result
object. Default is False.

Kwargs:
results_dir: The directory where the results are stored in dispatch
id named folders.
workflow_output: Whether to return the workflow output.
Defaults to True.
intermediate_outputs: Whether to return all intermediate outputs in the
compute graph. Defaults to True.
sublattice_results: Whether to recursively retrieve sublattice results.
Default is True.
qelectron_db: Whether to load the bytes data of qelectron_db. Default
is False.
completed_check : Whether to only include completed results.
started_before: Only return results that started before this time.
started_after: Only return results that started after this time.
completed_before: Only return results that completed before this time.
If the result as not been completed, it will not appear in the list.
completed_after: Only return results that completed after this time.
If the result as not been completed, it will not appear in the list.

Returns:
A list of Result objects from the Covalent server that meet the query.
"""

# This implementation depends on the results being stored in the same
# machine. May need a better implemenation to fix. As a work around, the
# user can specify the dispatch_id_lst, otherwise it will default to the
# machine specific implementation.
if len(dispatch_id_lst) == 0:
try:
dispatch_dir = get_config()[dispatcher_addr][results_dir]
dispatch_id_lst = os.listdir(dispatch_dir)
except:
raise ValueError("No dispatch ids specified and no default directory found.")

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L578-L583

Added lines #L578 - L583 were not covered by tests

# Initialize the list of results
result_lst = []

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L586

Added line #L586 was not covered by tests

for _id in dispatch_id_lst:
try:

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L588-L589

Added lines #L588 - L589 were not covered by tests
# Depends on get_result method, works as normal
result = get_result(_id, wait, dispatcher_addr, status_only,

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L591

Added line #L591 was not covered by tests
results_dir, workflow_output,
intermediate_outputs, sublattice_results,
qelectron_db)

# Does not follow PEP8 for better readability of conditional logic.
# The logic could be simplified. This is it currently as follows:
# If the result does not meet the query, then exclude it from the
# list. started_before and started_after are datetime objects that
# determine the time range for the start time. completed_before and
# completed_after are datetime objects that determine the time range
# for the end time. If the result does not meet the query, then
# it is excluded from the list.

# Check if result has been completed to avoid errors (if not
# completed, there is no end time)
if result.status != "COMPLETED":
if (not (

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L607-L608

Added lines #L607 - L608 were not covered by tests
(started_before is not None and result.start_time > started_before) or \
(started_after is not None and result.start_time < started_after)
)) and (not completed_check):
result_lst.append({"dispatch_id": _id, "result": result})

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L612

Added line #L612 was not covered by tests
else:
if not ((started_before is not None and result.start_time > started_before) or \

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L614

Added line #L614 was not covered by tests
(started_after is not None and result.start_time < started_after) or \
(completed_before is not None and result.end_time > completed_before) or \
(completed_after is not None and result.end_time < completed_after)):

result_lst.append({"dispatch_id": _id, "result": result})

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L619

Added line #L619 was not covered by tests


# If the record does not exist, a MissingLatticeRecordError is raised,
# which is caught and ignored
except MissingLatticeRecordError:
continue

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L624-L625

Added lines #L624 - L625 were not covered by tests

return result_lst

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

View check run for this annotation

Codecov / codecov/patch

covalent/_results_manager/results_manager.py#L627

Added line #L627 was not covered by tests
Loading