|
| 1 | +import logging |
| 2 | +import pathlib |
| 3 | +import time |
| 4 | + |
| 5 | +from .db_core import DBInterrogator |
| 6 | +from .db_api import APIInterrogator |
| 7 | +from .meta_cache import MetaCache |
| 8 | +from .extract import DBExtract |
| 9 | + |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class CachedAPIInterrogator(DBInterrogator): |
| 15 | + def __init__(self, api, cache_location): |
| 16 | + self.api = api.copy() |
| 17 | + self.ai = APIInterrogator(api) |
| 18 | + self.cache_location = ( |
| 19 | + pathlib.Path(cache_location) |
| 20 | + / api.hostname |
| 21 | + / (api.user_name or "anonymous") |
| 22 | + ) |
| 23 | + self.cache_location.mkdir(parents=True, exist_ok=True) |
| 24 | + |
| 25 | + self._cache_fleeting = { |
| 26 | + "circles": [], |
| 27 | + "collections": [], |
| 28 | + } |
| 29 | + |
| 30 | + if api.user_id: |
| 31 | + mode = "user" |
| 32 | + user_data = api.get_user_dict() |
| 33 | + else: |
| 34 | + mode = "public" |
| 35 | + user_data = None |
| 36 | + |
| 37 | + self._mc = MetaCache(self.cache_location) |
| 38 | + self._mc_timestamp_path = self.cache_location / "cache_timestamp" |
| 39 | + self._mc_timestamp_path.touch() |
| 40 | + self._mc_version_path = self.cache_location / "cache_version" |
| 41 | + self._mc_version_path.touch() |
| 42 | + |
| 43 | + super(CachedAPIInterrogator, self).__init__(mode=mode, |
| 44 | + user_data=user_data) |
| 45 | + |
| 46 | + @property |
| 47 | + def local_timestamp(self): |
| 48 | + return float(self._mc_timestamp_path.read_text().strip() or "0") |
| 49 | + |
| 50 | + @local_timestamp.setter |
| 51 | + def local_timestamp(self, timestamp): |
| 52 | + self._mc_timestamp_path.write_text(str(timestamp)) |
| 53 | + |
| 54 | + @property |
| 55 | + def local_version_score(self): |
| 56 | + return int(self._mc_version_path.read_text().strip() or "0") |
| 57 | + |
| 58 | + def close(self): |
| 59 | + self._mc.close() |
| 60 | + |
| 61 | + def get_circles(self, refresh=False): |
| 62 | + """Return the list of DCOR Circle names |
| 63 | + """ |
| 64 | + clist = self._cache_fleeting["circles"] |
| 65 | + if not clist or refresh: |
| 66 | + clist.clear() |
| 67 | + clist += self.ai.get_circles() |
| 68 | + return clist |
| 69 | + |
| 70 | + def get_collections(self, refresh=False): |
| 71 | + """Return the list of DCOR Collection names""" |
| 72 | + clist = self._cache_fleeting["collections"] |
| 73 | + if not clist or refresh: |
| 74 | + clist.clear() |
| 75 | + clist += self.ai.get_collections() |
| 76 | + return clist |
| 77 | + |
| 78 | + def get_datasets_user_following(self): |
| 79 | + """Return datasets the user is following""" |
| 80 | + # TODO: Use datasets in self._mc |
| 81 | + return self.ai.get_datasets_user_following() |
| 82 | + |
| 83 | + def get_datasets_user_owned(self): |
| 84 | + """Return datasets the user created""" |
| 85 | + # TODO: Use datasets in self._mc |
| 86 | + return self.ai.get_datasets_user_owned() |
| 87 | + |
| 88 | + def get_datasets_user_shared(self): |
| 89 | + """Return datasets shared with the user""" |
| 90 | + # TODO: Use datasets in self._mc |
| 91 | + return self.ai.get_datasets_user_shared() |
| 92 | + |
| 93 | + def get_users(self): |
| 94 | + """Return the list of DCOR users""" |
| 95 | + return self.ai.get_users() |
| 96 | + |
| 97 | + def search_dataset(self, query="", limit=100): |
| 98 | + """Search datasets via the CKAN API |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + query: str |
| 103 | + search query |
| 104 | + limit: int |
| 105 | + limit number of search results; Set to 0 to get all results |
| 106 | + """ |
| 107 | + return DBExtract(self._mc.search(query, limit)) |
| 108 | + |
| 109 | + def update(self, reset=False, progress_callback=None): |
| 110 | + """Update the local metadata cache based on the last local timestamp""" |
| 111 | + if self.remote_version_score != self.local_version_score: |
| 112 | + reset = True |
| 113 | + |
| 114 | + if reset: |
| 115 | + self.local_timestamp = 0 |
| 116 | + self._mc.destroy() |
| 117 | + self._mc = MetaCache(self.cache_location) |
| 118 | + |
| 119 | + circles = self.get_circles(refresh=True) |
| 120 | + self.get_collections(refresh=True) |
| 121 | + |
| 122 | + new_timestamp = time.time() |
| 123 | + for cc in circles: |
| 124 | + logger.info(f"Updating metadata cache for circle '{cc}'") |
| 125 | + for ds_dict in self.ai.search_dataset_via_api( |
| 126 | + circles=[cc], |
| 127 | + since_time=self.local_timestamp, |
| 128 | + ): |
| 129 | + self._mc.upsert_dataset(ds_dict) |
| 130 | + self.local_timestamp = new_timestamp |
0 commit comments