changed has_data to has_index; encode incoming integer index to timestamp
This commit is contained in:
+15
-4
@@ -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")
|
||||
|
||||
|
||||
|
||||
|
||||
+4
-4
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user