feat(#237): implement app_status_advanced endpoint (#238)

Some apps might give status information that is computationally to
expensive to include in the normal status endpoint which can be polled
more often.

closes #237
This commit is contained in:
fusion44 2024-02-18 12:10:35 +01:00 committed by GitHub
parent f22074c0b2
commit 00e56ba155
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 99 additions and 1 deletions

View file

@ -10,6 +10,10 @@ class AppsBase:
async def get_app_status(self):
raise NotImplementedError()
@abstractmethod
async def get_app_status_advanced(self, app_id: str):
raise NotImplementedError()
@abstractmethod
async def get_app_status_sub(self):
raise NotImplementedError()

View file

@ -18,6 +18,9 @@ class NativePythonApps(AppsBase):
async def get_app_status(self):
raise _NotImplemented()
async def get_app_status_advanced(self, app_id: str):
raise _NotImplemented()
async def get_app_status_sub(self):
raise _NotImplemented()

View file

@ -25,6 +25,7 @@ available_app_ids = {
"mempool",
"thunderhub",
"jam",
"electrs",
}
@ -97,6 +98,7 @@ class RaspiBlitzApps(AppsBase):
"isIndexed": data["isIndexed"],
"indexInfo": data["indexInfo"],
}
return {
"id": app_id,
"version": version,
@ -125,6 +127,18 @@ class RaspiBlitzApps(AppsBase):
"error": f"script result processing error: {script_call}",
}
async def get_app_status_advanced(self, app_id):
if app_id not in available_app_ids:
raise HTTPException(
status.HTTP_400_BAD_REQUEST,
detail=f"App id invalid. Available app ids: {available_app_ids}",
)
if app_id == "electrs":
return await _do_electrs_status_advanced()
return {}
async def get_app_status(self):
appStatusList: List = []
for appID in available_app_ids:
@ -346,3 +360,59 @@ class RaspiBlitzApps(AppsBase):
logging.debug(f"updatedAppData: {updatedAppData}")
logging.debug(f"params: {params}")
return
async def _do_electrs_status_advanced():
app_id = "electrs"
script_call = (
os.path.join(SHELL_SCRIPT_PATH, "config.scripts", f"bonus.{app_id}.sh")
+ " status showAddress"
)
try:
result = await call_sudo_script(script_call)
except:
# script had error or was not able to deliver all requested data fields
logging.warning(f"error on calling: {script_call}")
return {
"id": f"{app_id}",
"error": f"script not working for api: {script_call}",
}
try:
data = parse_key_value_text(result)
except:
logging.warning(f"error on parsing: {result}")
return {
"id": f"{app_id}",
"error": f"script result parsing error: {script_call}",
}
if data["serviceInstalled"] == "0":
return {
"id": app_id,
"error": "Service not installed.",
}
if data["serviceRunning"] == "0":
return {
"id": app_id,
"error": "Service installed, but not running.",
}
if "initialSynced" not in data:
logging.warning(f"error on calling: {script_call}")
return {
"id": app_id,
"error": f"script not working for api: {script_call}",
}
return {
"version": data["version"],
"localIP": data["localIP"],
"publicIP": data["publicIP"],
"portTCP": data["portTCP"],
"portSSL": data["portSSL"],
"TORaddress": data["TORaddress"],
"initialSyncDone": data["initialSynced"] == "1",
}

View file

@ -1,4 +1,4 @@
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, HTTPException, Path
from fastapi.params import Depends
from loguru import logger
from pydantic import BaseModel
@ -36,6 +36,23 @@ async def get_single_status(id):
return await repo.get_app_status_single(id)
@router.get(
"/status_advanced/{id}",
name=f"{_PREFIX}/status_advanced",
summary="Get the advanced status of a single app by id.",
description="""Some apps might give status information that is computationally
to expensive to include in the normal status endpoint.
> _This endpoint is not implemented on all platforms_
""",
dependencies=[Depends(JWTBearer())],
responses={400: {"description": ("If no or invalid app id is given.")}},
)
@logger.catch(exclude=(HTTPException,))
async def get_single_status_advanced(id: str = Path(..., required=True)):
return await repo.get_app_status_advanced(id)
@router.get(
"/status-sub",
name=f"{_PREFIX}/status-sub",

View file

@ -24,6 +24,10 @@ async def get_app_status():
return await apps.get_app_status()
async def get_app_status_advanced(app_id: str):
return await apps.get_app_status_advanced(app_id)
async def get_app_status_sub():
return await apps.get_app_status_sub()