Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/hw2-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
env:
PYTHONPATH: ${{ github.workspace }}/hw2/hw
run: |
pytest test_homework2.py -v
pytest -v
25 changes: 25 additions & 0 deletions hw2/hw/.github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: tests

on:
push:
branches: ["**"]
pull_request:
branches: ["**"]

jobs:
pytest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests with coverage
run: |
pytest

16 changes: 16 additions & 0 deletions hw2/hw/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY shop_api ./shop_api
COPY README.md ./

EXPOSE 8000

CMD ["uvicorn", "shop_api.main:app", "--host", "0.0.0.0", "--port", "8000"]
16 changes: 16 additions & 0 deletions hw2/hw/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import sys
import pytest

PROJECT_ROOT = os.path.dirname(__file__)
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT)


@pytest.fixture(scope="session", autouse=True)
def _ensure_db_schema() -> None:
from shop_api.db import Base as ORMBase, engine

ORMBase.metadata.drop_all(bind=engine)
ORMBase.metadata.create_all(bind=engine)

61 changes: 61 additions & 0 deletions hw2/hw/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
services:
db:
image: postgres:16-alpine
container_name: shop_db
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 5s
timeout: 5s
retries: 5
volumes:
- pg_data:/var/lib/postgresql/data

api:
build: .
container_name: shop_api
environment:
DATABASE_URL: postgresql+psycopg://postgres:postgres@db:5432/postgres
PYTHONUNBUFFERED: "1"
ports:
- "8000:8000"
depends_on:
db:
condition: service_healthy

prometheus:
image: prom/prometheus:v2.53.0
container_name: prometheus
command: ["--config.file=/etc/prometheus/prometheus.yml", "--web.enable-lifecycle"]
volumes:
- ./monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
ports:
- "9090:9090"
depends_on:
- api

grafana:
image: grafana/grafana:11.1.4
container_name: grafana
ports:
- "3000:3000"
volumes:
- grafana_data:/var/lib/grafana
- ./monitoring/grafana/provisioning/datasources:/etc/grafana/provisioning/datasources:ro
- ./monitoring/grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards:ro
- ./monitoring/grafana/dashboards:/var/lib/grafana/dashboards:ro
depends_on:
- prometheus

volumes:
pg_data:
driver: local
grafana_data:
driver: local


52 changes: 52 additions & 0 deletions hw2/hw/monitoring/grafana/dashboards/fastapi-overview.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"id": null,
"uid": "fastapi-overview",
"title": "FastAPI Overview",
"timezone": "browser",
"schemaVersion": 39,
"version": 1,
"refresh": "10s",
"panels": [
{
"type": "timeseries",
"title": "Requests per second by status",
"gridPos": {"x": 0, "y": 0, "w": 12, "h": 8},
"targets": [
{
"refId": "A",
"expr": "sum by (status) (rate(http_requests_total[1m]))"
}
]
},
{
"type": "timeseries",
"title": "Request duration p95 (s)",
"gridPos": {"x": 12, "y": 0, "w": 12, "h": 8},
"targets": [
{
"refId": "B",
"expr": "histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))"
}
]
},
{
"type": "stat",
"title": "API target up",
"gridPos": {"x": 0, "y": 8, "w": 6, "h": 4},
"options": {"reduceOptions": {"calcs": ["lastNotNull"], "fields": "", "values": false}},
"targets": [
{"refId": "C", "expr": "up{job=\"api\"}"}
]
},
{
"type": "stat",
"title": "Total requests",
"gridPos": {"x": 6, "y": 8, "w": 6, "h": 4},
"options": {"reduceOptions": {"calcs": ["lastNotNull"], "fields": "", "values": false}},
"targets": [
{"refId": "D", "expr": "sum(http_requests_total)"}
]
}
],
"templating": {"list": []}
}
11 changes: 11 additions & 0 deletions hw2/hw/monitoring/grafana/provisioning/dashboards/dashboards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: 1

providers:
- name: 'default'
orgId: 1
folder: ''
type: file
disableDeletion: false
allowUiUpdates: false
options:
path: /var/lib/grafana/dashboards
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: 1

datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
editable: true
13 changes: 13 additions & 0 deletions hw2/hw/monitoring/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
global:
scrape_interval: 15s
evaluation_interval: 15s

scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["prometheus:9090"]

- job_name: "api"
metrics_path: /metrics
static_configs:
- targets: ["api:8000"]
4 changes: 4 additions & 0 deletions hw2/hw/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
addopts = -q --maxfail=1 --disable-warnings --cov=shop_api --cov-report=term-missing --cov-branch --cov-fail-under=95
testpaths = .

8 changes: 8 additions & 0 deletions hw2/hw/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ uvicorn>=0.24.0
# Зависимости для тестирования
pytest>=7.4.0
pytest-asyncio>=0.21.0
pytest-cov>=4.1.0
httpx>=0.27.2
Faker>=37.8.0

# База данных
SQLAlchemy>=2.0.29
psycopg[binary]>=3.1.18
python-dotenv>=1.0.1
prometheus-client>=0.20.0
prometheus-fastapi-instrumentator>=6.1.0
Loading