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

add init healthcheck code #40

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 27 additions & 8 deletions 05-assistive-chatbot/chatbot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from typing import Dict

import dotenv
from fastapi import Body, FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi import Body, FastAPI, Request, status
from pydantic import BaseModel

import chatbot

Expand Down Expand Up @@ -52,19 +52,38 @@ def query(message: str | Dict):
return response


# Make sure to use async functions for faster responses
@app.get("/healthcheck")
async def healthcheck(request: Request):
class HealthCheck(BaseModel):
"""Response model to validate and return when performing a health check."""

status: str
build_date: str
git_sha: str
service_name: str
hostname: str


@app.get(
"/healthcheck",
tags=["healthcheck"],
summary="Perform a Health Check",
response_description="Return HTTP Status Code 200 (OK)",
status_code=status.HTTP_200_OK,
response_model=HealthCheck,
)
async def healthcheck(request: Request) -> HealthCheck:
# Make sure to use async functions for faster responses
logger.info(request.headers)
# TODO: Add a health check - https://pypi.org/project/fastapi-healthchecks/

git_sha = os.environ.get("GIT_SHA", "")
build_date = os.environ.get("BUILD_DATE", "")

service_name = os.environ.get("SERVICE_NAME", "")
hostname = f"{platform.node()} {socket.gethostname()}"

logger.info("Returning: Healthy %s %s", build_date, git_sha)
return HTMLResponse(f"Healthy {git_sha} built at {build_date}<br/>{service_name} {hostname}")
logger.info("Healthy {git_sha} built at {build_date}<br/>{service_name} {hostname}")
ccheng26 marked this conversation as resolved.
Show resolved Hide resolved
return HealthCheck(
build_date=build_date, git_sha=git_sha, status="OK", service_name=service_name, hostname=hostname
)


ALLOWED_ENV_VARS = [
Expand Down
Empty file.
19 changes: 19 additions & 0 deletions 05-assistive-chatbot/test/test_chatbot_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json
import unittest
from chatbot_api import app
from fastapi.testclient import TestClient


# logger = logging.getLogger(f"chatbot.chatbot_api")
ccheng26 marked this conversation as resolved.
Show resolved Hide resolved

client = TestClient(app)
ccheng26 marked this conversation as resolved.
Show resolved Hide resolved


class TestAPI(unittest.TestCase):
def test_read_healthcheck(self):
with self.assertLogs("chatbot.chatbot_api", level="INFO") as cm:
response = client.get("/healthcheck")
response_data = json.loads(response.content)
assert response.status_code == 200
assert response_data["status"] == "OK"
self.assertIn("Healthy", cm.output[1])