diff --git a/stand/adb.py b/stand/adb.py index a091df2..342a1a8 100644 --- a/stand/adb.py +++ b/stand/adb.py @@ -26,9 +26,12 @@ class ArcticLibrary: return getattr(self.lib, name) - def get_data(self, *args, **kwargs): + def get_data(self, symbol, index=None): + if index is not None: + index = encode_index(index) + try: - df = self.lib.read(*args, **kwargs).data + df = self.lib.read(symbol, date_range=index).data except adb.exceptions.NoSuchVersionException: df = pd.DataFrame() @@ -38,11 +41,19 @@ class ArcticLibrary: return df - def has_data(self, *args, **kwargs): + def has_index(self, symbol, index): + index = encode_index(index) + try: - return not self.lib.read(*args, columns=[], **kwargs).data.index.empty + return not self.lib.read(symbol, date_range=index, columns=[]).data.index.empty except adb.exceptions.NoSuchVersionException: return False +def encode_index(index): + ts = pd.Timestamp(index) # adb supports update only for timeseries indexes + return pd.Index([ts], name="run") + + + diff --git a/stand/api.py b/stand/api.py index 0aa4a04..85ab2e8 100644 --- a/stand/api.py +++ b/stand/api.py @@ -22,10 +22,7 @@ def get_pgroup(beamline: Beamline, pgroup: PGroup, orient: str = None): def append(beamline: Beamline, pgroup: PGroup, run: int, row: dict[str, Any]): lib = adb.get(beamline) - index = pd.Timestamp(run) # adb supports update only for timeseries indexes - index = pd.Index([index], name="run") - - run_exists = lib.has_data(pgroup, date_range=index) + run_exists = lib.has_index(pgroup, run) if run_exists: raise HTTPException(HTTPStatus.CONFLICT, f"run {run} exists already in {beamline}/{pgroup}") @@ -33,6 +30,9 @@ def append(beamline: Beamline, pgroup: PGroup, run: int, row: dict[str, Any]): timestamp = datetime.fromisoformat(timestamp) if timestamp else datetime.now() row = {"timestamp": timestamp, **row} # setdefault would not force timestamp to be the first column + index = pd.Timestamp(run) # adb supports update only for timeseries indexes + index = pd.Index([index], name="run") + df = pd.DataFrame(row, index=index) lib.append(pgroup, df)