Skip to content

Commit

Permalink
Fix /event endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckierDodge committed Dec 4, 2023
1 parent 21d343f commit 5fd9130
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 29 deletions.
12 changes: 0 additions & 12 deletions examples/experiment_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@
"exp = Experiment(\"wei_server\", \"8000\", \"Example Program\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "753e034a-732a-4f6f-bfef-f6ef5ce8b2f6",
"metadata": {},
"outputs": [],
"source": [
"# This initializes the connection to the server and the logs\n",
"# for this run of the program.\n",
"exp.register_exp()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
1 change: 0 additions & 1 deletion wei/core/data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,3 @@ class Location(BaseModel):
"""State of the location"""
queue: List[str] = []
"""Queue of workflows to be run at this location"""

7 changes: 4 additions & 3 deletions wei/core/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Event(BaseModel):
event_name: str
event_info: Optional[Any] = None


class EventLogger:
"""Registers Events during the Experiment execution both in a logfile and on Kafka"""

Expand Down Expand Up @@ -123,7 +124,8 @@ def _return_response(self, response: requests.Response) -> Dict[Any, Any]:
response: Dict
The JSON portion of the request"""
if response.status_code != 200:
return {"http_error": response.status_code}
print(response.status_code, response.content)
return {"http_error": response.status_code, "content": response.content}

return dict(response.json())

Expand Down Expand Up @@ -151,11 +153,10 @@ def _log_event(
"event_info": event_info,
}
)
print({'event': event.model_dump_json()})
response = self._return_response(
requests.post(
url,
json={"event": event.model_dump_json()},
json=event.model_dump(mode="json"),
)
)
return response
Expand Down
25 changes: 24 additions & 1 deletion wei/experiment_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def start_run(
payload_path.expanduser().parent.mkdir(parents=True, exist_ok=True)
with open(payload_path.expanduser(), "w+") as payload_file_handle:
json.dump(payload, payload_file_handle)

with open(workflow_file, "r", encoding="utf-8") as workflow_file_handle:
with open(payload_path.expanduser(), "rb") as payload_file_handle:
params = {
Expand Down Expand Up @@ -268,3 +268,26 @@ def query_queue(self) -> Dict[Any, Any]:
return {"http_error": response.status_code}

return self._return_response(response)

def register_exp(self) -> Dict[Any, Any]:
"""Deprecated method for registering an experiment with the server
Parameters
----------
None
Returns
-------
response: Dict
The JSON portion of the response from the server"""
warn(
"""
This method is deprecated.
Experiment registration is now handled when initializing the ExperimentClient.
You can safely remove any calls to this function.
""",
DeprecationWarning,
stacklevel=2,
)
return {"exp_dir": self.experiment_path}
8 changes: 5 additions & 3 deletions wei/routers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"""

from typing import Any

from fastapi import APIRouter
from wei.core.events import EventLogger, Event

from wei.core.events import Event, EventLogger

router = APIRouter()


@router.post("/")
def log_event(event: Any) -> Any:
def log_event(event: Event) -> Any:
"""Logs a value to the log file for a given experiment"""
print(event)
EventLogger(event.experiment_id).log_event(event)
return event
8 changes: 0 additions & 8 deletions wei/routers/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@
from fastapi import APIRouter
from fastapi.responses import FileResponse

from wei.core.events import EventLogger, Event
from wei.core.experiment import Experiment

router = APIRouter()


@router.post("/{experiment_id}/log")
def log_experiment(experiment_id: str, event: Event) -> Event:
"""Logs a value to the log file for a given experiment"""
EventLogger(experiment_id).log_event(event)
return event


@router.get("/{experiment_id}/log")
async def log_return(experiment_id: str) -> str:
"""Returns the log for a given experiment"""
Expand Down
2 changes: 1 addition & 1 deletion wei/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi import FastAPI

from wei.config import Config
from wei.routers import experiments, locations, modules, runs, workcells, events
from wei.routers import events, experiments, locations, modules, runs, workcells


@asynccontextmanager # type: ignore
Expand Down

0 comments on commit 5fd9130

Please sign in to comment.