Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v0.1.1

- Removed f-string queries from Heasarc #13
- Refactor `SQLiteDB.fetch sync()` to clean up and abstract out some code in #12
- Included `schema/base.sql` in package data in #11
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ python >= 3.10

## 🚧 Current State

**Version**: `v0.1.0`
**Version**: `v0.1.1`
**Status**: Alpha

**Supported Archives**:
Expand All @@ -46,4 +46,6 @@ python >= 3.10

2. Run `python -m pip install .`

**Standard and Wheel dists + PyPI COMING SOON**
OR

Run `pip install astrostash`
35 changes: 30 additions & 5 deletions astrostash/astrostash.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,36 @@ def _stash_table(self, df: pd.DataFrame,
else:
self.ingest_table(df, table_name)

def _get_stashed_rows(self, catalog: str,
qid: int, idcol: str) -> pd.DataFrame:
"""
Gets the stashed rows associated with a query and response

Parameters
----------
catalog: str, name of catalog/table

qid: int, query id

idcol: str, name of column in catalog/table used for id

Returns:
pd.DataFrame, rows of a catalog associated with a query
"""
rows = pd.read_sql(
"""SELECT rowid FROM response_rowid_pivot rrp
INNER JOIN query_response_pivot qrp
ON qrp.responseid = rrp.responseid
WHERE qrp.queryid = :queryid;""",
self.conn,
params={"queryid": qid})
df = pd.read_sql_table(catalog, self.aconn)
return df[df[idcol].isin(rows["rowid"])]

def fetch_sync(self, query_func, table_name: str,
dbquery: str, query_params: dict,
refresh_rate: int | None, idcol: str = "__row",
query_params: dict,
refresh_rate: int | None,
idcol: str = "__row",
refresh: bool = False,
*args, **kwargs) -> pd.DataFrame:
"""
Expand Down Expand Up @@ -489,9 +516,7 @@ def fetch_sync(self, query_func, table_name: str,
self._ingest_response_and_links(df, qid, idcol)
# Stash the the external response in the database
self._stash_table(df, table_name, idcol)
return pd.read_sql(dbquery,
self.conn,
params={"queryid": qid})
return self._get_stashed_rows(table_name, qid, idcol)

def close(self):
"""
Expand Down
20 changes: 0 additions & 20 deletions astrostash/heasarc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,8 @@ def list_catalogs(self, *,
"""
params = locals().copy()
del params["self"]
dbquery = """SELECT name, description FROM heasarc_catalog_list
WHERE name IN (
SELECT rowid FROM response_rowid_pivot rrp
INNER JOIN query_response_pivot qrp
ON qrp.responseid = rrp.responseid
WHERE queryid = :queryid
);"""
return self.ldb.fetch_sync(self.aq.list_catalogs,
"heasarc_catalog_list",
dbquery,
params,
refresh_rate,
idcol="name",
Expand Down Expand Up @@ -98,14 +90,8 @@ def query_region(self, position=None, catalog=None,
params = locals().copy()
del params["self"]
if self._check_catalog_exists(catalog):
dbquery = f"""SELECT * FROM {catalog} WHERE __row IN (
SELECT rowid FROM response_rowid_pivot rrp
INNER JOIN query_response_pivot qrp
ON qrp.responseid = rrp.responseid
WHERE qrp.queryid = :queryid);"""
return self.ldb.fetch_sync(self.aq.query_region,
catalog,
dbquery,
params,
refresh_rate,
refresh=refresh,
Expand Down Expand Up @@ -163,15 +149,9 @@ def query_tap(self, query: str, catalog: str, maxrec=None,
params = locals().copy()
del params["self"]
if self._check_catalog_exists(catalog):
dbquery = f"""SELECT * FROM {catalog} WHERE __row IN (
SELECT rowid FROM response_rowid_pivot rrp
INNER JOIN query_response_pivot qrp
ON qrp.responseid = rrp.responseid
WHERE qrp.queryid = :queryid);"""
del params["catalog"]
return self.ldb.fetch_sync(self.aq.query_tap,
catalog,
dbquery,
params,
refresh_rate,
refresh=refresh)
7 changes: 5 additions & 2 deletions astrostash/heasarc/tests/test_heasarc.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ def test_query_object(cleanup_copies):
crab_refresh = heasarc2.query_object(
"crab", catalog="nicermastr", refresh_rate=2
)
changed_row = crab_refresh.loc[crab_refresh["__row"] == "43561"]
assert len(alias_query) == len(crab_refresh)
changed_row = crab_refresh.loc[
crab_refresh["__row"] == "43561"
].reset_index(drop=True)
assert len(changed_row) == 1
assert changed_row.at[187, "processing_status"] == "VALIDATED"
assert changed_row.at[0, "processing_status"] == "VALIDATED"
# Test pull existing data
aql_x1 = heasarc2.query_object("AQL X-1", catalog="nicermastr")
assert len(aql_x1) == 302
Expand Down