Skip to content
Snippets Groups Projects
Commit 4b7855cc authored by Florian Schröder's avatar Florian Schröder
Browse files

Add logging for participant connections

A logging functionality has been added to record participant connections in the study_server.py. The logs, which include the participant's IP, environment ID, level info, and player info, are stored in the directory specified in the study_config.yaml file. Moreover, the participant's host IP is now provided when retrieving their game connection.
parent 284b7d63
No related branches found
No related tags found
1 merge request!90Resolve "Complete Cooperative Cousine Terminal Command"
Pipeline #49698 canceled
...@@ -52,3 +52,5 @@ levels: ...@@ -52,3 +52,5 @@ levels:
num_players: 1 num_players: 1
num_bots: 0 num_bots: 0
study_log_path: USER_LOG_DIR/ENV_NAME/
\ No newline at end of file
...@@ -13,6 +13,7 @@ The environment starts when all players connected. ...@@ -13,6 +13,7 @@ The environment starts when all players connected.
import argparse import argparse
import asyncio import asyncio
import json
import logging import logging
import os import os
import random import random
...@@ -26,7 +27,7 @@ from typing import Tuple, Any ...@@ -26,7 +27,7 @@ from typing import Tuple, Any
import requests import requests
import uvicorn import uvicorn
import yaml import yaml
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel from pydantic import BaseModel
from cooperative_cuisine import ROOT_DIR from cooperative_cuisine import ROOT_DIR
...@@ -294,7 +295,7 @@ class Study: ...@@ -294,7 +295,7 @@ class Study:
self.next_level() self.next_level()
def get_connection( def get_connection(
self, participant_id: str self, participant_id: str, participant_host: str
) -> Tuple[dict[str, PlayerInfo] | None, LevelInfo | None]: ) -> Tuple[dict[str, PlayerInfo] | None, LevelInfo | None]:
"""Get the assigned connections to the game server for a participant. """Get the assigned connections to the game server for a participant.
...@@ -318,6 +319,22 @@ class Study: ...@@ -318,6 +319,22 @@ class Study:
number_players=len(self.current_running_env["player_info"]), number_players=len(self.current_running_env["player_info"]),
kitchen_size=self.current_running_env["kitchen_size"], kitchen_size=self.current_running_env["kitchen_size"],
) )
log_path = expand_path(
self.study_config["study_log_path"],
env_name=self.current_running_env["env_id"],
)
os.makedirs(log_path, exist_ok=True)
with open(Path(log_path) / "study_log", "a") as log_file:
log_file.write(
json.dumps(
{
"env_id": self.current_running_env["env_id"],
"participant_ip": participant_host,
"level_info": level_info.dict(),
"player_info": player_info,
}
)
)
return player_info, level_info return player_info, level_info
else: else:
raise HTTPException( raise HTTPException(
...@@ -466,7 +483,7 @@ class StudyManager: ...@@ -466,7 +483,7 @@ class StudyManager:
raise HTTPException(status_code=409, detail="Participant not in any study.") raise HTTPException(status_code=409, detail="Participant not in any study.")
def get_participant_game_connection( def get_participant_game_connection(
self, participant_id: str self, participant_id: str, participant_host: str
) -> Tuple[dict[str, PlayerInfo], LevelInfo]: ) -> Tuple[dict[str, PlayerInfo], LevelInfo]:
"""Get the assigned connections to the game server for a participant. """Get the assigned connections to the game server for a participant.
...@@ -494,7 +511,10 @@ class StudyManager: ...@@ -494,7 +511,10 @@ class StudyManager:
if participant_id in self.participant_id_to_study_map.keys(): if participant_id in self.participant_id_to_study_map.keys():
assigned_study = self.participant_id_to_study_map[participant_id] assigned_study = self.participant_id_to_study_map[participant_id]
player_info, level_info = assigned_study.get_connection(participant_id) player_info, level_info = assigned_study.get_connection(
participant_id, participant_host
)
return player_info, level_info return player_info, level_info
else: else:
raise HTTPException(status_code=409, detail="Participant not in any study.") raise HTTPException(status_code=409, detail="Participant not in any study.")
...@@ -622,6 +642,7 @@ async def level_done(participant_id: str): ...@@ -622,6 +642,7 @@ async def level_done(participant_id: str):
@app.post("/get_game_connection/{participant_id}") @app.post("/get_game_connection/{participant_id}")
async def get_game_connection( async def get_game_connection(
participant_id: str, participant_id: str,
request: Request,
) -> dict[str, dict[str, PlayerInfo] | LevelInfo]: ) -> dict[str, dict[str, PlayerInfo] | LevelInfo]:
"""Request to get the connection to the game server of a participant. """Request to get the connection to the game server of a participant.
...@@ -633,7 +654,7 @@ async def get_game_connection( ...@@ -633,7 +654,7 @@ async def get_game_connection(
""" """
player_info, level_info = study_manager.get_participant_game_connection( player_info, level_info = study_manager.get_participant_game_connection(
participant_id participant_id, request.client.host
) )
return {"player_info": player_info, "level_info": level_info} return {"player_info": player_info, "level_info": level_info}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment