Skip to content

Commit

Permalink
Add TimeseriesNotFound for fqme lookup failures
Browse files Browse the repository at this point in the history
A common usage error is to run `piker anal mnq.cme.ib` where the CLI
passed fqme is not actually fully-qualified (in this case missing an
expiry token) and we get an underlying `FileNotFoundError` from the
`StorageClient.read_ohlcv()` call. In such key misses, scan the existing
`StorageClient._index` for possible matches and report in a `raise from`
the new error.

CHERRY into #486
  • Loading branch information
goodboy committed Dec 4, 2023
1 parent ebd1eb1 commit 24a54a7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
9 changes: 8 additions & 1 deletion piker/storage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,21 @@ def anal(
period: int = 60,

) -> np.ndarray:
'''
Anal-ysis is when you take the data do stuff to it, i think.
'''
async def main():
async with (
open_piker_runtime(
# are you a bear or boi?
'tsdb_polars_anal',
debug_mode=True,
),
open_storage_client() as (mod, client),
open_storage_client() as (
mod,
client,
),
):
syms: list[str] = await client.list_keys()
print(f'{len(syms)} FOUND for {mod.name}')
Expand Down
27 changes: 24 additions & 3 deletions piker/storage/nativedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
log = get_logger('storage.nativedb')


class TimeseriesNotFound(Exception):
'''
No timeseries entry can be found for this backend.
'''

# NOTE: thanks to this SO answer for the below conversion routines
# to go from numpy struct-arrays to polars dataframes and back:
# https://stackoverflow.com/a/72054819
Expand Down Expand Up @@ -228,8 +234,21 @@ async def load(
fqme,
timeframe,
)
except FileNotFoundError:
return None
except FileNotFoundError as fnfe:

bs_fqme, _, *_ = fqme.rpartition('.')

possible_matches: list[str] = []
for tskey in self._index:
if bs_fqme in tskey:
possible_matches.append(tskey)

match_str: str = '\n'.join(possible_matches)
raise TimeseriesNotFound(
f'No entry for `{fqme}`?\n'
f'Maybe you need a more specific fqme-key like:\n\n'
f'{match_str}'
) from fnfe

times = array['time']
return (
Expand Down Expand Up @@ -376,6 +395,8 @@ async def delete_ts(
# ...


# TODO: does this need to be async on average?
# I guess for any IPC connected backend yes?
@acm
async def get_client(

Expand All @@ -393,7 +414,7 @@ async def get_client(
'''
datadir: Path = config.get_conf_dir() / 'nativedb'
if not datadir.is_dir():
log.info(f'Creating `nativedb` director: {datadir}')
log.info(f'Creating `nativedb` dir: {datadir}')
datadir.mkdir()

client = NativeStorageClient(datadir)
Expand Down

0 comments on commit 24a54a7

Please sign in to comment.