DAQ: added all p group request

This commit is contained in:
appleb_m committed 2025-11-03 16:26:06 +01:00
1 parent da821a5e3d
commit 3185df5d40
1 file changed
+41 -2
+41 -2
View File
@@ -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,