Files
notifications-api/app/status/healthcheck.py

72 lines
2.5 KiB
Python
Raw Normal View History

2025-06-05 08:12:56 -07:00
from flask import Blueprint, current_app, jsonify, request
2024-04-24 16:27:20 -04:00
from sqlalchemy import text
2025-07-07 07:42:33 -07:00
from werkzeug.exceptions import ServiceUnavailable
from app import db, version
2023-07-10 11:06:29 -07:00
from app.dao.organization_dao import dao_count_organizations_with_live_services
2021-03-10 13:55:06 +00:00
from app.dao.services_dao import dao_count_live_services
2023-08-29 14:54:30 -07:00
status = Blueprint("status", __name__)
2023-08-29 14:54:30 -07:00
@status.route("/", methods=["GET"])
@status.route("/_status", methods=["GET", "POST"])
2016-01-14 16:13:27 +00:00
def show_status():
2025-06-05 08:12:56 -07:00
try:
if request.args.get("simple", None):
2025-07-31 10:50:55 -04:00
response = jsonify(status="ok")
2025-06-05 08:12:56 -07:00
else:
2025-07-31 10:50:55 -04:00
response = jsonify(
status="ok", # This should be considered part of the public API
git_commit=version.__git_commit__,
build_time=version.__time__,
db_version=get_db_version(),
2025-06-05 08:12:56 -07:00
)
2025-07-31 10:50:55 -04:00
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response, 200
2025-06-05 08:12:56 -07:00
except Exception as e:
current_app.logger.error(
f"Unexpected error in show_status: {str(e)}", exc_info=True
2023-08-29 14:54:30 -07:00
)
2025-07-07 07:42:33 -07:00
# raise Exception(status_code=503, detail="Service temporarily unavailable")
raise ServiceUnavailable("Service temporarily unavailable")
2023-08-29 14:54:30 -07:00
@status.route("/_status/live-service-and-organization-counts")
2023-07-10 11:06:29 -07:00
def live_service_and_organization_counts():
2025-06-05 08:12:56 -07:00
try:
2025-07-31 10:50:55 -04:00
response = jsonify(
organizations=dao_count_organizations_with_live_services(),
services=dao_count_live_services(),
2025-06-05 08:12:56 -07:00
)
2025-07-31 10:50:55 -04:00
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response, 200
2025-06-05 08:12:56 -07:00
except Exception as e:
current_app.logger.error(
f"Unexpected error in live_service_and_organization_counts: {str(e)}",
exc_info=True,
)
2025-07-07 07:42:33 -07:00
# raise Exception(status_code=503, detail="Service temporarily unavailable")
raise ServiceUnavailable("Service temporarily unavailable")
def get_db_version():
2025-06-05 08:17:30 -07:00
try:
query = "SELECT version_num FROM alembic_version"
full_name = db.session.execute(text(query)).fetchone()[0]
return full_name
except Exception as e:
current_app.logger.error(
f"Unexpected error in get_db_version: {str(e)}",
exc_info=True,
)
raise Exception(status_code=503, detail="Database temporarily unavailable")