Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/alert status #322

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion iap/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import os.path
from datetime import datetime, timezone

import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, Depends
from fastapi.exceptions import RequestValidationError
from mangum import Mangum
from pydantic.v1.error_wrappers import _display_error_type_and_ctx
from sqlalchemy import select
from starlette.requests import Request
from starlette.responses import FileResponse, JSONResponse
from starlette.staticfiles import StaticFiles
from starlette.status import HTTP_400_BAD_REQUEST, HTTP_500_INTERNAL_SERVER_ERROR

from common import logger
from common.models.receipt import Receipt
from common.enums import TxStatus
from iap.dependencies import session
from iap.exceptions import ReceiptNotFoundException
from . import api, settings

Expand Down Expand Up @@ -72,6 +77,7 @@ def handle_500(request, e):
"""
This can cause Exception inside itself: Please track https://github.com/tiangolo/fastapi/discussions/8647
"""
logger.error(e)
return JSONResponse(status_code=HTTP_500_INTERNAL_SERVER_ERROR,
content=f"An unexpected error occurred. Please contact to administrator. :: {str(e)}")

Expand Down Expand Up @@ -101,6 +107,18 @@ def favicon():
return "iap/frontend/build/favicon.png"


@app.get("/receipt-status", tags=["Default"])
def receipt_status(sess=Depends(session)):
invalid_receipt_list = sess.scalars(select(Receipt).where(Receipt.tx_status == TxStatus.INVALID)).fetchall()
if not invalid_receipt_list:
return JSONResponse(status_code=200, content="No INVALID transactions.")

delay = (datetime.now(tz=timezone.utc) - min([x.created_at for x in invalid_receipt_list])).seconds
msg = f"There are {len(invalid_receipt_list)} Invalid receipts over {delay} seconds."
# Return error when delay more than 5min
return JSONResponse(status_code=503 if delay >= 300 else 200, content=msg)


@app.get("/", response_class=FileResponse, tags=["View"], summary="Index page")
@app.get(
"/{page}", response_class=FileResponse, tags=["View"], summary="Opens pages provided name",
Expand Down