Skip to content
Draft
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
122 changes: 122 additions & 0 deletions .github/scripts/api_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env python3
"""API check: counts, validation summary, and route health."""

import json
import sys
import urllib.request
from datetime import datetime, timezone

BASE = "http://localhost:5050/api"


def get(path):
url = f"{BASE}{path}"
try:
req = urllib.request.Request(url)
with urllib.request.urlopen(req, timeout=15) as r:
return r.status, json.loads(r.read())
except urllib.error.HTTPError as e:
return e.code, None
except Exception:
return 0, None


errors = []

# 1. Entity counts
ENTITIES = {
"Tools": "/tools/",
"Methods": "/methods/",
"Case studies": "/casestudies/",
"Regulatory questions": "/regulatory-questions/",
"Stage explanations": "/stages/",
}

counts = {}
for label, path in ENTITIES.items():
status, data = get(path)
if status == 200 and isinstance(data, list):
counts[label] = len(data)
else:
counts[label] = None
errors.append(f"GET {path} -> {status}")

# 2. Validation summary
status, validation = get("/validation/")
if status != 200:
errors.append(f"GET /validation/ -> {status}")
validation = None

# 3. Health check every route
ROUTES = [
("GET", "/tools/"),
("GET", "/tools/cdkdepict"),
("GET", "/methods/"),
("GET", "/methods/5_cfda_assay_to_determine_cytotoxicity"),
("GET", "/regulatory-questions/"),
("GET", "/stages/"),
("GET", "/casestudies/"),
("GET", "/casestudies/kidney"),
("GET", "/compounds/Q2270"),
("GET", "/compounds/Q2270/properties"),
("GET", "/compounds/Q2270/identifiers"),
("GET", "/compounds/Q2270/toxicology"),
("GET", "/compounds/Q2270/experimental-data"),
("GET", "/data/"),
("GET", "/validation/"),
("GET", "/validation/tools"),
]

health = []
for method, path in ROUTES:
status, _ = get(path)
ok = 200 <= status < 300
health.append((method, path, status, ok))
if not ok:
errors.append(f"{method} {path} -> {status}")

# ── build report ──────────────────────────────────────────────────

now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
lines = [f"## API check -- {now}", ""]

# counts
lines.append("### Entity counts")
lines.append("")
lines.append("| Entity | Count |")
lines.append("|--------|------:|")
for label, n in counts.items():
lines.append(f"| {label} | {n if n is not None else 'ERR'} |")
lines.append("")

# validation
if validation and "entities" in validation:
lines.append("### Validation (field completeness)")
lines.append("")
lines.append("| Entity | Entries | Avg complete | Full |")
lines.append("|--------|--------:|-------------:|-----:|")
for e in validation["entities"]:
lines.append(
f"| {e['entity']} | {e['total_entries']}"
f" | {e['avg_completeness_pct']}%"
f" | {e['fully_complete']}/{e['total_entries']} |"
)
lines.append("")

# health
lines.append("### Route health")
lines.append("")
lines.append("| Method | Route | Status |")
lines.append("|--------|-------|-------:|")
for method, path, status, ok in health:
mark = "ok" if ok else f"FAIL ({status})"
lines.append(f"| {method} | `{path}` | {mark} |")
lines.append("")

# result
all_ok = not errors
lines.append(f"**Result: {'PASS' if all_ok else 'FAIL'}**")

print("\n".join(lines))
if not all_ok:
sys.exit(1)
45 changes: 45 additions & 0 deletions .github/workflows/pr-api-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: API check

on:
pull_request:

permissions:
contents: read
pull-requests: write

jobs:
api-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Build Docker image
run: docker build -t vhp4safety .

- name: Start container
run: |
docker run -d --name vhp4safety -p 5050:5050 vhp4safety
for i in $(seq 1 30); do
curl -sf http://localhost:5050/api/tools/ && break
sleep 2
done

- name: Run API checks
id: report
run: |
python3 .github/scripts/api_check.py > report.md
{
echo 'REPORT<<GHEOF'
cat report.md
echo 'GHEOF'
} >> "$GITHUB_OUTPUT"

- name: Post PR comment
uses: marocchino/sticky-pull-request-comment@v2
with:
header: api-check
message: ${{ steps.report.outputs.REPORT }}

- name: Stop container
if: always()
run: docker stop vhp4safety && docker rm vhp4safety
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ __pycache__/
*.py[cod]
*$py.class

# SQLite database
data/*.db
# C extensions
*.so

Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Create data directory for SQLite DB
RUN mkdir -p /usr/src/app/data

# Copy entrypoint script
COPY entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh

EXPOSE 5050

# Define the entrypoint script
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
Loading
Loading