Skip to content

Commit

Permalink
added repair option for corrupted endpoint data (master token only)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Oct 9, 2024
1 parent 0247f75 commit 7cf78ad
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 15 deletions.
11 changes: 4 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
.idea
.git
*.log
*_servers*
.github
LICENSE
tokens.json
*endpoints*
beta
Copy of endpoints
beta_users.py
tokens.json
TMP
__pycache__
endpoints*
tokens*
src/tokens.json
src/Bot/config.py
src/_astroidapi.log
/website
/web
website.zip
src/Bot/config.py
src/astroidapi.zip
src/astroidapi/beta_config.py
src/Bot/config.py

src/Bot/nerimity_servers/1528027692992208896.json
src/tokens.json
assets/517057624072519680.png
src/astroidapi/test.py
1 change: 0 additions & 1 deletion src/.tokens.json
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

2 changes: 1 addition & 1 deletion src/Bot/.config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
SDB_USER = ""
SDB_PASS = ""
SDB_NAMESPACE = ""
SDB_DATABASE = ""
SDB_DATABASE = ""
21 changes: 17 additions & 4 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@
docs_url=None
)





api.add_middleware(
CORSMiddleware,
allow_origins=["*"],
Expand Down Expand Up @@ -490,6 +486,23 @@ async def endpoint_healthcheck(endpoint: int, token: str):
"details": "getendpointerror"})


@api.post("/healthcheck/{endpoint}/repair", description="Repair the endpoint.")
async def repair_endpoint(endpoint: int, token: str):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})

if token == Bot.config.MASTER_TOKEN:
try:
summary = await astroidapi.health_check.HealthCheck.EndpointCheck.repair_structure(endpoint)
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Repaired.", "summary": summary})
except Exception as e:
logging.exception(traceback.print_exc())
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})


@api.post("/create", description="Create an endpoint.",
response_description="Endpoints data.")
async def create_endpoint(endpoint: int):
Expand Down
369 changes: 367 additions & 2 deletions src/astroidapi/health_check.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/astroidapi/surrealdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ async def get_all_endpoints():
raise errors.SurrealDBHandler.GetEndpointError(e)


async def write_to_structure(endpoint, key, value):
print(f"Writing {key} to {endpoint}")
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
print(key, value)
await db.query(f"UPDATE endpoints:`{endpoint}` SET {key} = {value}")
return await db.query(f"SELECT * FROM endpoints:`{endpoint}`")
except Exception as e:
raise errors.SurrealDBHandler.UpdateEndpointError(e)


class AttachmentProcessor:

@classmethod
Expand Down
11 changes: 11 additions & 0 deletions src/astroidapi/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def convert_keys_to_dotted(json_data, parent_key=""):
dotted_dict = {}
for key, value in json_data.items():
if isinstance(value, dict):
dotted_dict.update(convert_keys_to_dotted(value, f"{parent_key}{key}."))
else:
dotted_dict[f"{parent_key}{key}"] = value
return dotted_dict


print(convert_keys_to_dotted({"a": {"b": {"c": 1}}}))

0 comments on commit 7cf78ad

Please sign in to comment.