mirror of
https://github.com/fusion44/blitz_api.git
synced 2026-08-13 11:52:45 +02:00
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:
parent
f22074c0b2
commit
00e56ba155
5 changed files with 99 additions and 1 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue