-
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.
[Explorer] New APIs to expose task graph and result version history (#…
…116)
- Loading branch information
Showing
24 changed files
with
527 additions
and
53 deletions.
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
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
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,74 @@ | ||
import enum | ||
from typing import Optional, List | ||
from pydantic import BaseModel | ||
|
||
import conductor.task_identifier as ci | ||
import conductor.task_types.base as ct | ||
from conductor.task_types.combine import Combine | ||
from conductor.task_types.group import Group | ||
from conductor.task_types.run import RunExperiment, RunCommand | ||
from conductor.execution.version_index import Version | ||
|
||
|
||
class TaskType(enum.Enum): | ||
RunExperiment = "run_experiment" | ||
RunCommand = "run_command" | ||
Group = "group" | ||
Combine = "combine" | ||
|
||
@classmethod | ||
def from_cond(cls, task_type: ct.TaskType) -> "TaskType": | ||
if isinstance(task_type, RunExperiment): | ||
return cls.RunExperiment | ||
elif isinstance(task_type, RunCommand): | ||
return cls.RunCommand | ||
elif isinstance(task_type, Group): | ||
return cls.Group | ||
elif isinstance(task_type, Combine): | ||
return cls.Combine | ||
else: | ||
raise ValueError(f"Unknown task type: {task_type}") | ||
|
||
|
||
class TaskIdentifier(BaseModel): | ||
path: str | ||
name: str | ||
|
||
@classmethod | ||
def from_cond(cls, identifier: ci.TaskIdentifier) -> "TaskIdentifier": | ||
return cls(path=str(identifier.path), name=identifier.name) | ||
|
||
@property | ||
def display(self) -> str: | ||
return f"//{self.path}/{self.name}" | ||
|
||
|
||
class ResultVersion(BaseModel): | ||
timestamp: int | ||
commit_hash: Optional[str] | ||
has_uncommitted_changes: bool | ||
|
||
@classmethod | ||
def from_version(cls, version: Version) -> "ResultVersion": | ||
return cls( | ||
timestamp=version.timestamp, | ||
commit_hash=version.commit_hash, | ||
has_uncommitted_changes=version.has_uncommitted_changes, | ||
) | ||
|
||
|
||
class TaskResults(BaseModel): | ||
identifier: TaskIdentifier | ||
versions: List[ResultVersion] | ||
|
||
|
||
class Task(BaseModel): | ||
task_type: TaskType | ||
identifier: TaskIdentifier | ||
deps: List[TaskIdentifier] | ||
|
||
|
||
class TaskGraph(BaseModel): | ||
tasks: List[Task] | ||
# These are tasks that have no dependees. | ||
root_tasks: List[TaskIdentifier] |
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 |
---|---|---|
@@ -1,29 +1,97 @@ | ||
import importlib.resources as pkg_resources | ||
from typing import Optional, List, Dict | ||
|
||
from pydantic import BaseModel | ||
from fastapi import FastAPI | ||
from fastapi import FastAPI, HTTPException | ||
from fastapi.staticfiles import StaticFiles | ||
|
||
from conductor.context import Context | ||
from conductor.errors import ConductorError | ||
from conductor.explorer.workspace import Workspace | ||
from conductor.task_identifier import TaskIdentifier | ||
import conductor.explorer as explorer_module | ||
import conductor.explorer.models as m | ||
|
||
# Conductor's explorer API requires access to a `Context` instance. This must be | ||
# set globally before the API is used. Note that this means only one API | ||
# instance can run at a time, but that is acceptable for our use case. | ||
ctx: Optional[Context] = None | ||
workspace = Workspace() | ||
app = FastAPI() | ||
|
||
|
||
class Simple(BaseModel): | ||
message: str | ||
def set_context(context: Context) -> None: | ||
""" | ||
Sets the global context for the explorer API. | ||
""" | ||
global ctx # pylint: disable=global-statement | ||
ctx = context | ||
|
||
|
||
@app.get("/api/1/hello") | ||
def hello_world() -> Simple: | ||
return Simple(message="Hello, World!") | ||
@app.get("/api/1/results/all_versions") | ||
def get_all_versions() -> List[m.TaskResults]: | ||
""" | ||
Retrieve all versioned results that Conductor manages. | ||
""" | ||
assert ctx is not None | ||
version_index = ctx.version_index.clone() | ||
all_results = version_index.get_all_versions() | ||
mapped: Dict[TaskIdentifier, List[m.ResultVersion]] = {} | ||
for task_id, version in all_results: | ||
if task_id not in mapped: | ||
mapped[task_id] = [] | ||
mapped[task_id].append(m.ResultVersion.from_version(version)) | ||
list_results = [ | ||
m.TaskResults(identifier=m.TaskIdentifier.from_cond(task_id), versions=versions) | ||
for task_id, versions in mapped.items() | ||
] | ||
list_results.sort(key=lambda r: r.identifier.display) | ||
return list_results | ||
|
||
|
||
@app.get("/api/1/task_graph") | ||
def get_task_graph() -> m.TaskGraph: | ||
""" | ||
Retrieves the entire known task graph. | ||
""" | ||
assert ctx is not None | ||
index = ctx.task_index | ||
index.load_all_known_tasks(ctx.git) | ||
try: | ||
if workspace.root_task_ids is None: | ||
root_task_ids = index.validate_all_loaded_tasks() | ||
workspace.set_root_task_ids(root_task_ids) | ||
else: | ||
root_task_ids = workspace.root_task_ids | ||
tasks = [ | ||
m.Task( | ||
task_type=m.TaskType.from_cond(task), | ||
identifier=m.TaskIdentifier.from_cond(task.identifier), | ||
deps=[m.TaskIdentifier.from_cond(dep) for dep in task.deps], | ||
) | ||
for _, task in index.get_all_loaded_tasks().items() | ||
] | ||
return m.TaskGraph( | ||
tasks=tasks, | ||
root_tasks=[ | ||
m.TaskIdentifier.from_cond(task_id) for task_id in root_task_ids | ||
], | ||
) | ||
except ConductorError as ex: | ||
raise HTTPException(status_code=400, detail=ex.printable_message()) from ex | ||
|
||
|
||
# Serve the static pages. | ||
# Note that this should go last as a "catch all" route. | ||
explorer_module_pkg = pkg_resources.files(explorer_module) | ||
with pkg_resources.as_file(explorer_module_pkg) as explorer_module_path: | ||
# Make sure the directory exists. This is needed for our test environments | ||
# where we do not build the explorer UI. The file is supposed to exist as a | ||
# symlink. | ||
static_dir = explorer_module_path / "static" | ||
if not static_dir.exists(): | ||
static_dir.mkdir(exist_ok=True) | ||
app.mount( | ||
"/", | ||
StaticFiles(directory=explorer_module_path / "static", html=True), | ||
StaticFiles(directory=static_dir, html=True), | ||
name="static", | ||
) |
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,21 @@ | ||
from typing import List, Optional | ||
from conductor.task_identifier import TaskIdentifier | ||
|
||
|
||
class Workspace: | ||
""" | ||
Used to cache useful state used by our explorer APIs. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self._root_task_ids: Optional[List[TaskIdentifier]] = None | ||
|
||
def clear(self) -> None: | ||
self._root_task_ids = None | ||
|
||
@property | ||
def root_task_ids(self) -> Optional[List[TaskIdentifier]]: | ||
return self._root_task_ids | ||
|
||
def set_root_task_ids(self, root_task_ids: List[TaskIdentifier]) -> None: | ||
self._root_task_ids = root_task_ids |
Oops, something went wrong.