Skip to content

Commit

Permalink
Merge pull request #107 from newgene/hotfix/index-api
Browse files Browse the repository at this point in the history
Update indexes_by_name api: allow to specify env_name to restrict only fetching indexes from that env
  • Loading branch information
newgene authored Jan 29, 2024
2 parents d94d7c6 + b345241 commit a758b55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ config.py
.notes/
.env
.notes
ssh_host_key*
20 changes: 14 additions & 6 deletions biothings/hub/dataindex/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ def __init__(self, build_doc, indexer_env, index_name):
self.pinfo = ProcessInfo(self, indexer_env.get("concurrency", 10))

def setup_log(self):
log_folder = os.path.join(btconfig.LOG_FOLDER, "build", self.build_name or "", "index") if btconfig.LOG_FOLDER else None
log_folder = (
os.path.join(btconfig.LOG_FOLDER, "build", self.build_name or "", "index") if btconfig.LOG_FOLDER else None
)
log_name = f"index_{self.es_index_name}"
self.logger, self.logfile = get_logger(log_name, log_folder=log_folder, force=True)

Expand Down Expand Up @@ -804,8 +806,9 @@ async def _enhance(conf):

return self._config

def get_indexes_by_name(self, index_name=None, limit=10):
def get_indexes_by_name(self, index_name=None, env_name=None, limit=10):
"""Accept an index_name and return a list of indexes get from all elasticsearch environments
or from specific elasticsearch environment.
If index_name is blank, it will be return all indexes.
limit can be used to specify how many indexes should be return.
Expand All @@ -829,9 +832,13 @@ def get_indexes_by_name(self, index_name=None, limit=10):
index_name = "*"
limit = int(limit)

async def fetch(index_name, limit=None):
async def fetch(index_name, env_name=None, limit=None):
indexes = []
for env_name, env in self.register.items():
for _env_name, env in self.register.items():
# If env_name is set, only fetch indexes for the specific es server
if env_name and env_name != _env_name:
continue

async with AsyncElasticsearch(**env["args"]) as client:
try:
indices = await client.indices.get(index_name)
Expand All @@ -842,11 +849,12 @@ async def fetch(index_name, limit=None):
indexes.append(
{
"index_name": index_name,
"doc_type": mapping_meta["biothing_type"],
"build_version": mapping_meta["build_version"],
"count": mapping_meta["stats"]["total"],
"creation_date": index_data["settings"]["index"]["creation_date"],
"environment": {
"name": env_name,
"name": _env_name,
"host": env["args"]["hosts"],
},
}
Expand All @@ -858,7 +866,7 @@ async def fetch(index_name, limit=None):
indexes = indexes[:limit]
return indexes

job = asyncio.ensure_future(fetch(index_name, limit=limit))
job = asyncio.ensure_future(fetch(index_name, env_name=env_name, limit=limit))
job.add_done_callback(self.logger.debug)
return job

Expand Down

0 comments on commit a758b55

Please sign in to comment.