-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from roocs/intake
using intake catalog
- Loading branch information
Showing
17 changed files
with
421 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,9 @@ dependencies: | |
- prov>=2.0.0 | ||
- pydot | ||
- graphviz | ||
# catalog | ||
- intake | ||
- pandas | ||
- aiohttp | ||
# tests | ||
- pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,7 @@ networkx | |
# provenance | ||
prov>=2.0.0 | ||
pydot | ||
# catalog | ||
intake | ||
pandas | ||
aiohttp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from rook.exceptions import InvalidCollection | ||
|
||
from .intake import IntakeCatalog | ||
from .db import DBCatalog | ||
|
||
|
||
def get_catalog(project): | ||
if project == "c3s-cmip6": | ||
catalog = DBCatalog(project) | ||
else: | ||
raise InvalidCollection() | ||
return catalog | ||
|
||
|
||
__all__ = [ | ||
get_catalog, | ||
IntakeCatalog, | ||
DBCatalog, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import os | ||
from rook import CONFIG | ||
|
||
|
||
def make_list(value): | ||
if isinstance(value, list): | ||
val = value | ||
else: | ||
val = [value] | ||
return val | ||
|
||
|
||
class Catalog: | ||
def __init__(self, project): | ||
self.project = project | ||
|
||
def _query(self, collection, time=None): | ||
raise NotImplementedError | ||
|
||
def search(self, collection, time=None): | ||
cols = make_list(collection) | ||
records = self._query(cols, time) | ||
result = Result(self.project, records) | ||
return result | ||
|
||
|
||
class Result: | ||
def __init__(self, project, records): | ||
"""records are an OrderedDict of dataset ids with a list of files: | ||
{'ds_id': [files]} | ||
""" | ||
self.base_dir = CONFIG.get(f"project:{project}", {}).get("base_dir") | ||
self.base_url = CONFIG.get(f"project:{project}", {}).get("data_node_root") | ||
self.records = records | ||
|
||
@property | ||
def matches(self): | ||
"""Return number of matched records.""" | ||
return len(self.records) | ||
|
||
def __len__(self): | ||
return self.matches | ||
|
||
def _records(self, prefix): | ||
new_records = {} | ||
for ds_id, fpaths in self.records.items(): | ||
new_records[ds_id] = [os.path.join(prefix, fpath) for fpath in fpaths] | ||
return new_records | ||
|
||
def files(self): | ||
"""Return matched records with file path.""" | ||
return self._records(prefix=self.base_dir) | ||
|
||
def download_urls(self): | ||
"""Return matched records with download URL.""" | ||
return self._records(prefix=self.base_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import sqlalchemy | ||
from sqlalchemy.types import Integer, Text, String, DateTime | ||
import pandas as pd | ||
|
||
from pywps.dblog import get_session | ||
|
||
from .base import Catalog | ||
from .intake import IntakeCatalog | ||
from .util import parse_time, MIN_DATETIME, MAX_DATETIME | ||
|
||
|
||
class DBCatalog(Catalog): | ||
def __init__(self, project, url=None): | ||
super(DBCatalog, self).__init__(project) | ||
self.table_name = f"rook_catalog_{self.project}".replace("-", "_") | ||
self.intake_catalog = IntakeCatalog(project, url) | ||
|
||
def exists(self): | ||
session = get_session() | ||
engine = get_session().get_bind() | ||
try: | ||
ins = sqlalchemy.inspect(engine) | ||
exists_ = ins.dialect.has_table(engine.connect(), self.table_name) | ||
except Exception: | ||
exists_ = False | ||
finally: | ||
session.close() | ||
return exists_ | ||
|
||
def update(self): | ||
if not self.exists(): | ||
self.to_db() | ||
|
||
def to_db(self): | ||
df = self.intake_catalog.load() | ||
# workaround for NaN values when no time axis (fx datasets) | ||
sdf = df.fillna({"start_time": MIN_DATETIME, "end_time": MAX_DATETIME}) | ||
sdf = sdf.set_index("ds_id") | ||
# db connection | ||
session = get_session() | ||
try: | ||
sdf.to_sql( | ||
self.table_name, | ||
session.connection(), | ||
if_exists="replace", | ||
index=True, | ||
chunksize=500, | ||
) | ||
session.commit() | ||
finally: | ||
session.close() | ||
|
||
def _query(self, collection, time=None): | ||
""" | ||
https://stackoverflow.com/questions/8603088/sqlalchemy-in-clause | ||
""" | ||
self.update() | ||
start, end = parse_time(time) | ||
session = get_session() | ||
try: | ||
if len(collection) > 1: | ||
query_ = ( | ||
f"SELECT * FROM {self.table_name} WHERE ds_id IN {tuple(collection)} " | ||
f"and end_time>='{start}' and start_time<='{end}'" | ||
) | ||
else: | ||
query_ = ( | ||
f"SELECT * FROM {self.table_name} WHERE ds_id='{collection[0]}' " | ||
f"and end_time>='{start}' and start_time<='{end}'" | ||
) | ||
result = session.execute(query_).fetchall() | ||
except Exception: | ||
result = [] | ||
finally: | ||
session.close() | ||
records = {} | ||
for row in result: | ||
if row.ds_id not in records: | ||
records[row.ds_id] = [] | ||
records[row.ds_id].append(row.path) | ||
return records |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import intake | ||
|
||
# from intake.config import conf as intake_config | ||
|
||
from rook import CONFIG | ||
|
||
from .base import Catalog | ||
from .util import parse_time, MIN_DATETIME, MAX_DATETIME | ||
|
||
|
||
class IntakeCatalog(Catalog): | ||
def __init__(self, project, url=None): | ||
super(IntakeCatalog, self).__init__(project) | ||
self.url = url or CONFIG.get("catalog", {}).get("intake_catalog_url") | ||
self._cat = None | ||
self._store = {} | ||
# intake_config["cache_dir"] = "/tmp/inventory_cache" | ||
|
||
@property | ||
def catalog(self): | ||
if not self._cat: | ||
self._cat = intake.open_catalog(self.url) | ||
return self._cat | ||
|
||
def load(self): | ||
if self.project not in self._store: | ||
self._store[self.project] = self.catalog[self.project].read() | ||
return self._store[self.project] | ||
|
||
def _query(self, collection, time=None): | ||
df = self.load() | ||
start, end = parse_time(time) | ||
# workaround for NaN values when no time axis (fx datasets) | ||
sdf = df.fillna({"start_time": MIN_DATETIME, "end_time": MAX_DATETIME}) | ||
# search | ||
result = sdf.loc[ | ||
(sdf.ds_id.isin(collection)) | ||
& (sdf.end_time >= start) | ||
& (sdf.start_time <= end) | ||
] | ||
records = {} | ||
for _, row in result.iterrows(): | ||
if row.ds_id not in records: | ||
records[row.ds_id] = [] | ||
records[row.ds_id].append(row.path) | ||
return records |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import datetime | ||
|
||
from roocs_utils.parameter import time_parameter | ||
|
||
MIN_DATETIME = datetime.datetime(datetime.MINYEAR, 1, 1).isoformat() | ||
MAX_DATETIME = datetime.datetime(datetime.MAXYEAR, 12, 30).isoformat() | ||
|
||
|
||
def parse_time(time): | ||
# TODO: refactor code ... maybe we need this only in the catalog. | ||
if time: | ||
start, end = time_parameter.TimeParameter(time).tuple | ||
else: | ||
start = MIN_DATETIME | ||
end = MAX_DATETIME | ||
return start, end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.