|
23 | 23 | from murfey.server.murfey_db import murfey_db |
24 | 24 | from murfey.util import secure_path |
25 | 25 | from murfey.util.config import get_machine_config |
26 | | -from murfey.util.db import Session, SessionProcessingParameters |
| 26 | +from murfey.util.db import RsyncInstance, Session, SessionProcessingParameters |
27 | 27 | from murfey.util.models import File, MultigridWatcherSetup |
28 | 28 |
|
29 | 29 | # Create APIRouter class object |
@@ -407,3 +407,67 @@ async def restart_rsyncer( |
407 | 407 | ) as resp: |
408 | 408 | data = await resp.json() |
409 | 409 | return data |
| 410 | + |
| 411 | + |
| 412 | +class RSyncerInfo(BaseModel): |
| 413 | + source: str |
| 414 | + num_files_transferred: int |
| 415 | + num_files_in_queue: int |
| 416 | + alive: bool |
| 417 | + stopping: bool |
| 418 | + destination: str |
| 419 | + tag: str |
| 420 | + files_transferred: int |
| 421 | + files_counted: int |
| 422 | + transferring: bool |
| 423 | + session_id: int |
| 424 | + |
| 425 | + |
| 426 | +@router.get("/instruments/{instrument_name}/sessions/{session_id}/rsyncer_info") |
| 427 | +async def get_rsyncer_info( |
| 428 | + instrument_name: str, session_id: MurfeySessionID, db=murfey_db |
| 429 | +) -> List[RSyncerInfo]: |
| 430 | + data = [] |
| 431 | + machine_config = get_machine_config(instrument_name=instrument_name)[ |
| 432 | + instrument_name |
| 433 | + ] |
| 434 | + rsync_instances = db.exec( |
| 435 | + select(RsyncInstance).where(RsyncInstance.session_id == session_id) |
| 436 | + ).all() |
| 437 | + if machine_config.instrument_server_url: |
| 438 | + try: |
| 439 | + async with lock: |
| 440 | + token = instrument_server_tokens[session_id]["access_token"] |
| 441 | + async with aiohttp.ClientSession() as clientsession: |
| 442 | + async with clientsession.get( |
| 443 | + f"{machine_config.instrument_server_url}/sessions/{session_id}/rsyncer_info", |
| 444 | + headers={"Authorization": f"Bearer {token}"}, |
| 445 | + ) as resp: |
| 446 | + data = await resp.json() |
| 447 | + except KeyError: |
| 448 | + data = [] |
| 449 | + except Exception: |
| 450 | + log.warning( |
| 451 | + "Exception encountered gathering rsyncer info from the instrument server", |
| 452 | + exc_info=True, |
| 453 | + ) |
| 454 | + combined_data = [] |
| 455 | + data_source_lookup = {d["source"]: d for d in data} |
| 456 | + for ri in rsync_instances: |
| 457 | + d = data_source_lookup.get(ri.source, {}) |
| 458 | + combined_data.append( |
| 459 | + RSyncerInfo( |
| 460 | + source=ri.source, |
| 461 | + num_files_transferred=d.get("num_files_transferred", 0), |
| 462 | + num_files_in_queue=d.get("num_files_in_queue", 0), |
| 463 | + alive=d.get("alive", False), |
| 464 | + stopping=d.get("stopping", True), |
| 465 | + destination=ri.destination, |
| 466 | + tag=ri.tag, |
| 467 | + files_transferred=ri.files_transferred, |
| 468 | + files_counted=ri.files_counted, |
| 469 | + transferring=ri.transferring, |
| 470 | + session_id=session_id, |
| 471 | + ) |
| 472 | + ) |
| 473 | + return combined_data |
0 commit comments