From 3185df5d404834a785187759f19109022aa4bd78 Mon Sep 17 00:00:00 2001 From: appleb_m Date: Mon, 3 Nov 2025 16:26:06 +0100 Subject: [PATCH] DAQ: added all p group request --- daq/src/aaredaq/server.py | 43 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/daq/src/aaredaq/server.py b/daq/src/aaredaq/server.py index 907142e7..b142dc2e 100644 --- a/daq/src/aaredaq/server.py +++ b/daq/src/aaredaq/server.py @@ -1,5 +1,6 @@ import asyncio import io +import os, time from typing import Tuple, AsyncGenerator import cv2 @@ -36,6 +37,9 @@ bl = mx_beamline() cfg = BeamlineConfig(bl) daq = AareDAQ(cfg, bl) +_all_pgroups_cache: dict[str, tuple[list[str], float]] = {} +_ALL_PGROUPS_TTL_S = 60.0 # adjust TTL as needed + logger = setup_logger("aareDAQ") @app.post("/token") @@ -444,14 +448,14 @@ async def pgroup(token: str = Depends(oauth2_scheme)) -> str: @app.put("/access/pgroup") async def set_pgroup(val: str, token: str = Depends(oauth2_scheme)) -> str: - auth.check_jwt_staff(cfg, auth.parse_token(token)) + auth.check_jwt_ro(cfg, auth.parse_token(token)) cfg.pgroup = val return "OK" @app.delete("/access/pgroup") async def del_pgroup(token: str = Depends(oauth2_scheme)) -> str: - auth.check_jwt_staff(cfg, auth.parse_token(token)) + auth.check_jwt_ro(cfg, auth.parse_token(token)) cfg.pgroup = None return "OK" @@ -507,6 +511,41 @@ async def put_cryo_settings(s: CryojetSettingsModel, token: str = Depends(oauth2 auth.check_jwt_staff(cfg, auth.parse_token(token)) cfg.cryojet_settings = s +@app.put("/access/all_pgroups") +async def get_all_pgroups(token: str = Depends(oauth2_scheme)): + logger.debug(f"Get all pgroups") + auth.check_jwt_staff(cfg, auth.parse_token(token)) + + base_path = "/sls/mx/data/" + + now=time.monotonic() + cached = _all_pgroups_cache.get(base_path) + if cached is not None: + items, ts = cached + if (now - ts) < _ALL_PGROUPS_TTL_S: + return items + + # Refresh: fast scan using scandir, no regex + try: + names: list[str] = [] + with os.scandir(base_path) as it: + for entry in it: + name = entry.name + # quick name filter: p + 5 digits, and directory + if ( + len(name) == 6 and + name[0] == "p" and + name[1:].isdigit() and + entry.is_dir(follow_symlinks=False) + ): + names.append(name) + names.sort(key=lambda d: int(d[1:])) + _all_pgroups_cache[base_path] = (names, now) + return names + except Exception as e: + logger.error(f"Failed to list pgroups: {e}") + return [] + LOGGING_CONFIG = { "version": 1, "disable_existing_loggers": False,