-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from TheJacksonLaboratory/G3-271-health-check-…
…enpoint G3-271 server health check endpoint
- Loading branch information
Showing
11 changed files
with
369 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "geneweaver-api" | ||
version = "0.7.0a2" | ||
version = "0.7.0a3" | ||
description = "The Geneweaver API" | ||
authors = [ | ||
"Alexander Berger <[email protected]>", | ||
|
@@ -20,7 +20,7 @@ python = "^3.9" | |
geneweaver-core = "^0.10.0a2" | ||
fastapi = {extras = ["all"], version = "^0.99.1"} | ||
uvicorn = {extras = ["standard"], version = "^0.24.0"} | ||
geneweaver-db = "0.5.0a2" | ||
geneweaver-db = "0.5.0a8" | ||
psycopg-pool = "^3.1.7" | ||
requests = "^2.31.0" | ||
python-jose = {extras = ["cryptography"], version = "^3.3.0"} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Endpoints related to system health.""" | ||
|
||
from typing import Optional | ||
|
||
from fastapi import APIRouter, Depends, Query | ||
from geneweaver.api import dependencies as deps | ||
from geneweaver.api.services import monitors as monitors_service | ||
from typing_extensions import Annotated | ||
|
||
from . import message as api_message | ||
|
||
router = APIRouter(prefix="/monitors", tags=["monitors"]) | ||
|
||
|
||
@router.get("/servers/health") | ||
def get_health_check( | ||
cursor: Optional[deps.Cursor] = Depends(deps.cursor), | ||
db_health_check: Annotated[ | ||
Optional[bool], Query(description=api_message.CHECK_DB_HEALTH) | ||
] = False, | ||
) -> dict: | ||
"""Return 200 API response if reachable and optionally check db health.""" | ||
response = {"status": "UP"} | ||
|
||
if db_health_check: | ||
db_health_response = monitors_service.check_db_health(cursor) | ||
response["DB_status"] = db_health_response | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Service monitors for system health.""" | ||
|
||
from fastapi.logger import logger | ||
from geneweaver.db.monitor.db_health import health_check as db_health_check | ||
from psycopg import Cursor | ||
|
||
|
||
def check_db_health(cursor: Cursor) -> dict: | ||
"""Check DB health status. | ||
@param cursor: DB cursor | ||
@return: db health response (dict). | ||
""" | ||
try: | ||
results = db_health_check(cursor) | ||
return results | ||
|
||
except Exception as err: | ||
logger.error(err) | ||
raise err |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Tests for system monitor apis.""" | ||
|
||
from unittest.mock import patch | ||
|
||
from tests.data import test_monitors_data | ||
|
||
db_health_status = test_monitors_data.get("db_health_status") | ||
|
||
|
||
@patch("geneweaver.api.services.monitors.check_db_health") | ||
def test_valid_url_req(mock_db_heath_service_call, client): | ||
"""Test valid url request to get system health status.""" | ||
response = client.get( | ||
url="/api/monitors/servers/health", params={"db_health_check": False} | ||
) | ||
assert response.status_code == 200 | ||
|
||
mock_db_heath_service_call.return_value = db_health_status.get("DB_status") | ||
response = client.get( | ||
url="/api/monitors/servers/health", params={"db_health_check": True} | ||
) | ||
assert response.status_code == 200 | ||
assert response.json() == db_health_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"db_health_status": { | ||
"status": "UP", | ||
"DB_status": { | ||
"gene_identifier_last_update": { | ||
"gi_date": "2024-06-24T01:54:48.023015+00:00" | ||
}, | ||
"gene_count": { | ||
"count": 16 | ||
}, | ||
"geneset_count": { | ||
"count": 272143 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Tests for system monitor services.""" | ||
|
||
from unittest.mock import patch | ||
|
||
import pytest | ||
from geneweaver.api.services import monitors | ||
|
||
from tests.data import test_monitors_data | ||
|
||
db_health_status = test_monitors_data.get("db_health_status") | ||
|
||
|
||
@patch("geneweaver.api.services.monitors.db_health_check") | ||
def test_db_health_check(mock_db_heath): | ||
"""Test call to get db health status.""" | ||
mock_db_heath.return_value = db_health_status.get("DB_status") | ||
response = monitors.check_db_health(None) | ||
|
||
assert response == db_health_status.get("DB_status") | ||
|
||
|
||
@patch("geneweaver.api.services.monitors.db_health_check") | ||
def test_db_health_check_error(mock_db_heath): | ||
"""Test error in call to get db health status.""" | ||
mock_db_heath.side_effect = Exception("ERROR") | ||
|
||
with pytest.raises(expected_exception=Exception): | ||
monitors.check_db_health(None) |