Skip to content

Commit

Permalink
Merge pull request #180 from Studio-Yandex-Practicum/feature/add_heal…
Browse files Browse the repository at this point in the history
…thcheck

feature/add_healthcheck_bot_checkup
  • Loading branch information
NiKuma0 authored Sep 8, 2023
2 parents 377b2d3 + 56e9b87 commit e9dacf5
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 42 deletions.
101 changes: 65 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ isort = "^5.12.0"
pyngrok = "^6.0.0"
python-dotenv = "^1.0.0"
pre-commit = "^3.3.2"
gitpython = "^3.1.32"


[build-system]
Expand Down
1 change: 1 addition & 0 deletions src/api/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATE_FORMAT = "%d-%m-%Y"
DATE_TIME_FORMAT = "%d-%m-%Y %H:%M:%S"
2 changes: 2 additions & 0 deletions src/api/endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from .analytics import analytic_router
from .categories import category_router
from .external_site_user import site_user_router
from .health_check import health_check_router
from .notification import notification_router
from .tasks import task_router
from .telegram_webhook import telegram_webhook_router

__all__ = (
"analytic_router",
"category_router",
"health_check_router",
"task_router",
"telegram_webhook_router",
"form_router",
Expand Down
22 changes: 22 additions & 0 deletions src/api/endpoints/health_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends

from src.api.schemas import HealthCheck
from src.api.services.health_check import HealthCheckService
from src.core.logging.utils import logger_decor
from src.depends import Container

health_check_router = APIRouter()


@logger_decor
@health_check_router.get("/", description="Проверяет соединение с БД, ботом и выводит информацию о последнем коммите.")
@inject
async def get_health_check(
health_check_service: HealthCheckService = Depends(Provide[Container.health_check_service]),
) -> HealthCheck:
return HealthCheck(
db=await health_check_service.check_db_connection(),
bot=await health_check_service.check_bot(),
git=await health_check_service.get_last_commit(),
)
2 changes: 2 additions & 0 deletions src/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from src.api.endpoints import (
analytic_router,
category_router,
health_check_router,
notification_router,
site_user_router,
task_router,
Expand All @@ -13,6 +14,7 @@
api_router = APIRouter(prefix=settings.ROOT_PATH)
api_router.include_router(analytic_router, prefix="/analytics", tags=["Analytic"])
api_router.include_router(category_router, prefix="/categories", tags=["Categories"])
api_router.include_router(health_check_router, prefix="/health_check", tags=["Healthcheck"])
api_router.include_router(notification_router, prefix="/messages", tags=["Messages"])
api_router.include_router(site_user_router, prefix="/external_user_registration", tags=["ExternalSiteUser"])
api_router.include_router(task_router, prefix="/tasks", tags=["Tasks"])
Expand Down
46 changes: 41 additions & 5 deletions src/api/schemas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import urllib
from datetime import date
from typing import Dict, Optional
from typing import Optional

from pydantic import BaseModel, Extra, Field, NonNegativeInt, StrictStr, field_validator, root_validator
from typing_extensions import NotRequired, TypedDict

from src.core.db.models import ExternalSiteUser
from src.core.enums import TelegramNotificationUsersGroups
Expand Down Expand Up @@ -163,9 +164,44 @@ def specializations_str_validation(cls, value: str):
class Analytic(BaseModel):
"""Класс модели запроса для статистики."""

command_stats: Dict[str, str] = {}
command_stats: dict[str, str] = {}
reasons_canceling: str = ""
number_users: int = 0
all_users_statistic: Dict[str, str] = {}
active_users_statistic: Dict[str, str] = {}
tasks: Dict[str, str] = {}
all_users_statistic: dict[str, str] = {}
active_users_statistic: dict[str, str] = {}
tasks: dict[str, str] = {}


class DBStatus(TypedDict):
"""Класс ответа для проверки работы базы данных."""

status: bool
last_update: NotRequired[str]
active_tasks: NotRequired[int]
db_connection_error: NotRequired[str]


class BotStatus(TypedDict):
"""Класс ответа для проверки работы бота."""

status: bool
method: NotRequired[str]
url: NotRequired[str]
error: NotRequired[str]


class CommitStatus(TypedDict):
"""Класс ответа для git коммита."""

last_commit: str
commit_date: str
git_tags: list[str]
commit_error: NotRequired[str]


class HealthCheck(ResponseBase):
"""Класс модели запроса для проверки работы бота."""

db: DBStatus = {}
bot: BotStatus = {}
git: CommitStatus = {}
Loading

0 comments on commit e9dacf5

Please sign in to comment.