Compare commits
56
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
249f1aeea0 | ||
|
|
0b34b85a5a | ||
|
|
00a8837aa1 | ||
|
|
5e2591fb56 | ||
|
|
93cdde13a7 | ||
|
|
0c143124b3 | ||
|
|
58d2260d89 | ||
|
|
070f9967bc | ||
|
|
d2af8881a8 | ||
|
|
9fd41ce3f0 | ||
|
|
d9eefcb1fa | ||
|
|
78cf89c0fa | ||
|
|
c0de60e4a5 | ||
|
|
123df8a48a | ||
|
|
7f821b5723 | ||
|
|
597b94ff25 | ||
|
|
375ed57778 | ||
|
|
41e5db3921 | ||
|
|
af08c64032 | ||
|
|
ed05448e56 | ||
|
|
ff58caaad0 | ||
|
|
36c82ac942 | ||
|
|
4fbfdb1c6a | ||
|
|
0805e48fe6 | ||
|
|
427d3f5419 | ||
|
|
16890af944 | ||
|
|
32e673044f | ||
|
|
40b1151023 | ||
|
|
5c51d3bc4f | ||
|
|
1dbcf33540 | ||
|
|
3806f4ac47 | ||
|
|
4d67cd9d66 | ||
|
|
9c955db191 | ||
|
|
81473e20eb | ||
|
|
798e103eb8 | ||
|
|
d65129f477 | ||
|
|
482605e6a1 | ||
|
|
47b2519b0b | ||
|
|
c3b2ee3310 | ||
|
|
31d1e85b5c | ||
|
|
5dbe887ce3 | ||
|
|
4552e33cb3 | ||
|
|
c53aade360 | ||
|
|
d88979e37a | ||
|
|
11373138ca | ||
|
|
75b257bb15 | ||
|
|
c0bb562a8d | ||
|
|
32577a7fda | ||
|
|
a6d68134cd | ||
|
|
fa8a92208a | ||
|
|
0c047b708f | ||
|
|
efbb978074 | ||
|
|
21ce1dc395 | ||
|
|
380b361e70 | ||
|
|
2a859aa16c | ||
|
|
d14ed9a511 |
No files matched your search
+35
-51
@@ -3,10 +3,7 @@ name: Deploy Trainer_LLM to llm-node
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
paths:
|
||||
- "knowledge/**"
|
||||
- "llm-api/**"
|
||||
- "scripts/**"
|
||||
# Kein paths-Filter mehr: neue Ordner deployen sofort mit, sobald sie in DEPLOY_DIRS stehen.
|
||||
|
||||
concurrency:
|
||||
group: deploy-trainer-llm
|
||||
@@ -14,65 +11,52 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: linux_host # muss zum Runner-Label passen
|
||||
runs-on: linux_host
|
||||
env:
|
||||
# -> Hier trägst du ALLE zu deployenden Top-Level-Verzeichnisse ein:
|
||||
# Bei neuen Ordnern einfach anhängen (durch Leerzeichen getrennt)
|
||||
DEPLOY_DIRS: "knowledge llm-api scripts schemas tests"
|
||||
TARGET_BASE: "/home/llmadmin"
|
||||
steps:
|
||||
- name: Checkout
|
||||
# Absolute URL reduziert Abhängigkeit von DEFAULT_ACTIONS_URL
|
||||
uses: https://github.com/actions/checkout@v4
|
||||
|
||||
- name: Sanity — Runner & Commit
|
||||
run: |
|
||||
echo "Runner: $RUNNER_NAME Labels: $RUNNER_LABELS"
|
||||
echo "Commit: $GITEA_SHA Ref: $GITEA_REF"
|
||||
echo "Commit: ${GITHUB_SHA:-$GITEA_SHA} Ref: ${GITHUB_REF:-$GITEA_REF}"
|
||||
uname -a
|
||||
|
||||
- name: Debug whoami & write test
|
||||
- name: Ensure target base exists
|
||||
run: |
|
||||
whoami
|
||||
id
|
||||
getent passwd $(whoami) || true
|
||||
# Testet Schreibrecht unter /home/llmadmin
|
||||
touch /home/llmadmin/.write_test && rm /home/llmadmin/.write_test || echo "no write"
|
||||
install -d "$TARGET_BASE"
|
||||
|
||||
- name: Ensure target directories exist
|
||||
- name: Deploy whitelisted directories
|
||||
run: |
|
||||
mkdir -p /home/llmadmin/knowledge
|
||||
mkdir -p /home/llmadmin/llm-api
|
||||
mkdir -p /home/llmadmin/scripts
|
||||
set -euo pipefail
|
||||
IFS=' ' read -r -a DIRS <<< "$DEPLOY_DIRS"
|
||||
|
||||
- name: Rsync knowledge/
|
||||
if: ${{ hashFiles('knowledge/**') != '' }}
|
||||
run: |
|
||||
rsync -a --delete \
|
||||
--exclude='.git' \
|
||||
--exclude='.env' --exclude='.env.*' --exclude='**/.env' --exclude='**/.env.*' \
|
||||
knowledge/ /home/llmadmin/knowledge/
|
||||
echo "Synced knowledge/"
|
||||
|
||||
- name: Rsync llm-api/
|
||||
if: ${{ hashFiles('llm-api/**') != '' }}
|
||||
run: |
|
||||
rsync -a --delete \
|
||||
--exclude='.git' \
|
||||
--exclude='.env' --exclude='.env.*' --exclude='**/.env' --exclude='**/.env.*' \
|
||||
llm-api/ /home/llmadmin/llm-api/
|
||||
echo "Synced llm-api/"
|
||||
|
||||
- name: Rsync scripts/
|
||||
if: ${{ hashFiles('scripts/**') != '' }}
|
||||
run: |
|
||||
rsync -a --delete \
|
||||
--exclude='.git' \
|
||||
--exclude='.env' --exclude='.env.*' --exclude='**/.env' --exclude='**/.env.*' \
|
||||
scripts/ /home/llmadmin/scripts/
|
||||
echo "Synced scripts/"
|
||||
for d in "${DIRS[@]}"; do
|
||||
if [ -d "$d" ]; then
|
||||
# Nur wenn im Ordner auch Dateien/Unterordner liegen
|
||||
if [ -n "$(find "$d" -mindepth 1 -print -quit)" ]; then
|
||||
echo ">> Syncing $d -> $TARGET_BASE/$d"
|
||||
install -d "$TARGET_BASE/$d"
|
||||
rsync -a --delete \
|
||||
--exclude='.git' \
|
||||
--exclude='.env' --exclude='.env.*' --exclude='**/.env' --exclude='**/.env.*' \
|
||||
"$d"/ "$TARGET_BASE/$d"/
|
||||
else
|
||||
echo ">> Skipping $d (leer)"
|
||||
fi
|
||||
else
|
||||
echo ">> Skipping $d (existiert nicht im Repo)"
|
||||
fi
|
||||
done
|
||||
|
||||
- name: Optional — systemctl --user restart llm-api (ignore if missing)
|
||||
continue-on-error: true
|
||||
env:
|
||||
XDG_RUNTIME_DIR: /run/user/${{ inputs.uid || 1000 }}
|
||||
run: |
|
||||
# Versuche nur zu restarten, wenn der Service existiert
|
||||
if systemctl --user list-unit-files | grep -q '^llm-api.service'; then
|
||||
systemctl --user restart llm-api.service
|
||||
systemctl --user --no-pager status llm-api.service --full -l || true
|
||||
@@ -80,9 +64,9 @@ jobs:
|
||||
echo "llm-api.service nicht gefunden — Schritt wird übersprungen."
|
||||
fi
|
||||
|
||||
- name: Post-check — show latest changes
|
||||
- name: Post-check — list targets
|
||||
run: |
|
||||
echo "Deploy complete. Listing targets:"
|
||||
ls -la /home/llmadmin/knowledge | tail -n +1 || true
|
||||
ls -la /home/llmadmin/llm-api | tail -n +1 || true
|
||||
ls -la /home/llmadmin/scripts | tail -n +1 || true
|
||||
for d in $DEPLOY_DIRS; do
|
||||
echo "== $TARGET_BASE/$d =="
|
||||
ls -la "$TARGET_BASE/$d" 2>/dev/null | tail -n +1 || true
|
||||
done
|
||||
@@ -0,0 +1,77 @@
|
||||
# ======================
|
||||
# Laufzeit / Server
|
||||
# ======================
|
||||
UVICORN_HOST=0.0.0.0
|
||||
UVICORN_PORT=8000
|
||||
LOG_LEVEL=INFO
|
||||
|
||||
# ======================
|
||||
# Qdrant – Verbindung
|
||||
# ======================
|
||||
QDRANT_HOST=127.0.0.1
|
||||
QDRANT_PORT=6333
|
||||
QDRANT_URL=http://localhost:6333
|
||||
|
||||
# ======================
|
||||
# Collections – Namen
|
||||
# Hinweise:
|
||||
# - PLAN_COLLECTION wird von unseren neuen Routern verwendet.
|
||||
# - Einige ältere Komponenten nutzen ggf. *QDRANT_COLLECTION_PLANS*/*QDRANT_COLLECTION_EXERCISES*.
|
||||
# Belasse sie konsistent oder kommentiere sie aus, um Verwirrung zu vermeiden.
|
||||
# ======================
|
||||
PLAN_COLLECTION=plans
|
||||
PLAN_TEMPLATE_COLLECTION=plan_templates
|
||||
PLAN_SESSION_COLLECTION=plan_sessions
|
||||
EXERCISE_COLLECTION=exercises
|
||||
# Kompatibilität (optional, falls von Alt-Code gelesen):
|
||||
# QDRANT_COLLECTION_PLANS=training_plans
|
||||
# QDRANT_COLLECTION_EXERCISES=exercises
|
||||
|
||||
# ======================
|
||||
# Strict-Mode für /plan
|
||||
# 0 / leer = aus (Standard)
|
||||
# 1/true/...= an → jede exercise_external_id muss in EXERCISE_COLLECTION existieren, sonst 422
|
||||
# ======================
|
||||
PLAN_STRICT_EXERCISES=0
|
||||
|
||||
# ======================
|
||||
# Ollama (LLM) – lokal
|
||||
# ======================
|
||||
OLLAMA_URL=http://127.0.0.1:11434/api/generate
|
||||
OLLAMA_ENDPOINT=/api/generate
|
||||
OLLAMA_MODEL=mistral
|
||||
OLLAMA_TIMEOUT_SECONDS=120
|
||||
|
||||
# ======================
|
||||
# Embeddings
|
||||
# ======================
|
||||
EMBEDDING_MODEL=all-MiniLM-L6-v2
|
||||
EMBEDDING_DIM=384
|
||||
|
||||
# ======================
|
||||
# FastAPI / App Defaults
|
||||
# ======================
|
||||
DEFAULT_COLLECTION=default
|
||||
API_TITLE="KI Trainerassistent API"
|
||||
API_DESCRIPTION="Lokale API für Trainingsplanung (Karate, Gewaltschutz, etc.)"
|
||||
|
||||
# ======================
|
||||
# Wiki Importer
|
||||
# ======================
|
||||
API_BASE_URL=http://localhost:8000
|
||||
WIKI_BASE_URL=https://karatetrainer.net
|
||||
WIKI_API_URL=https://karatetrainer.net/api.php
|
||||
WIKI_BOT_USER=Bot
|
||||
WIKI_BOT_PASSWORD=***set_me***
|
||||
WIKI_SMW_LIMIT=500
|
||||
WIKI_SMW_OFFSET=0
|
||||
WIKI_TIMEOUT=15
|
||||
WIKI_BATCH=50
|
||||
WIKI_RETRIES=1
|
||||
WIKI_SLEEP_MS=0
|
||||
|
||||
# ======================
|
||||
# Test-/Hilfs-URLs (für pytest & Tools)
|
||||
# ======================
|
||||
BASE_URL=http://127.0.0.1:8000
|
||||
QDRANT_BASE=http://127.0.0.1:6333
|
||||
+285
-42
@@ -1,4 +1,16 @@
|
||||
# Test eines Kommentars, um die Funktion des gitea testen zu können
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
exercise_router.py – v1.7.0
|
||||
|
||||
Neu:
|
||||
- Endpoint **POST /exercise/search**: kombinierbare Filter (discipline, duration, equipment any/all, keywords any/all,
|
||||
capability_geN / capability_eqN + names) + optionaler Vektor-Query (query-Text). Ausgabe inkl. Score.
|
||||
- Facetten erweitert: neben capability_ge1..ge5 jetzt auch capability_eq1..eq5.
|
||||
- Idempotenz-Fix & Payload-Scroll (aus v1.6.2) beibehalten.
|
||||
- API-Signaturen bestehender Routen unverändert.
|
||||
|
||||
Hinweis: Die „eq/ge“-Felder werden beim Upsert gesetzt; für Alt-Punkte einmal das Backfill laufen lassen.
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -11,8 +23,9 @@ from qdrant_client.models import (
|
||||
VectorParams,
|
||||
Distance,
|
||||
PointIdsList,
|
||||
# NEW: für Filter-Queries (Lookup via external_id)
|
||||
Filter, FieldCondition, MatchValue,
|
||||
Filter,
|
||||
FieldCondition,
|
||||
MatchValue,
|
||||
)
|
||||
import os
|
||||
|
||||
@@ -23,13 +36,13 @@ router = APIRouter()
|
||||
# =========================
|
||||
class Exercise(BaseModel):
|
||||
id: str = Field(default_factory=lambda: str(uuid4()))
|
||||
# NEW — optional, bricht vorhandene POST-Calls nicht
|
||||
external_id: Optional[str] = None # z.B. "mw:12345"
|
||||
fingerprint: Optional[str] = None # sha256 über Kernfelder
|
||||
source: Optional[str] = None # Herkunft, z.B. "MediaWiki"
|
||||
imported_at: Optional[datetime] = None # vom Import gesetzt
|
||||
# Upsert-Metadaten
|
||||
external_id: Optional[str] = None
|
||||
fingerprint: Optional[str] = None
|
||||
source: Optional[str] = None
|
||||
imported_at: Optional[datetime] = None
|
||||
|
||||
# Bestehende Felder (unverändert)
|
||||
# Domain-Felder
|
||||
title: str
|
||||
summary: str
|
||||
short_description: str
|
||||
@@ -55,12 +68,43 @@ class DeleteResponse(BaseModel):
|
||||
count: int
|
||||
collection: str
|
||||
|
||||
class ExerciseSearchRequest(BaseModel):
|
||||
# Optionaler Semantik-Query (Vektor)
|
||||
query: Optional[str] = None
|
||||
limit: int = Field(default=20, ge=1, le=200)
|
||||
offset: int = Field(default=0, ge=0)
|
||||
|
||||
# Einfache Filter
|
||||
discipline: Optional[str] = None
|
||||
target_group: Optional[str] = None
|
||||
age_group: Optional[str] = None
|
||||
max_duration: Optional[int] = Field(default=None, ge=0)
|
||||
|
||||
# Listen-Filter
|
||||
equipment_any: Optional[List[str]] = None # mindestens eins muss passen
|
||||
equipment_all: Optional[List[str]] = None # alle müssen passen
|
||||
keywords_any: Optional[List[str]] = None
|
||||
keywords_all: Optional[List[str]] = None
|
||||
|
||||
# Capabilities (Namen + Level-Operator)
|
||||
capability_names: Optional[List[str]] = None
|
||||
capability_ge_level: Optional[int] = Field(default=None, ge=1, le=5)
|
||||
capability_eq_level: Optional[int] = Field(default=None, ge=1, le=5)
|
||||
|
||||
class ExerciseSearchHit(BaseModel):
|
||||
id: str
|
||||
score: Optional[float] = None
|
||||
payload: Exercise
|
||||
|
||||
class ExerciseSearchResponse(BaseModel):
|
||||
hits: List[ExerciseSearchHit]
|
||||
|
||||
# =========================
|
||||
# Helpers
|
||||
# =========================
|
||||
COLLECTION = os.getenv("EXERCISE_COLLECTION", "exercises")
|
||||
|
||||
# CHANGED: Factorized to reuse for both create and update
|
||||
|
||||
def _ensure_collection():
|
||||
if not qdrant.collection_exists(COLLECTION):
|
||||
qdrant.recreate_collection(
|
||||
@@ -71,73 +115,181 @@ def _ensure_collection():
|
||||
),
|
||||
)
|
||||
|
||||
# NEW: gemeinsamer Helper für external_id-Lookup
|
||||
|
||||
def _lookup_by_external_id(external_id: str) -> Optional[Dict[str, Any]]:
|
||||
_ensure_collection()
|
||||
flt = Filter(must=[FieldCondition(key="external_id", match=MatchValue(value=external_id))])
|
||||
pts, _ = qdrant.scroll(collection_name=COLLECTION, scroll_filter=flt, limit=1)
|
||||
pts, _ = qdrant.scroll(
|
||||
collection_name=COLLECTION,
|
||||
scroll_filter=flt,
|
||||
limit=1,
|
||||
with_payload=True,
|
||||
)
|
||||
if not pts:
|
||||
return None
|
||||
# qdrant_client liefert PointStruct; wir geben die payload + id zurück
|
||||
doc = pts[0].payload or {}
|
||||
doc = dict(doc)
|
||||
doc = dict(pts[0].payload or {})
|
||||
doc.setdefault("id", str(pts[0].id))
|
||||
return doc
|
||||
|
||||
# NEW: konsistente Embedding-Erzeugung
|
||||
_def_embed_text_fields = ("title", "summary", "short_description", "purpose", "execution", "notes")
|
||||
|
||||
def _make_vector(ex: Exercise) -> List[float]:
|
||||
text = ". ".join([getattr(ex, f, "") for f in _def_embed_text_fields if getattr(ex, f, None)])
|
||||
# Achtung: model.encode muss synchron sein; sonst async anpassen
|
||||
vec = model.encode(text).tolist()
|
||||
return vec
|
||||
_DEF_EMBED_FIELDS = ("title", "summary", "short_description", "purpose", "execution", "notes")
|
||||
|
||||
|
||||
def _make_vector_from_exercise(ex: Exercise) -> List[float]:
|
||||
text = ". ".join([getattr(ex, f, "") for f in _DEF_EMBED_FIELDS if getattr(ex, f, None)])
|
||||
return model.encode(text).tolist()
|
||||
|
||||
|
||||
def _make_vector_from_query(query: str) -> List[float]:
|
||||
return model.encode(query).tolist()
|
||||
|
||||
|
||||
def _norm_list(xs: List[Any]) -> List[str]:
|
||||
out = []
|
||||
seen = set()
|
||||
for x in xs or []:
|
||||
s = str(x).strip()
|
||||
if not s:
|
||||
continue
|
||||
key = s.casefold()
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
out.append(s)
|
||||
return sorted(out, key=str.casefold)
|
||||
|
||||
|
||||
def _facet_capabilities(caps: Dict[str, Any]) -> Dict[str, List[str]]:
|
||||
caps = caps or {}
|
||||
|
||||
def names_where(pred) -> List[str]:
|
||||
out = []
|
||||
for k, v in caps.items():
|
||||
try:
|
||||
iv = int(v)
|
||||
except Exception:
|
||||
iv = 0
|
||||
if pred(iv):
|
||||
t = str(k).strip()
|
||||
if t:
|
||||
out.append(t)
|
||||
return sorted({t for t in out}, key=str.casefold)
|
||||
|
||||
all_keys = sorted({str(k).strip() for k in caps.keys() if str(k).strip()}, key=str.casefold)
|
||||
return {
|
||||
"capability_keys": all_keys,
|
||||
# >= N
|
||||
"capability_ge1": names_where(lambda lv: lv >= 1),
|
||||
"capability_ge2": names_where(lambda lv: lv >= 2),
|
||||
"capability_ge3": names_where(lambda lv: lv >= 3),
|
||||
"capability_ge4": names_where(lambda lv: lv >= 4),
|
||||
"capability_ge5": names_where(lambda lv: lv >= 5),
|
||||
# == N
|
||||
"capability_eq1": names_where(lambda lv: lv == 1),
|
||||
"capability_eq2": names_where(lambda lv: lv == 2),
|
||||
"capability_eq3": names_where(lambda lv: lv == 3),
|
||||
"capability_eq4": names_where(lambda lv: lv == 4),
|
||||
"capability_eq5": names_where(lambda lv: lv == 5),
|
||||
}
|
||||
|
||||
|
||||
def _response_strip_extras(payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||
allowed = set(Exercise.model_fields.keys())
|
||||
return {k: v for k, v in payload.items() if k in allowed}
|
||||
|
||||
|
||||
def _build_filter(req: ExerciseSearchRequest) -> Filter:
|
||||
must: List[Any] = []
|
||||
should: List[Any] = []
|
||||
|
||||
if req.discipline:
|
||||
must.append(FieldCondition(key="discipline", match=MatchValue(value=req.discipline)))
|
||||
if req.target_group:
|
||||
must.append(FieldCondition(key="target_group", match=MatchValue(value=req.target_group)))
|
||||
if req.age_group:
|
||||
must.append(FieldCondition(key="age_group", match=MatchValue(value=req.age_group)))
|
||||
if req.max_duration is not None:
|
||||
# Range ohne Import zusätzlicher Modelle: Qdrant akzeptiert auch {'range': {'lte': n}} per JSON;
|
||||
# über Client-Modell tun wir es hier nicht, da wir Filter primär für Keyword-Felder nutzen.
|
||||
must.append({"key": "duration_minutes", "range": {"lte": int(req.max_duration)}})
|
||||
|
||||
# equipment
|
||||
if req.equipment_all:
|
||||
for it in req.equipment_all:
|
||||
must.append(FieldCondition(key="equipment", match=MatchValue(value=it)))
|
||||
if req.equipment_any:
|
||||
# OR: über 'should' Liste
|
||||
for it in req.equipment_any:
|
||||
should.append(FieldCondition(key="equipment", match=MatchValue(value=it)))
|
||||
|
||||
# keywords
|
||||
if req.keywords_all:
|
||||
for it in req.keywords_all:
|
||||
must.append(FieldCondition(key="keywords", match=MatchValue(value=it)))
|
||||
if req.keywords_any:
|
||||
for it in req.keywords_any:
|
||||
should.append(FieldCondition(key="keywords", match=MatchValue(value=it)))
|
||||
|
||||
# capabilities (ge/eq)
|
||||
if req.capability_names:
|
||||
names = [s for s in req.capability_names if s and s.strip()]
|
||||
if req.capability_eq_level:
|
||||
key = f"capability_eq{int(req.capability_eq_level)}"
|
||||
for n in names:
|
||||
must.append(FieldCondition(key=key, match=MatchValue(value=n)))
|
||||
elif req.capability_ge_level:
|
||||
key = f"capability_ge{int(req.capability_ge_level)}"
|
||||
for n in names:
|
||||
must.append(FieldCondition(key=key, match=MatchValue(value=n)))
|
||||
else:
|
||||
# Default: Level >=1 (alle vorhanden)
|
||||
for n in names:
|
||||
must.append(FieldCondition(key="capability_ge1", match=MatchValue(value=n)))
|
||||
|
||||
flt = Filter(must=must)
|
||||
if should:
|
||||
# qdrant: 'should' mit implizitem minimum_should_match=1
|
||||
flt.should = should
|
||||
return flt
|
||||
|
||||
# =========================
|
||||
# Endpoints
|
||||
# =========================
|
||||
@router.get("/exercise/by-external-id") # NEW
|
||||
@router.get("/exercise/by-external-id")
|
||||
def get_exercise_by_external_id(external_id: str = Query(..., min_length=3)):
|
||||
"""Lookup für Idempotenz im Importer. Liefert 404, wenn nicht vorhanden."""
|
||||
found = _lookup_by_external_id(external_id)
|
||||
if not found:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
return found
|
||||
|
||||
|
||||
@router.post("/exercise", response_model=Exercise)
|
||||
def create_or_update_exercise(ex: Exercise):
|
||||
"""
|
||||
CHANGED: Upsert-Semantik. Wenn `external_id` existiert und bereits in Qdrant gefunden wird,
|
||||
wird dieselbe Point-ID überschrieben (echtes Update). Ansonsten neuer Eintrag.
|
||||
API-Signatur bleibt identisch (POST /exercise, Body = Exercise).
|
||||
"""
|
||||
_ensure_collection()
|
||||
|
||||
# Default: neue Point-ID aus dem Exercise-Objekt
|
||||
point_id = ex.id
|
||||
|
||||
# Wenn external_id gesetzt → prüfen, ob bereits vorhanden → Point-ID übernehmen
|
||||
if ex.external_id:
|
||||
prior = _lookup_by_external_id(ex.external_id)
|
||||
if prior:
|
||||
point_id = prior.get("id", point_id)
|
||||
|
||||
# Embedding berechnen
|
||||
vector = _make_vector(ex)
|
||||
vector = _make_vector_from_exercise(ex)
|
||||
|
||||
# Payload synchronisieren (id == point_id)
|
||||
payload = ex.dict()
|
||||
payload: Dict[str, Any] = ex.model_dump()
|
||||
payload["id"] = str(point_id)
|
||||
payload["keywords"] = _norm_list(payload.get("keywords") or [])
|
||||
payload["equipment"] = _norm_list(payload.get("equipment") or [])
|
||||
|
||||
payload.update(_facet_capabilities(payload.get("capabilities") or {}))
|
||||
|
||||
# Upsert in Qdrant
|
||||
qdrant.upsert(
|
||||
collection_name=COLLECTION,
|
||||
points=[PointStruct(id=str(point_id), vector=vector, payload=payload)],
|
||||
)
|
||||
|
||||
return Exercise(**payload)
|
||||
return Exercise(**_response_strip_extras(payload))
|
||||
|
||||
|
||||
# (Optional) – Einzel-Abruf per ID (falls bereits vorhanden, unverändert)
|
||||
@router.get("/exercise/{exercise_id}", response_model=Exercise)
|
||||
def get_exercise(exercise_id: str):
|
||||
_ensure_collection()
|
||||
@@ -145,28 +297,119 @@ def get_exercise(exercise_id: str):
|
||||
collection_name=COLLECTION,
|
||||
scroll_filter=Filter(must=[FieldCondition(key="id", match=MatchValue(value=exercise_id))]),
|
||||
limit=1,
|
||||
with_payload=True,
|
||||
)
|
||||
if not pts:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
payload = dict(pts[0].payload or {})
|
||||
payload.setdefault("id", str(pts[0].id))
|
||||
return Exercise(**payload)
|
||||
return Exercise(**_response_strip_extras(payload))
|
||||
|
||||
|
||||
@router.post("/exercise/search", response_model=ExerciseSearchResponse)
|
||||
def search_exercises(req: ExerciseSearchRequest) -> ExerciseSearchResponse:
|
||||
_ensure_collection()
|
||||
flt = _build_filter(req)
|
||||
|
||||
hits: List[ExerciseSearchHit] = []
|
||||
if req.query:
|
||||
vec = _make_vector_from_query(req.query)
|
||||
# qdrant_client.search unterstützt offset/limit
|
||||
res = qdrant.search(
|
||||
collection_name=COLLECTION,
|
||||
query_vector=vec,
|
||||
limit=req.limit,
|
||||
offset=req.offset,
|
||||
query_filter=flt,
|
||||
)
|
||||
for h in res:
|
||||
payload = dict(h.payload or {})
|
||||
payload.setdefault("id", str(h.id))
|
||||
hits.append(ExerciseSearchHit(id=str(h.id), score=float(h.score or 0.0), payload=Exercise(**_response_strip_extras(payload))))
|
||||
else:
|
||||
# Filter-only: per Scroll (ohne Score); einfache Paginierung via offset/limit
|
||||
# Hole offset+limit Punkte und simuliere Score=None
|
||||
collected = 0
|
||||
skipped = 0
|
||||
next_offset = None
|
||||
while collected < req.limit:
|
||||
page, next_offset = qdrant.scroll(
|
||||
collection_name=COLLECTION,
|
||||
scroll_filter=flt,
|
||||
offset=next_offset,
|
||||
limit=max(1, min(256, req.limit - collected + req.offset - skipped)),
|
||||
with_payload=True,
|
||||
)
|
||||
if not page:
|
||||
break
|
||||
for pt in page:
|
||||
if skipped < req.offset:
|
||||
skipped += 1
|
||||
continue
|
||||
payload = dict(pt.payload or {})
|
||||
payload.setdefault("id", str(pt.id))
|
||||
hits.append(ExerciseSearchHit(id=str(pt.id), score=None, payload=Exercise(**_response_strip_extras(payload))))
|
||||
collected += 1
|
||||
if collected >= req.limit:
|
||||
break
|
||||
if next_offset is None:
|
||||
break
|
||||
return ExerciseSearchResponse(hits=hits)
|
||||
|
||||
|
||||
# Bestehende Admin-Utilities (Delete nach Filter / komplette Collection) – unverändert außer Nutzung von CONSTs
|
||||
@router.delete("/exercise/delete-by-external-id", response_model=DeleteResponse)
|
||||
def delete_by_external_id(external_id: str = Query(...)):
|
||||
_ensure_collection()
|
||||
flt = Filter(must=[FieldCondition(key="external_id", match=MatchValue(value=external_id))])
|
||||
pts, _ = qdrant.scroll(collection_name=COLLECTION, scroll_filter=flt, limit=10000)
|
||||
pts, _ = qdrant.scroll(collection_name=COLLECTION, scroll_filter=flt, limit=10000, with_payload=False)
|
||||
ids = [str(p.id) for p in pts]
|
||||
if not ids:
|
||||
return DeleteResponse(status="🔍 Keine Einträge gefunden.", count=0, collection=COLLECTION)
|
||||
qdrant.delete(collection_name=COLLECTION, points_selector=PointIdsList(points=ids))
|
||||
return DeleteResponse(status="🗑️ gelöscht", count=len(ids), collection=COLLECTION)
|
||||
|
||||
|
||||
@router.delete("/exercise/delete-collection", response_model=DeleteResponse)
|
||||
def delete_collection(collection: str = Query(default=COLLECTION)):
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
qdrant.delete_collection(collection_name=collection)
|
||||
return DeleteResponse(status="🗑️ gelöscht", count=0, collection=collection)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# OPTIONAL: einfacher Selbsttest (kannst du auch separat als Script verwenden)
|
||||
# ---------------------------
|
||||
TEST_DOC = """
|
||||
Speicher als tests/test_exercise_search.py und mit pytest laufen lassen.
|
||||
|
||||
import os, requests
|
||||
|
||||
BASE = os.getenv("API_BASE", "http://localhost:8000")
|
||||
|
||||
# 1) Filter-only
|
||||
r = requests.post(f"{BASE}/exercise/search", json={
|
||||
"discipline": "Karate",
|
||||
"max_duration": 12,
|
||||
"equipment_any": ["Bälle"],
|
||||
"capability_names": ["Reaktionsfähigkeit"],
|
||||
"capability_ge_level": 2,
|
||||
"limit": 5
|
||||
})
|
||||
r.raise_for_status()
|
||||
js = r.json()
|
||||
assert "hits" in js
|
||||
for h in js["hits"]:
|
||||
p = h["payload"]
|
||||
assert p["discipline"] == "Karate"
|
||||
assert p["duration_minutes"] <= 12
|
||||
|
||||
# 2) Vector + Filter
|
||||
r = requests.post(f"{BASE}/exercise/search", json={
|
||||
"query": "Aufwärmen 10min, Reaktionsfähigkeit, Teenager, Bälle",
|
||||
"discipline": "Karate",
|
||||
"limit": 3
|
||||
})
|
||||
r.raise_for_status()
|
||||
js = r.json(); assert len(js["hits"]) <= 3
|
||||
"""
|
||||
@@ -8,6 +8,8 @@ from clients import model, qdrant
|
||||
from wiki_router import router as wiki_router
|
||||
from embed_router import router as embed_router
|
||||
from exercise_router import router as exercise_router
|
||||
from plan_router import router as plan_router
|
||||
from plan_session_router import router as plan_session_router
|
||||
|
||||
# Version
|
||||
__version__ = "1.1.6"
|
||||
@@ -31,3 +33,5 @@ async def unicorn_exception_handler(request, exc):
|
||||
app.include_router(wiki_router)
|
||||
app.include_router(embed_router)
|
||||
app.include_router(exercise_router)
|
||||
app.include_router(plan_router)
|
||||
app.include_router(plan_session_router)
|
||||
@@ -0,0 +1,521 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
plan_router.py – v0.13.4 (WP-15)
|
||||
|
||||
Änderungen ggü. v0.13.3
|
||||
- Idempotenter POST /plan: Wenn ein Plan mit gleichem Fingerprint existiert und die neue
|
||||
Anfrage ein späteres `created_at` trägt, wird der gespeicherte Plan mit dem neueren
|
||||
`created_at` und `created_at_ts` aktualisiert (kein Duplikat, aber zeitlich „frisch“).
|
||||
- /plans: Mehrseitiges Scrollen bleibt aktiv; Zeitfenster-Filter robust (serverseitig + Fallback).
|
||||
"""
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import List, Optional, Dict, Any
|
||||
from uuid import uuid4
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
|
||||
from clients import model, qdrant
|
||||
from qdrant_client.models import (
|
||||
PointStruct, Filter, FieldCondition, MatchValue,
|
||||
VectorParams, Distance, Range
|
||||
)
|
||||
|
||||
router = APIRouter(tags=["plans"])
|
||||
|
||||
# -----------------
|
||||
# Konfiguration
|
||||
# -----------------
|
||||
PLAN_COLLECTION = os.getenv("PLAN_COLLECTION") or os.getenv("QDRANT_COLLECTION_PLANS", "plans")
|
||||
PLAN_TEMPLATE_COLLECTION = os.getenv("PLAN_TEMPLATE_COLLECTION", "plan_templates")
|
||||
PLAN_SESSION_COLLECTION = os.getenv("PLAN_SESSION_COLLECTION", "plan_sessions")
|
||||
EXERCISE_COLLECTION = os.getenv("EXERCISE_COLLECTION", "exercises")
|
||||
|
||||
# -----------------
|
||||
# Modelle
|
||||
# -----------------
|
||||
class TemplateSection(BaseModel):
|
||||
name: str
|
||||
target_minutes: int
|
||||
must_keywords: List[str] = []
|
||||
ideal_keywords: List[str] = []
|
||||
supplement_keywords: List[str] = []
|
||||
forbid_keywords: List[str] = []
|
||||
capability_targets: Dict[str, int] = {}
|
||||
|
||||
class PlanTemplate(BaseModel):
|
||||
id: str = Field(default_factory=lambda: str(uuid4()))
|
||||
name: str
|
||||
discipline: str
|
||||
age_group: str
|
||||
target_group: str
|
||||
total_minutes: int
|
||||
sections: List[TemplateSection] = []
|
||||
goals: List[str] = []
|
||||
equipment_allowed: List[str] = []
|
||||
created_by: str
|
||||
version: str
|
||||
|
||||
class PlanItem(BaseModel):
|
||||
exercise_external_id: str
|
||||
duration: int
|
||||
why: str
|
||||
|
||||
class PlanSection(BaseModel):
|
||||
name: str
|
||||
items: List[PlanItem] = []
|
||||
minutes: int
|
||||
|
||||
class Plan(BaseModel):
|
||||
id: str = Field(default_factory=lambda: str(uuid4()))
|
||||
template_id: Optional[str] = None
|
||||
title: str
|
||||
discipline: str
|
||||
age_group: str
|
||||
target_group: str
|
||||
total_minutes: int
|
||||
sections: List[PlanSection] = []
|
||||
goals: List[str] = []
|
||||
capability_summary: Dict[str, int] = {}
|
||||
novelty_against_last_n: Optional[float] = None
|
||||
fingerprint: Optional[str] = None
|
||||
created_by: str
|
||||
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
||||
source: str = "API"
|
||||
|
||||
class PlanTemplateList(BaseModel):
|
||||
items: List[PlanTemplate]
|
||||
limit: int
|
||||
offset: int
|
||||
count: int
|
||||
|
||||
class PlanList(BaseModel):
|
||||
items: List[Plan]
|
||||
limit: int
|
||||
offset: int
|
||||
count: int
|
||||
|
||||
# -----------------
|
||||
# Helpers
|
||||
# -----------------
|
||||
def _ensure_collection(name: str):
|
||||
if not qdrant.collection_exists(name):
|
||||
qdrant.recreate_collection(
|
||||
collection_name=name,
|
||||
vectors_config=VectorParams(size=model.get_sentence_embedding_dimension(), distance=Distance.COSINE),
|
||||
)
|
||||
|
||||
def _norm_list(xs: List[str]) -> List[str]:
|
||||
seen, out = set(), []
|
||||
for x in xs or []:
|
||||
s = str(x).strip()
|
||||
k = s.casefold()
|
||||
if s and k not in seen:
|
||||
seen.add(k)
|
||||
out.append(s)
|
||||
return sorted(out, key=str.casefold)
|
||||
|
||||
def _template_embed_text(tpl: PlanTemplate) -> str:
|
||||
parts = [tpl.name, tpl.discipline, tpl.age_group, tpl.target_group]
|
||||
parts += tpl.goals
|
||||
parts += [s.name for s in tpl.sections]
|
||||
return ". ".join([p for p in parts if p])
|
||||
|
||||
def _plan_embed_text(p: Plan) -> str:
|
||||
parts = [p.title, p.discipline, p.age_group, p.target_group]
|
||||
parts += p.goals
|
||||
parts += [s.name for s in p.sections]
|
||||
return ". ".join([p for p in parts if p])
|
||||
|
||||
def _embed(text: str):
|
||||
return model.encode(text or "").tolist()
|
||||
|
||||
def _fingerprint_for_plan(p: Plan) -> str:
|
||||
"""sha256(title, total_minutes, sections.items.exercise_external_id, sections.items.duration)"""
|
||||
core = {
|
||||
"title": p.title,
|
||||
"total_minutes": int(p.total_minutes),
|
||||
"items": [
|
||||
{"exercise_external_id": it.exercise_external_id, "duration": int(it.duration)}
|
||||
for sec in p.sections
|
||||
for it in (sec.items or [])
|
||||
],
|
||||
}
|
||||
raw = json.dumps(core, sort_keys=True, ensure_ascii=False)
|
||||
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
|
||||
|
||||
def _get_by_field(collection: str, key: str, value: Any):
|
||||
flt = Filter(must=[FieldCondition(key=key, match=MatchValue(value=value))])
|
||||
pts, _ = qdrant.scroll(collection_name=collection, scroll_filter=flt, limit=1, with_payload=True)
|
||||
if not pts:
|
||||
return None
|
||||
point = pts[0]
|
||||
payload = dict(point.payload or {})
|
||||
payload.setdefault("id", str(point.id))
|
||||
return {"id": point.id, "payload": payload}
|
||||
|
||||
def _as_model(model_cls, payload: Dict[str, Any]):
|
||||
fields = getattr(model_cls, "model_fields", None) or getattr(model_cls, "__fields__", {})
|
||||
allowed = set(fields.keys())
|
||||
data = {k: payload[k] for k in payload.keys() if k in allowed}
|
||||
return model_cls(**data)
|
||||
|
||||
def _truthy(val: Optional[str]) -> bool:
|
||||
return str(val or "").strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
def _exists_in_collection(collection: str, key: str, value: Any) -> bool:
|
||||
flt = Filter(must=[FieldCondition(key=key, match=MatchValue(value=value))])
|
||||
pts, _ = qdrant.scroll(collection_name=collection, scroll_filter=flt, limit=1, with_payload=False)
|
||||
return bool(pts)
|
||||
|
||||
def _parse_iso_to_ts(iso_str: str) -> float:
|
||||
try:
|
||||
return float(datetime.fromisoformat(iso_str.replace("Z", "+00:00")).timestamp())
|
||||
except Exception:
|
||||
return float(datetime.now(timezone.utc).timestamp())
|
||||
|
||||
def _scroll_collect(collection: str, flt: Optional[Filter], need: int, page: int = 256):
|
||||
out = []
|
||||
offset = None
|
||||
page = max(1, min(page, 1024))
|
||||
while len(out) < need:
|
||||
pts, offset = qdrant.scroll(collection_name=collection, scroll_filter=flt, limit=min(page, need - len(out)), with_payload=True, offset=offset)
|
||||
if not pts:
|
||||
break
|
||||
out.extend(pts)
|
||||
if offset is None:
|
||||
break
|
||||
return out
|
||||
|
||||
# -----------------
|
||||
# Endpoints: Templates
|
||||
# -----------------
|
||||
@router.post(
|
||||
"/plan_templates",
|
||||
response_model=PlanTemplate,
|
||||
summary="Create a plan template",
|
||||
description=(
|
||||
"Erstellt ein Plan-Template (Strukturplanung).\n\n"
|
||||
"• Mehrere Sections erlaubt.\n"
|
||||
"• Section-Felder: must/ideal/supplement/forbid keywords + capability_targets.\n"
|
||||
"• Materialisierte Facettenfelder (section_*) werden intern geschrieben, um Qdrant-Filter zu beschleunigen."
|
||||
),
|
||||
)
|
||||
def create_plan_template(t: PlanTemplate):
|
||||
_ensure_collection(PLAN_TEMPLATE_COLLECTION)
|
||||
payload = t.model_dump()
|
||||
payload["goals"] = _norm_list(payload.get("goals"))
|
||||
sections = payload.get("sections", []) or []
|
||||
for s in sections:
|
||||
s["must_keywords"] = _norm_list(s.get("must_keywords") or [])
|
||||
s["ideal_keywords"] = _norm_list(s.get("ideal_keywords") or [])
|
||||
s["supplement_keywords"] = _norm_list(s.get("supplement_keywords") or [])
|
||||
s["forbid_keywords"] = _norm_list(s.get("forbid_keywords") or [])
|
||||
|
||||
# Materialisierte Facetten (KEYWORD-Indizes)
|
||||
payload["section_names"] = _norm_list([s.get("name", "") for s in sections])
|
||||
payload["section_must_keywords"] = _norm_list([kw for s in sections for kw in (s.get("must_keywords") or [])])
|
||||
payload["section_ideal_keywords"] = _norm_list([kw for s in sections for kw in (s.get("ideal_keywords") or [])])
|
||||
payload["section_supplement_keywords"] = _norm_list([kw for s in sections for kw in (s.get("supplement_keywords") or [])])
|
||||
payload["section_forbid_keywords"] = _norm_list([kw for s in sections for kw in (s.get("forbid_keywords") or [])])
|
||||
|
||||
vec = _embed(_template_embed_text(t))
|
||||
qdrant.upsert(collection_name=PLAN_TEMPLATE_COLLECTION, points=[PointStruct(id=str(t.id), vector=vec, payload=payload)])
|
||||
return t
|
||||
|
||||
@router.get(
|
||||
"/plan_templates/{tpl_id}",
|
||||
response_model=PlanTemplate,
|
||||
summary="Read a plan template by id",
|
||||
description="Liest ein Template anhand seiner ID und gibt nur die Schemafelder zurück (zusätzliche Payload wird herausgefiltert).",
|
||||
)
|
||||
def get_plan_template(tpl_id: str):
|
||||
_ensure_collection(PLAN_TEMPLATE_COLLECTION)
|
||||
found = _get_by_field(PLAN_TEMPLATE_COLLECTION, "id", tpl_id)
|
||||
if not found:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
return _as_model(PlanTemplate, found["payload"])
|
||||
|
||||
@router.get(
|
||||
"/plan_templates",
|
||||
response_model=PlanTemplateList,
|
||||
summary="List plan templates (filterable)",
|
||||
description=(
|
||||
"Listet Plan-Templates mit Filtern.\n\n"
|
||||
"**Filter** (exakte Matches, KEYWORD-Felder):\n"
|
||||
"- discipline, age_group, target_group\n"
|
||||
"- section: Section-Name (nutzt materialisierte `section_names`)\n"
|
||||
"- goal: Ziel (nutzt `goals`)\n"
|
||||
"- keyword: trifft auf beliebige Section-Keyword-Felder (must/ideal/supplement/forbid).\n\n"
|
||||
"**Pagination:** limit/offset. Feld `count` entspricht der Anzahl zurückgegebener Items (keine Gesamtsumme)."
|
||||
),
|
||||
)
|
||||
def list_plan_templates(
|
||||
discipline: Optional[str] = Query(None, description="Filter: Disziplin (exaktes KEYWORD-Match)", example="Karate"),
|
||||
age_group: Optional[str] = Query(None, description="Filter: Altersgruppe", example="Teenager"),
|
||||
target_group: Optional[str] = Query(None, description="Filter: Zielgruppe", example="Breitensport"),
|
||||
section: Optional[str] = Query(None, description="Filter: Section-Name (materialisiert)", example="Warmup"),
|
||||
goal: Optional[str] = Query(None, description="Filter: Trainingsziel", example="Technik"),
|
||||
keyword: Optional[str] = Query(None, description="Filter: Keyword in must/ideal/supplement/forbid", example="Koordination"),
|
||||
limit: int = Query(20, ge=1, le=200, description="Max. Anzahl Items"),
|
||||
offset: int = Query(0, ge=0, description="Start-Offset für Paging"),
|
||||
):
|
||||
_ensure_collection(PLAN_TEMPLATE_COLLECTION)
|
||||
must: List[Any] = []
|
||||
should: List[Any] = []
|
||||
if discipline:
|
||||
must.append(FieldCondition(key="discipline", match=MatchValue(value=discipline)))
|
||||
if age_group:
|
||||
must.append(FieldCondition(key="age_group", match=MatchValue(value=age_group)))
|
||||
if target_group:
|
||||
must.append(FieldCondition(key="target_group", match=MatchValue(value=target_group)))
|
||||
if section:
|
||||
must.append(FieldCondition(key="section_names", match=MatchValue(value=section)))
|
||||
if goal:
|
||||
must.append(FieldCondition(key="goals", match=MatchValue(value=goal)))
|
||||
if keyword:
|
||||
for k in ("section_must_keywords","section_ideal_keywords","section_supplement_keywords","section_forbid_keywords"):
|
||||
should.append(FieldCondition(key=k, match=MatchValue(value=keyword)))
|
||||
|
||||
flt = Filter(must=must or None, should=should or None) if (must or should) else None
|
||||
need = max(offset + limit, 1)
|
||||
pts = _scroll_collect(PLAN_TEMPLATE_COLLECTION, flt, need)
|
||||
items: List[PlanTemplate] = []
|
||||
for p in pts[offset:offset+limit]:
|
||||
payload = dict(p.payload or {})
|
||||
payload.setdefault("id", str(p.id))
|
||||
items.append(_as_model(PlanTemplate, payload))
|
||||
return PlanTemplateList(items=items, limit=limit, offset=offset, count=len(items))
|
||||
|
||||
# -----------------
|
||||
# Endpoints: Pläne
|
||||
# -----------------
|
||||
@router.post(
|
||||
"/plan",
|
||||
response_model=Plan,
|
||||
summary="Create a concrete training plan",
|
||||
description=(
|
||||
"Erstellt einen konkreten Trainingsplan.\n\n"
|
||||
"Idempotenz: gleicher Fingerprint (title + items) → gleicher Plan (kein Duplikat).\n"
|
||||
"Bei erneutem POST mit späterem `created_at` wird `created_at`/`created_at_ts` des bestehenden Plans aktualisiert."
|
||||
),
|
||||
)
|
||||
def create_plan(p: Plan):
|
||||
_ensure_collection(PLAN_COLLECTION)
|
||||
|
||||
# Template-Referenz prüfen (falls gesetzt)
|
||||
if p.template_id:
|
||||
if not _exists_in_collection(PLAN_TEMPLATE_COLLECTION, "id", p.template_id):
|
||||
raise HTTPException(status_code=422, detail=f"Unknown template_id: {p.template_id}")
|
||||
|
||||
# Optional: Strict-Mode – Exercises gegen EXERCISE_COLLECTION prüfen
|
||||
if _truthy(os.getenv("PLAN_STRICT_EXERCISES")):
|
||||
missing: List[str] = []
|
||||
for sec in p.sections or []:
|
||||
for it in sec.items or []:
|
||||
exid = (it.exercise_external_id or "").strip()
|
||||
if exid and not _exists_in_collection(EXERCISE_COLLECTION, "external_id", exid):
|
||||
missing.append(exid)
|
||||
if missing:
|
||||
raise HTTPException(status_code=422, detail={"error": "unknown exercise_external_id", "missing": sorted(set(missing))})
|
||||
|
||||
# Fingerprint
|
||||
fp = _fingerprint_for_plan(p)
|
||||
p.fingerprint = p.fingerprint or fp
|
||||
|
||||
# Ziel-ISO + TS aus Request berechnen (auch wenn Duplikat)
|
||||
req_payload = p.model_dump()
|
||||
dt = req_payload.get("created_at")
|
||||
if isinstance(dt, datetime):
|
||||
dt = dt.astimezone(timezone.utc).isoformat()
|
||||
elif isinstance(dt, str):
|
||||
try:
|
||||
_ = datetime.fromisoformat(dt.replace("Z", "+00:00"))
|
||||
except Exception:
|
||||
dt = datetime.now(timezone.utc).isoformat()
|
||||
else:
|
||||
dt = datetime.now(timezone.utc).isoformat()
|
||||
req_payload["created_at"] = dt
|
||||
req_ts = _parse_iso_to_ts(dt)
|
||||
req_payload["created_at_ts"] = float(req_ts)
|
||||
|
||||
# Dup-Check
|
||||
existing = _get_by_field(PLAN_COLLECTION, "fingerprint", p.fingerprint)
|
||||
if existing:
|
||||
# Falls neues created_at später ist → gespeicherten Plan aktualisieren
|
||||
cur = existing["payload"]
|
||||
cur_ts = cur.get("created_at_ts")
|
||||
if cur_ts is None:
|
||||
cur_ts = _parse_iso_to_ts(str(cur.get("created_at", dt)))
|
||||
if req_ts > float(cur_ts):
|
||||
try:
|
||||
qdrant.set_payload(
|
||||
collection_name=PLAN_COLLECTION,
|
||||
payload={"created_at": req_payload["created_at"], "created_at_ts": req_payload["created_at_ts"]},
|
||||
points=[existing["id"]],
|
||||
)
|
||||
# Antwort-Objekt aktualisieren
|
||||
cur["created_at"] = req_payload["created_at"]
|
||||
cur["created_at_ts"] = req_payload["created_at_ts"]
|
||||
except Exception:
|
||||
pass
|
||||
return _as_model(Plan, cur)
|
||||
|
||||
# Neu anlegen
|
||||
p.goals = _norm_list(p.goals)
|
||||
payload = req_payload # enthält bereits korrektes created_at + created_at_ts
|
||||
payload.update({
|
||||
"id": p.id,
|
||||
"template_id": p.template_id,
|
||||
"title": p.title,
|
||||
"discipline": p.discipline,
|
||||
"age_group": p.age_group,
|
||||
"target_group": p.target_group,
|
||||
"total_minutes": p.total_minutes,
|
||||
"sections": [s.model_dump() for s in p.sections],
|
||||
"goals": _norm_list(p.goals),
|
||||
"capability_summary": p.capability_summary,
|
||||
"novelty_against_last_n": p.novelty_against_last_n,
|
||||
"fingerprint": p.fingerprint,
|
||||
"created_by": p.created_by,
|
||||
"source": p.source,
|
||||
})
|
||||
# Section-Namen materialisieren
|
||||
payload["plan_section_names"] = _norm_list([ (s.get("name") or "").strip() for s in (payload.get("sections") or []) if isinstance(s, dict) ])
|
||||
|
||||
vec = _embed(_plan_embed_text(p))
|
||||
qdrant.upsert(collection_name=PLAN_COLLECTION, points=[PointStruct(id=str(p.id), vector=vec, payload=payload)])
|
||||
return p
|
||||
|
||||
@router.get(
|
||||
"/plan/{plan_id}",
|
||||
response_model=Plan,
|
||||
summary="Read a training plan by id",
|
||||
description="Liest einen Plan anhand seiner ID. `created_at` wird (falls ISO-String) zu `datetime` geparst.",
|
||||
)
|
||||
def get_plan(plan_id: str):
|
||||
_ensure_collection(PLAN_COLLECTION)
|
||||
found = _get_by_field(PLAN_COLLECTION, "id", plan_id)
|
||||
if not found:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
payload = found["payload"]
|
||||
if isinstance(payload.get("created_at"), str):
|
||||
try:
|
||||
payload["created_at"] = datetime.fromisoformat(payload["created_at"])
|
||||
except Exception:
|
||||
pass
|
||||
return _as_model(Plan, payload)
|
||||
|
||||
@router.get(
|
||||
"/plans",
|
||||
response_model=PlanList,
|
||||
summary="List training plans (filterable)",
|
||||
description=(
|
||||
"Listet Trainingspläne mit Filtern.\n\n"
|
||||
"**Filter** (exakte Matches, KEYWORD-Felder):\n"
|
||||
"- created_by, discipline, age_group, target_group, goal\n"
|
||||
"- section: Section-Name (nutzt materialisiertes `plan_section_names`)\n"
|
||||
"- created_from / created_to: ISO-8601 Zeitfenster → serverseitiger Range-Filter über `created_at_ts` (FLOAT). "
|
||||
"Falls 0 Treffer: zweiter Durchlauf ohne Zeit-Range + lokale Zeitprüfung.\n\n"
|
||||
"**Pagination:** limit/offset. Feld `count` entspricht der Anzahl zurückgegebener Items (keine Gesamtsumme)."
|
||||
),
|
||||
)
|
||||
def list_plans(
|
||||
created_by: Optional[str] = Query(None, description="Filter: Ersteller", example="tester"),
|
||||
discipline: Optional[str] = Query(None, description="Filter: Disziplin", example="Karate"),
|
||||
age_group: Optional[str] = Query(None, description="Filter: Altersgruppe", example="Teenager"),
|
||||
target_group: Optional[str] = Query(None, description="Filter: Zielgruppe", example="Breitensport"),
|
||||
goal: Optional[str] = Query(None, description="Filter: Trainingsziel", example="Technik"),
|
||||
section: Optional[str] = Query(None, description="Filter: Section-Name", example="Warmup"),
|
||||
created_from: Optional[str] = Query(None, description="Ab-Zeitpunkt (ISO 8601, z. B. 2025-08-12T00:00:00Z)", example="2025-08-12T00:00:00Z"),
|
||||
created_to: Optional[str] = Query(None, description="Bis-Zeitpunkt (ISO 8601)", example="2025-08-13T00:00:00Z"),
|
||||
limit: int = Query(20, ge=1, le=200, description="Max. Anzahl Items"),
|
||||
offset: int = Query(0, ge=0, description="Start-Offset für Paging"),
|
||||
):
|
||||
_ensure_collection(PLAN_COLLECTION)
|
||||
|
||||
# Grundfilter (ohne Zeit)
|
||||
base_must: List[Any] = []
|
||||
if created_by:
|
||||
base_must.append(FieldCondition(key="created_by", match=MatchValue(value=created_by)))
|
||||
if discipline:
|
||||
base_must.append(FieldCondition(key="discipline", match=MatchValue(value=discipline)))
|
||||
if age_group:
|
||||
base_must.append(FieldCondition(key="age_group", match=MatchValue(value=age_group)))
|
||||
if target_group:
|
||||
base_must.append(FieldCondition(key="target_group", match=MatchValue(value=target_group)))
|
||||
if goal:
|
||||
base_must.append(FieldCondition(key="goals", match=MatchValue(value=goal)))
|
||||
if section:
|
||||
base_must.append(FieldCondition(key="plan_section_names", match=MatchValue(value=section)))
|
||||
|
||||
# Serverseitiger Zeitbereich
|
||||
range_args: Dict[str, float] = {}
|
||||
try:
|
||||
if created_from:
|
||||
range_args["gte"] = float(datetime.fromisoformat(created_from.replace("Z", "+00:00")).timestamp())
|
||||
if created_to:
|
||||
range_args["lte"] = float(datetime.fromisoformat(created_to.replace("Z", "+00:00")).timestamp())
|
||||
except Exception:
|
||||
range_args = {}
|
||||
|
||||
applied_server_range = bool(range_args)
|
||||
must_with_time = list(base_must)
|
||||
if applied_server_range:
|
||||
must_with_time.append(FieldCondition(key="created_at_ts", range=Range(**range_args)))
|
||||
|
||||
need = max(offset + limit, 1)
|
||||
|
||||
# 1) Scroll mit Zeit-Range (falls vorhanden)
|
||||
pts = _scroll_collect(PLAN_COLLECTION, Filter(must=must_with_time or None) if must_with_time else None, need)
|
||||
|
||||
# 2) Fallback: 0 Treffer → ohne Zeit-Range scrollen und lokal filtern
|
||||
fallback_local_time_check = False
|
||||
if applied_server_range and not pts:
|
||||
pts = _scroll_collect(PLAN_COLLECTION, Filter(must=base_must or None) if base_must else None, need)
|
||||
fallback_local_time_check = True
|
||||
|
||||
def _in_window(py: Dict[str, Any]) -> bool:
|
||||
if not (created_from or created_to):
|
||||
return True
|
||||
if applied_server_range and not fallback_local_time_check:
|
||||
return True # serverseitig bereits gefiltert
|
||||
ts = py.get("created_at")
|
||||
if isinstance(ts, dict) and ts.get("$date"):
|
||||
ts = ts["$date"]
|
||||
if isinstance(py.get("created_at_ts"), (int, float)):
|
||||
dt = datetime.fromtimestamp(float(py["created_at_ts"]), tz=timezone.utc)
|
||||
elif isinstance(ts, str):
|
||||
try:
|
||||
dt = datetime.fromisoformat(ts.replace("Z", "+00:00"))
|
||||
except Exception:
|
||||
return False
|
||||
elif isinstance(ts, datetime):
|
||||
dt = ts
|
||||
else:
|
||||
return False
|
||||
ok = True
|
||||
if created_from:
|
||||
try:
|
||||
ok = ok and dt >= datetime.fromisoformat(created_from.replace("Z", "+00:00"))
|
||||
except Exception:
|
||||
pass
|
||||
if created_to:
|
||||
try:
|
||||
ok = ok and dt <= datetime.fromisoformat(created_to.replace("Z", "+00:00"))
|
||||
except Exception:
|
||||
pass
|
||||
return ok
|
||||
|
||||
payloads: List[Dict[str, Any]] = []
|
||||
for p in pts:
|
||||
py = dict(p.payload or {})
|
||||
py.setdefault("id", str(p.id))
|
||||
if _in_window(py):
|
||||
payloads.append(py)
|
||||
|
||||
sliced = payloads[offset:offset+limit]
|
||||
items = [_as_model(Plan, x) for x in sliced]
|
||||
return PlanList(items=items, limit=limit, offset=offset, count=len(items))
|
||||
@@ -0,0 +1,117 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
plan_session_router.py – v0.2.0 (WP-15)
|
||||
|
||||
CRUD-Minimum für Plan-Sessions (POST/GET) mit Referenz-Validierung.
|
||||
Kompatibel zum Qdrant-Client-Stil der bestehenden Router.
|
||||
"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel, Field, conint
|
||||
from typing import List, Optional, Dict, Any
|
||||
from uuid import uuid4
|
||||
from datetime import datetime, timezone
|
||||
import os
|
||||
|
||||
from clients import model, qdrant
|
||||
from qdrant_client.models import PointStruct, Filter, FieldCondition, MatchValue, VectorParams, Distance
|
||||
|
||||
router = APIRouter(tags=["plan_sessions"])
|
||||
|
||||
# -----------------
|
||||
# Konfiguration
|
||||
# -----------------
|
||||
PLAN_SESSION_COLLECTION = os.getenv("PLAN_SESSION_COLLECTION", "plan_sessions")
|
||||
PLAN_COLLECTION = os.getenv("PLAN_COLLECTION", "plans")
|
||||
|
||||
# -----------------
|
||||
# Modelle
|
||||
# -----------------
|
||||
class Feedback(BaseModel):
|
||||
rating: conint(ge=1, le=5)
|
||||
notes: str
|
||||
|
||||
class PlanSession(BaseModel):
|
||||
id: str = Field(default_factory=lambda: str(uuid4()))
|
||||
plan_id: str
|
||||
executed_at: datetime
|
||||
location: str
|
||||
coach: str
|
||||
group_label: str
|
||||
feedback: Feedback
|
||||
used_equipment: List[str] = []
|
||||
|
||||
# -----------------
|
||||
# Helpers
|
||||
# -----------------
|
||||
|
||||
def _ensure_collection(name: str):
|
||||
if not qdrant.collection_exists(name):
|
||||
qdrant.recreate_collection(
|
||||
collection_name=name,
|
||||
vectors_config=VectorParams(size=model.get_sentence_embedding_dimension(), distance=Distance.COSINE),
|
||||
)
|
||||
|
||||
|
||||
def _norm_list(xs: List[str]) -> List[str]:
|
||||
seen, out = set(), []
|
||||
for x in xs or []:
|
||||
s = str(x).strip()
|
||||
k = s.casefold()
|
||||
if s and k not in seen:
|
||||
seen.add(k)
|
||||
out.append(s)
|
||||
return sorted(out, key=str.casefold)
|
||||
|
||||
|
||||
def _session_embed_text(s: PlanSession) -> str:
|
||||
parts = [s.plan_id, s.location, s.coach, s.group_label, s.feedback.notes]
|
||||
return ". ".join([p for p in parts if p])
|
||||
|
||||
|
||||
def _embed(text: str):
|
||||
return model.encode(text or "").tolist()
|
||||
|
||||
|
||||
def _get_by_field(collection: str, key: str, value: Any) -> Optional[Dict[str, Any]]:
|
||||
flt = Filter(must=[FieldCondition(key=key, match=MatchValue(value=value))])
|
||||
pts, _ = qdrant.scroll(collection_name=collection, scroll_filter=flt, limit=1, with_payload=True)
|
||||
if not pts:
|
||||
return None
|
||||
payload = dict(pts[0].payload or {})
|
||||
payload.setdefault("id", str(pts[0].id))
|
||||
return payload
|
||||
|
||||
# -----------------
|
||||
# Endpoints
|
||||
# -----------------
|
||||
@router.post("/plan_sessions", response_model=PlanSession)
|
||||
def create_plan_session(s: PlanSession):
|
||||
_ensure_collection(PLAN_SESSION_COLLECTION)
|
||||
# Referenz auf Plan prüfen
|
||||
if not _get_by_field(PLAN_COLLECTION, "id", s.plan_id):
|
||||
raise HTTPException(status_code=422, detail=f"Unknown plan_id: {s.plan_id}")
|
||||
|
||||
# Normalisieren
|
||||
s.used_equipment = _norm_list(s.used_equipment)
|
||||
payload = s.model_dump()
|
||||
# ISO8601 für executed_at sicherstellen
|
||||
if isinstance(payload.get("executed_at"), datetime):
|
||||
payload["executed_at"] = payload["executed_at"].astimezone(timezone.utc).isoformat()
|
||||
|
||||
vec = _embed(_session_embed_text(s))
|
||||
qdrant.upsert(collection_name=PLAN_SESSION_COLLECTION, points=[PointStruct(id=str(s.id), vector=vec, payload=payload)])
|
||||
return s
|
||||
|
||||
|
||||
@router.get("/plan_sessions/{session_id}", response_model=PlanSession)
|
||||
def get_plan_session(session_id: str):
|
||||
_ensure_collection(PLAN_SESSION_COLLECTION)
|
||||
found = _get_by_field(PLAN_SESSION_COLLECTION, "id", session_id)
|
||||
if not found:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
if isinstance(found.get("executed_at"), str):
|
||||
try:
|
||||
found["executed_at"] = datetime.fromisoformat(found["executed_at"])
|
||||
except Exception:
|
||||
pass
|
||||
return PlanSession(**found)
|
||||
+167
-33
@@ -1,24 +1,26 @@
|
||||
"""
|
||||
wiki_router.py – v1.4.1 (stabil & nachvollziehbar)
|
||||
wiki_router.py – v1.4.2 (Swagger angereichert)
|
||||
|
||||
Änderungen ggü. v1.4.0:
|
||||
- /info: Optionalen Request-Parameter entfernt (FastAPI/Pydantic Typfehler behoben)
|
||||
- Keine API-Signaturänderungen der Routen
|
||||
Änderungen ggü. v1.4.1:
|
||||
- Alle Endpunkte mit aussagekräftigem `summary`/`description`/`response_description` versehen
|
||||
- Parameter-Beschreibungen ergänzt (z. B. `verbose`, `category`, `title`)
|
||||
- Beispiele über `x-codeSamples` (cURL) und `json_schema_extra`
|
||||
- **Keine API-Signaturänderungen**
|
||||
|
||||
Ziele:
|
||||
- /semantic/pages reichert pageid/fullurl für ALLE Titel batchweise an (redirects=1, converttitles=1)
|
||||
- /info robust: 404 statt 500, mit Titel-Varianten
|
||||
- Wiederholungen & Throttling gegen MediaWiki
|
||||
- Optionale Diagnose-Ausgaben und Coverage-Kennzahlen
|
||||
- /info robust: 404 statt 500, mit Titel-Varianten (Leerzeichen/Unterstrich/Bindestrich)
|
||||
- Wiederholungen & Throttling gegen MediaWiki (WIKI_RETRIES, WIKI_SLEEP_MS)
|
||||
- Optional: Diagnose-Ausgaben (verbose) und Coverage-Kennzahlen (Logs)
|
||||
|
||||
|
||||
Wenn ihr stattdessen den Prefix im Router setzen wollt, einfach in der APIRouter-Zeile unten
|
||||
prefix="/import/wiki" ergänzen und in main.py OHNE prefix einbinden.
|
||||
Hinweis Prefix:
|
||||
- Der Router setzt `prefix="/import/wiki"`. In `llm_api.py` **ohne** weiteren Prefix einbinden, sonst entstehen Doppelpfade.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional, List
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, Field
|
||||
from textwrap import dedent
|
||||
import os, time, logging
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
@@ -35,30 +37,48 @@ WIKI_API_URL = os.getenv("WIKI_API_URL", "https://karatetrainer.net/api.php")
|
||||
WIKI_TIMEOUT = float(os.getenv("WIKI_TIMEOUT", "15"))
|
||||
WIKI_BATCH = int(os.getenv("WIKI_BATCH", "50"))
|
||||
WIKI_RETRIES = int(os.getenv("WIKI_RETRIES", "1")) # zusätzliche Versuche bei Upstream-Fehlern
|
||||
WIKI_SLEEPMS = int(os.getenv("WIKI_SLEEP_MS", "0")) # Throttle zwischen Requests
|
||||
WIKI_SLEEPMS = int(os.getenv("WIKI_SLEEP_MS", "0")) # Throttle zwischen Requests (Millisekunden)
|
||||
|
||||
# Single Session (Cookies für Login)
|
||||
wiki_session = requests.Session()
|
||||
wiki_session.headers.update({"User-Agent": "local-llm-wiki-proxy/1.4.1"})
|
||||
wiki_session.headers.update({"User-Agent": "local-llm-wiki-proxy/1.4.2"})
|
||||
|
||||
# -------- Schemas --------
|
||||
class WikiLoginRequest(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
username: str = Field(..., description="MediaWiki-Benutzername (kein .env-Wert)")
|
||||
password: str = Field(..., description="MediaWiki-Passwort (kein .env-Wert)")
|
||||
|
||||
model_config = {
|
||||
"json_schema_extra": {
|
||||
"example": {"username": "Bot", "password": "••••••"}
|
||||
}
|
||||
}
|
||||
|
||||
class WikiLoginResponse(BaseModel):
|
||||
status: str
|
||||
message: Optional[str] = None
|
||||
status: str = Field(..., description="'success' bei erfolgreichem Login")
|
||||
message: Optional[str] = Field(None, description="Optionale Zusatzinfos")
|
||||
|
||||
class PageInfoResponse(BaseModel):
|
||||
pageid: int
|
||||
title: str
|
||||
fullurl: str
|
||||
pageid: int = Field(..., description="Eindeutige PageID der MediaWiki-Seite")
|
||||
title: str = Field(..., description="Aufgelöster Titel (kann von Eingabe abweichen, z. B. Redirect/Normalize)")
|
||||
fullurl: str = Field(..., description="Kanonsiche URL zur Seite")
|
||||
|
||||
model_config = {
|
||||
"json_schema_extra": {
|
||||
"example": {"pageid": 218, "title": "Affenklatschen", "fullurl": "https://…/index.php?title=Affenklatschen"}
|
||||
}
|
||||
}
|
||||
|
||||
class PageContentResponse(BaseModel):
|
||||
pageid: int
|
||||
title: str
|
||||
wikitext: str
|
||||
pageid: int = Field(..., description="PageID der angefragten Seite")
|
||||
title: str = Field(..., description="Echo des mitgegebenen Titels (optional)")
|
||||
wikitext: str = Field(..., description="Roh-Wikitext (inkl. Templates), keine Sanitization")
|
||||
|
||||
model_config = {
|
||||
"json_schema_extra": {
|
||||
"example": {"pageid": 218, "title": "Affenklatschen", "wikitext": "{{ÜbungInfoBox|…}}"}
|
||||
}
|
||||
}
|
||||
|
||||
# -------- Utils --------
|
||||
|
||||
@@ -68,6 +88,9 @@ def _sleep():
|
||||
|
||||
|
||||
def _request_with_retry(method: str, params: Dict[str, Any], *, data: Dict[str, Any] | None = None) -> requests.Response:
|
||||
"""Wrapper um requests.* mit Retry/Throttle und konsistenten 502-Fehlern bei Upstream-Problemen.
|
||||
Nutzt .env: WIKI_RETRIES, WIKI_SLEEP_MS, WIKI_TIMEOUT, WIKI_API_URL
|
||||
"""
|
||||
last_exc: Optional[Exception] = None
|
||||
for attempt in range(WIKI_RETRIES + 1):
|
||||
try:
|
||||
@@ -86,6 +109,7 @@ def _request_with_retry(method: str, params: Dict[str, Any], *, data: Dict[str,
|
||||
|
||||
|
||||
def _normalize_variants(title: str) -> List[str]:
|
||||
"""Erzeuge robuste Titel-Varianten: Leerzeichen/Unterstrich, Bindestrich/Dash-Varianten."""
|
||||
t = (title or "").strip()
|
||||
variants = {t}
|
||||
if " " in t:
|
||||
@@ -98,6 +122,9 @@ def _normalize_variants(title: str) -> List[str]:
|
||||
|
||||
|
||||
def _fetch_pageinfo_batch(titles: List[str]) -> Dict[str, Dict[str, Any]]:
|
||||
"""Batch-Resolver für PageInfo (pageid, fullurl). Respektiert Redirects & Titel-Normalisierung.
|
||||
Achtung: Große Kategorien werden in Chunks à WIKI_BATCH verarbeitet. Throttling via WIKI_SLEEP_MS.
|
||||
"""
|
||||
if not titles:
|
||||
return {}
|
||||
out: Dict[str, Dict[str, Any]] = {}
|
||||
@@ -137,16 +164,55 @@ def _fetch_pageinfo_batch(titles: List[str]) -> Dict[str, Dict[str, Any]]:
|
||||
return out
|
||||
|
||||
# -------- Endpoints --------
|
||||
@router.get("/health")
|
||||
def health(verbose: Optional[int] = Query(default=0)) -> Dict[str, Any]:
|
||||
# einfacher Ping
|
||||
@router.get(
|
||||
"/health",
|
||||
summary="Ping & Site-Info des MediaWiki-Upstreams",
|
||||
description=dedent(
|
||||
"""
|
||||
Führt einen leichten `meta=siteinfo`-Request gegen den konfigurierten MediaWiki-Upstream aus.
|
||||
|
||||
**Besonderheiten**
|
||||
- Nutzt eine persistente `requests.Session` (Cookies werden für spätere Aufrufe wiederverwendet).
|
||||
- Respektiert `.env`: `WIKI_API_URL`, `WIKI_TIMEOUT`, `WIKI_RETRIES`, `WIKI_SLEEP_MS`.
|
||||
- Bei Upstream-Problemen wird **HTTP 502** geworfen (statt 500).
|
||||
|
||||
**Hinweis**: Je nach Wiki-Konfiguration sind detaillierte Infos (Generator/Sitename) nur **nach Login** sichtbar.
|
||||
"""
|
||||
),
|
||||
response_description="`{\"status\":\"ok\"}` oder mit `wiki.sitename/generator` bei `verbose=1`.",
|
||||
openapi_extra={
|
||||
"x-codeSamples": [
|
||||
{"lang": "bash", "label": "curl", "source": "curl -s 'http://localhost:8000/import/wiki/health?verbose=1' | jq ."}
|
||||
]
|
||||
}
|
||||
)
|
||||
def health(verbose: Optional[int] = Query(default=0, description="1 = Site-Metadaten (sitename/generator) mitsenden")) -> Dict[str, Any]:
|
||||
resp = _request_with_retry("GET", {"action": "query", "meta": "siteinfo", "format": "json"})
|
||||
if verbose:
|
||||
info = resp.json().get("query", {}).get("general", {})
|
||||
return {"status": "ok", "wiki": {"sitename": info.get("sitename"), "generator": info.get("generator")}}
|
||||
return {"status": "ok"}
|
||||
|
||||
@router.post("/login", response_model=WikiLoginResponse)
|
||||
|
||||
@router.post(
|
||||
"/login",
|
||||
response_model=WikiLoginResponse,
|
||||
summary="MediaWiki-Login (Session-Cookies werden serverseitig gespeichert)",
|
||||
description=dedent(
|
||||
"""
|
||||
Meldet den Proxy am MediaWiki an. Unterstützt `clientlogin` (mit `loginreturnurl`) und
|
||||
**fällt zurück** auf `action=login`, falls erforderlich. Erfolgreiche Logins hinterlegen die
|
||||
Session-Cookies in der Server-Session und gelten für nachfolgende Requests.
|
||||
|
||||
**Besonderheiten**
|
||||
- Erwartet **Benutzername/Passwort im Body** (keine .env-Creds).
|
||||
- Verwendet vor dem Login ein Logintoken (`meta=tokens`).
|
||||
- Rückgabe `{\"status\":\"success\"}` bei Erfolg, sonst **401**.
|
||||
- Respektiert Retry/Throttle aus `.env`.
|
||||
"""
|
||||
),
|
||||
response_description="`{\"status\":\"success\"}` bei Erfolg."
|
||||
)
|
||||
def login(data: WikiLoginRequest):
|
||||
# Token holen
|
||||
tok = _request_with_retry("GET", {"action": "query", "meta": "tokens", "type": "login", "format": "json"})
|
||||
@@ -182,8 +248,31 @@ def login(data: WikiLoginRequest):
|
||||
return WikiLoginResponse(status="success")
|
||||
raise HTTPException(status_code=401, detail=f"Login fehlgeschlagen: {res}")
|
||||
|
||||
@router.get("/semantic/pages")
|
||||
def semantic_pages(category: str = Query(..., description="Kategorie ohne 'Category:'")) -> Dict[str, Any]:
|
||||
|
||||
@router.get(
|
||||
"/semantic/pages",
|
||||
summary="SMW-Ask-Ergebnisse einer Kategorie mit PageID/URL anreichern",
|
||||
description=dedent(
|
||||
"""
|
||||
Ruft Semantic MediaWiki via `action=ask` auf und liefert ein **Dictionary**: `{"Titel": {...}}`.
|
||||
Anschließend werden **alle** Titel batchweise via `prop=info` um `pageid` und `fullurl` ergänzt
|
||||
(berücksichtigt Redirects & Titel-Normalisierung). Große Kategorien werden in Chunks der Größe
|
||||
`WIKI_BATCH` verarbeitet. Throttling gemäß `WIKI_SLEEP_MS`.
|
||||
|
||||
**Rückgabe**
|
||||
- Key = Seitentitel
|
||||
- Value = ursprüngliche Ask-Daten **plus** `pageid` & `fullurl` (falls auflösbar)
|
||||
- Kann leeres Objekt `{}` sein (z. B. wenn Login erforderlich oder Kategorie leer).
|
||||
"""
|
||||
),
|
||||
response_description="Dictionary pro Titel; Felder `pageid/fullurl` sind evtl. nicht für alle Titel gesetzt.",
|
||||
openapi_extra={
|
||||
"x-codeSamples": [
|
||||
{"lang": "bash", "label": "curl", "source": "curl -s 'http://localhost:8000/import/wiki/semantic/pages?category=%C3%9Cbungen' | jq . | head"}
|
||||
]
|
||||
}
|
||||
)
|
||||
def semantic_pages(category: str = Query(..., description="Kategorie-Name **ohne** 'Category:' Präfix")) -> Dict[str, Any]:
|
||||
# Rohdaten aus SMW (Ask)
|
||||
ask_query = f"[[Category:{category}]]|limit=50000"
|
||||
r = _request_with_retry("GET", {"action": "ask", "query": ask_query, "format": "json"})
|
||||
@@ -208,14 +297,59 @@ def semantic_pages(category: str = Query(..., description="Kategorie ohne 'Categ
|
||||
logger.info("/semantic/pages: %d Titel, %d ohne pageid nach Enrichment", len(results), missing)
|
||||
return enriched
|
||||
|
||||
@router.get("/parsepage", response_model=PageContentResponse)
|
||||
def parse_page(pageid: int = Query(...), title: str = Query(None)):
|
||||
|
||||
@router.get(
|
||||
"/parsepage",
|
||||
response_model=PageContentResponse,
|
||||
summary="Wikitext einer Seite per pageid holen",
|
||||
description=dedent(
|
||||
"""
|
||||
Liefert den **Roh-Wikitext** (`prop=wikitext`) zu einer Seite. Der optionale `title`-Parameter dient
|
||||
nur als Echo/Diagnose. Für strukturierte Extraktion (Infobox/Abschnitte) muss der Aufrufer den
|
||||
Wikitext selbst parsen.
|
||||
|
||||
**Besonderheiten**
|
||||
- Erfordert ggf. vorherigen Login (private Wikis).
|
||||
- Throttling/Retry gemäß `.env`.
|
||||
- Upstream-Fehler werden als **502** gemeldet.
|
||||
"""
|
||||
),
|
||||
response_description="Roh-Wikitext (keine HTML-Transformation).",
|
||||
openapi_extra={
|
||||
"x-codeSamples": [
|
||||
{"lang": "bash", "label": "curl", "source": "curl -s 'http://localhost:8000/import/wiki/parsepage?pageid=218&title=Affenklatschen' | jq ."}
|
||||
]
|
||||
}
|
||||
)
|
||||
def parse_page(pageid: int = Query(..., description="Numerische PageID der Seite"), title: str = Query(None, description="Optional: Seitentitel (nur Echo)")):
|
||||
resp = _request_with_retry("GET", {"action": "parse", "pageid": pageid, "prop": "wikitext", "format": "json"})
|
||||
wikitext = resp.json().get("parse", {}).get("wikitext", {}).get("*", "")
|
||||
return PageContentResponse(pageid=pageid, title=title or "", wikitext=wikitext)
|
||||
|
||||
@router.get("/info", response_model=PageInfoResponse)
|
||||
def page_info(title: str = Query(..., description="Seitentitel")):
|
||||
|
||||
@router.get(
|
||||
"/info",
|
||||
response_model=PageInfoResponse,
|
||||
summary="PageID/URL zu einem Titel auflösen (inkl. Varianten)",
|
||||
description=dedent(
|
||||
"""
|
||||
Versucht zuerst eine **exakte** Auflösung des angegebenen Titels (mit `redirects=1`, `converttitles=1`).
|
||||
Falls nicht erfolgreich, werden **Titel-Varianten** getestet (Leerzeichen↔Unterstrich, Bindestrich↔Gedankenstrich).
|
||||
Bei Erfolg Rückgabe mit `pageid`, aufgelöstem `title` und `fullurl`. Andernfalls **404**.
|
||||
|
||||
**Typische Fälle**
|
||||
- Unterschiedliche Schreibweisen (z. B. "Yoko Geri" vs. "Yoko_Geri").
|
||||
- Redirect-Ketten → es wird der **kanonische** Titel/URL geliefert.
|
||||
"""
|
||||
),
|
||||
response_description="Erfolg: PageInfo. Fehler: 404 'Page not found: <title>'.",
|
||||
openapi_extra={
|
||||
"x-codeSamples": [
|
||||
{"lang": "bash", "label": "curl", "source": "curl -s 'http://localhost:8000/import/wiki/info?title=Affenklatschen' | jq ."}
|
||||
]
|
||||
}
|
||||
)
|
||||
def page_info(title: str = Query(..., description="Seitentitel (unscharf; Varianten werden versucht)")):
|
||||
# 1. Versuch: wie geliefert, mit redirects/converttitles
|
||||
res = _fetch_pageinfo_batch([title])
|
||||
if res.get(title):
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://example.local/schemas/plan_sessions.json",
|
||||
"title": "PlanSession",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["id", "plan_id", "executed_at", "location", "coach", "group_label", "feedback", "used_equipment"],
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"plan_id": { "type": "string" },
|
||||
"executed_at": { "type": "string", "format": "date-time" },
|
||||
"location": { "type": "string" },
|
||||
"coach": { "type": "string" },
|
||||
"group_label": { "type": "string" },
|
||||
"feedback": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["rating", "notes"],
|
||||
"properties": {
|
||||
"rating": { "type": "integer", "minimum": 1, "maximum": 5 },
|
||||
"notes": { "type": "string" }
|
||||
}
|
||||
},
|
||||
"used_equipment": { "type": "array", "items": { "type": "string" } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://example.local/schemas/plan_templates.json",
|
||||
"title": "PlanTemplate",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"id", "name", "discipline", "age_group", "target_group", "total_minutes",
|
||||
"sections", "goals", "equipment_allowed", "created_by", "version"
|
||||
],
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"name": { "type": "string", "minLength": 1 },
|
||||
"discipline": { "type": "string" },
|
||||
"age_group": { "type": "string" },
|
||||
"target_group": { "type": "string" },
|
||||
"total_minutes": { "type": "integer", "minimum": 0 },
|
||||
"sections": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["name", "target_minutes", "must_keywords", "forbid_keywords", "capability_targets"],
|
||||
"properties": {
|
||||
"name": { "type": "string" },
|
||||
"target_minutes": { "type": "integer", "minimum": 0 },
|
||||
"must_keywords": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||
"ideal_keywords": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||
"supplement_keywords": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||
"forbid_keywords": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||
"capability_targets": { "type": "object", "additionalProperties": { "type": "integer" } }
|
||||
}
|
||||
}
|
||||
},
|
||||
"goals": { "type": "array", "items": { "type": "string" } },
|
||||
"equipment_allowed": { "type": "array", "items": { "type": "string" } },
|
||||
"created_by": { "type": "string" },
|
||||
"version": { "type": "string" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://example.local/schemas/plans.json",
|
||||
"title": "Plan",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"id", "template_id", "title", "discipline", "age_group", "target_group",
|
||||
"total_minutes", "sections", "goals", "capability_summary", "created_by",
|
||||
"created_at", "source", "fingerprint"
|
||||
],
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"template_id": { "type": ["string", "null"] },
|
||||
"title": { "type": "string", "minLength": 1 },
|
||||
"discipline": { "type": "string" },
|
||||
"age_group": { "type": "string" },
|
||||
"target_group": { "type": "string" },
|
||||
"total_minutes": { "type": "integer", "minimum": 0 },
|
||||
"sections": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["name", "items", "minutes"],
|
||||
"properties": {
|
||||
"name": { "type": "string" },
|
||||
"minutes": { "type": "integer", "minimum": 0 },
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["exercise_external_id", "duration", "why"],
|
||||
"properties": {
|
||||
"exercise_external_id": { "type": "string" },
|
||||
"duration": { "type": "integer", "minimum": 0 },
|
||||
"why": { "type": "string" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"goals": { "type": "array", "items": { "type": "string" } },
|
||||
"capability_summary": { "type": "object", "additionalProperties": { "type": "integer" } },
|
||||
"novelty_against_last_n": { "type": ["number", "null"] },
|
||||
"fingerprint": { "type": "string" },
|
||||
"created_by": { "type": "string" },
|
||||
"created_at": { "type": "string", "format": "date-time" },
|
||||
"source": { "type": "string" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Backfill Capability-Facetten in Qdrant – v1.2
|
||||
Fix: beendet korrekt, wenn `next_page_offset` (offset) None ist.
|
||||
"""
|
||||
import os
|
||||
from typing import Dict, Any, List
|
||||
from qdrant_client import QdrantClient
|
||||
|
||||
COLL = os.getenv("EXERCISE_COLLECTION", "exercises")
|
||||
QDRANT_HOST = os.getenv("QDRANT_HOST", "localhost")
|
||||
QDRANT_PORT = int(os.getenv("QDRANT_PORT", "6333"))
|
||||
BATCH = int(os.getenv("BACKFILL_BATCH", "256"))
|
||||
|
||||
|
||||
def _facet_capabilities(caps: Dict[str, Any]) -> Dict[str, List[str]]:
|
||||
caps = caps or {}
|
||||
def names_where(pred) -> List[str]:
|
||||
out = []
|
||||
for k, v in caps.items():
|
||||
try:
|
||||
iv = int(v)
|
||||
except Exception:
|
||||
iv = 0
|
||||
if pred(iv):
|
||||
s = str(k).strip()
|
||||
if s:
|
||||
out.append(s)
|
||||
return sorted({s for s in out}, key=str.casefold)
|
||||
|
||||
all_keys = sorted({str(k).strip() for k in caps.keys() if str(k).strip()}, key=str.casefold)
|
||||
return {
|
||||
"capability_keys": all_keys,
|
||||
"capability_ge1": names_where(lambda lv: lv >= 1),
|
||||
"capability_ge2": names_where(lambda lv: lv >= 2),
|
||||
"capability_ge3": names_where(lambda lv: lv >= 3),
|
||||
"capability_ge4": names_where(lambda lv: lv >= 4),
|
||||
"capability_ge5": names_where(lambda lv: lv >= 5),
|
||||
"capability_eq1": names_where(lambda lv: lv == 1),
|
||||
"capability_eq2": names_where(lambda lv: lv == 2),
|
||||
"capability_eq3": names_where(lambda lv: lv == 3),
|
||||
"capability_eq4": names_where(lambda lv: lv == 4),
|
||||
"capability_eq5": names_where(lambda lv: lv == 5),
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
client = QdrantClient(host=QDRANT_HOST, port=QDRANT_PORT)
|
||||
info = client.get_collection(COLL)
|
||||
print(f"[Backfill] Collection '{COLL}' ok – vectors={info.config.params.vectors}")
|
||||
|
||||
updated_total = 0
|
||||
offset = None
|
||||
page = 0
|
||||
|
||||
while True:
|
||||
points, next_offset = client.scroll(
|
||||
collection_name=COLL,
|
||||
scroll_filter=None,
|
||||
offset=offset,
|
||||
limit=BATCH,
|
||||
with_payload=True,
|
||||
)
|
||||
page += 1
|
||||
if not points:
|
||||
print("[Backfill] no more points – done")
|
||||
break
|
||||
|
||||
updated_page = 0
|
||||
for pt in points:
|
||||
pld = pt.payload or {}
|
||||
caps = pld.get("capabilities") or {}
|
||||
facets = _facet_capabilities(caps)
|
||||
|
||||
# nur setzen, wenn sich etwas ändert
|
||||
need = any(pld.get(k) != v for k, v in facets.items())
|
||||
if not need:
|
||||
continue
|
||||
client.set_payload(collection_name=COLL, points=[pt.id], payload=facets)
|
||||
updated_total += 1
|
||||
updated_page += 1
|
||||
|
||||
print(f"[Backfill] page={page} processed={len(points)} updated_page={updated_page} updated_total={updated_total}")
|
||||
|
||||
# Ende erreicht? Dann nach dieser Seite aussteigen.
|
||||
if next_offset is None:
|
||||
break
|
||||
offset = next_offset
|
||||
|
||||
print(f"[Backfill] done. total_updated={updated_total}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Idempotenter Bootstrap für die Qdrant-Collection 'exercises':
|
||||
- Legt fehlende Payload-Indizes an (ohne Collection zu droppen)
|
||||
- Optional prüft Dimension/Distanz
|
||||
|
||||
Starten mit
|
||||
pip install qdrant-client --upgrade
|
||||
python3 scripts/bootstrap_qdrant_exercises.py
|
||||
|
||||
"""
|
||||
|
||||
import os
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import PayloadSchemaType
|
||||
|
||||
QDRANT_HOST = os.getenv("QDRANT_HOST", "localhost")
|
||||
QDRANT_PORT = int(os.getenv("QDRANT_PORT", "6333"))
|
||||
COLLECTION = os.getenv("EXERCISE_COLLECTION", "exercises")
|
||||
|
||||
INDEX_SPECS = [
|
||||
("keywords", PayloadSchemaType.KEYWORD),
|
||||
("equipment", PayloadSchemaType.KEYWORD),
|
||||
("discipline", PayloadSchemaType.KEYWORD),
|
||||
("age_group", PayloadSchemaType.KEYWORD),
|
||||
("target_group", PayloadSchemaType.KEYWORD),
|
||||
("category", PayloadSchemaType.KEYWORD),
|
||||
("capability_keys", PayloadSchemaType.KEYWORD),
|
||||
("capability_ge1", PayloadSchemaType.KEYWORD),
|
||||
("capability_ge2", PayloadSchemaType.KEYWORD),
|
||||
("capability_ge3", PayloadSchemaType.KEYWORD),
|
||||
("capability_ge4", PayloadSchemaType.KEYWORD), # neu
|
||||
("capability_ge5", PayloadSchemaType.KEYWORD), # neu
|
||||
("capability_eq1", PayloadSchemaType.KEYWORD), # neu
|
||||
("capability_eq2", PayloadSchemaType.KEYWORD), # neu
|
||||
("capability_eq3", PayloadSchemaType.KEYWORD), # neu
|
||||
("capability_eq4", PayloadSchemaType.KEYWORD), # neu
|
||||
("capability_eq5", PayloadSchemaType.KEYWORD), # neu
|
||||
]
|
||||
|
||||
def main():
|
||||
client = QdrantClient(host=QDRANT_HOST, port=QDRANT_PORT)
|
||||
# Sanity: Collection existiert?
|
||||
info = client.get_collection(COLLECTION) # wirf Fehler, wenn nicht vorhanden
|
||||
print(f"[Bootstrap] Collection '{COLLECTION}' gefunden. Vectors={info.config.params.vectors}")
|
||||
|
||||
# Indizes idempotent anlegen
|
||||
for field, schema in INDEX_SPECS:
|
||||
try:
|
||||
client.create_payload_index(collection_name=COLLECTION,
|
||||
field_name=field,
|
||||
field_schema=schema)
|
||||
print(f"[Bootstrap] Index created: {field} ({schema})")
|
||||
except Exception as e:
|
||||
# Qdrant wirft hier Exceptions, wenn der Index schon existiert – das ist ok
|
||||
print(f"[Bootstrap] Index exists or skipped: {field} -> {e}")
|
||||
|
||||
print("[Bootstrap] done.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Bootstrap (idempotent) für Qdrant-Collections rund um Pläne – v1.3.0
|
||||
|
||||
- Fügt fehlende Payload-Indizes hinzu (KEYWORD/FLOAT), idempotent.
|
||||
- NEU: FLOAT-Index `plans.created_at_ts` für serverseitige Zeitfensterfilter.
|
||||
"""
|
||||
import os
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import PayloadSchemaType
|
||||
|
||||
QDRANT_HOST = os.getenv("QDRANT_HOST", "localhost")
|
||||
QDRANT_PORT = int(os.getenv("QDRANT_PORT", "6333"))
|
||||
|
||||
PLANS = os.getenv("PLAN_COLLECTION") or os.getenv("QDRANT_COLLECTION_PLANS", "plans")
|
||||
TEMPLATES = os.getenv("PLAN_TEMPLATE_COLLECTION", "plan_templates")
|
||||
SESSIONS = os.getenv("PLAN_SESSION_COLLECTION", "plan_sessions")
|
||||
|
||||
INDEX_SPECS = {
|
||||
TEMPLATES: [
|
||||
("discipline", PayloadSchemaType.KEYWORD),
|
||||
("age_group", PayloadSchemaType.KEYWORD),
|
||||
("target_group", PayloadSchemaType.KEYWORD),
|
||||
("section_names", PayloadSchemaType.KEYWORD),
|
||||
("section_must_keywords", PayloadSchemaType.KEYWORD),
|
||||
("section_ideal_keywords", PayloadSchemaType.KEYWORD),
|
||||
("section_supplement_keywords", PayloadSchemaType.KEYWORD),
|
||||
("section_forbid_keywords", PayloadSchemaType.KEYWORD),
|
||||
("goals", PayloadSchemaType.KEYWORD),
|
||||
],
|
||||
PLANS: [
|
||||
("discipline", PayloadSchemaType.KEYWORD),
|
||||
("age_group", PayloadSchemaType.KEYWORD),
|
||||
("target_group", PayloadSchemaType.KEYWORD),
|
||||
("sections.name", PayloadSchemaType.KEYWORD), # legacy, belassen
|
||||
("plan_section_names", PayloadSchemaType.KEYWORD),
|
||||
("goals", PayloadSchemaType.KEYWORD),
|
||||
("created_by", PayloadSchemaType.KEYWORD),
|
||||
("created_at", PayloadSchemaType.KEYWORD),
|
||||
("created_at_ts", PayloadSchemaType.FLOAT), # NEU
|
||||
("fingerprint", PayloadSchemaType.KEYWORD),
|
||||
("title", PayloadSchemaType.KEYWORD),
|
||||
],
|
||||
SESSIONS: [
|
||||
("plan_id", PayloadSchemaType.KEYWORD),
|
||||
("executed_at", PayloadSchemaType.KEYWORD),
|
||||
("coach", PayloadSchemaType.KEYWORD),
|
||||
("group_label", PayloadSchemaType.KEYWORD),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _create_indexes(client: QdrantClient, collection: str, specs):
|
||||
try:
|
||||
client.get_collection(collection)
|
||||
print(f"[Bootstrap v1.3.0] Collection '{collection}' ok.")
|
||||
except Exception as e:
|
||||
print(f"[Bootstrap v1.3.0] WARN: Collection '{collection}' nicht gefunden (wird beim ersten Upsert erstellt). Details: {e}")
|
||||
return
|
||||
|
||||
for field, schema in specs:
|
||||
try:
|
||||
client.create_payload_index(collection_name=collection, field_name=field, field_schema=schema)
|
||||
print(f"[Bootstrap v1.3.0] Index created: {collection}.{field} ({schema})")
|
||||
except Exception as e:
|
||||
print(f"[Bootstrap v1.3.0] Index exists or skipped: {collection}.{field} -> {e}")
|
||||
|
||||
|
||||
def main():
|
||||
print(f"[Bootstrap v1.3.0] Qdrant @ {QDRANT_HOST}:{QDRANT_PORT}")
|
||||
print(f"[Bootstrap v1.3.0] Collections: TEMPLATES={TEMPLATES} PLANS={PLANS} SESSIONS={SESSIONS}")
|
||||
client = QdrantClient(host=QDRANT_HOST, port=QDRANT_PORT)
|
||||
for coll, specs in INDEX_SPECS.items():
|
||||
_create_indexes(client, coll, specs)
|
||||
print("[Bootstrap v1.3.0] done.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,255 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Erweiterter API-Schnelltest - v2.1
|
||||
|
||||
Änderungen ggü. v2.0:
|
||||
- **Login VOR** /semantic/pages (viele Wikis liefern Ask-Ergebnisse nur authentifiziert)
|
||||
- Robuster Fallback: Wenn /semantic/pages {} liefert → optionaler ENV-Fallback `TEST_WIKI_TITLE`
|
||||
- Bessere Diagnoseausgaben (zeigt Basis-URL, Category, gesetzte Login-ENV)
|
||||
|
||||
ENV:
|
||||
BASE_URL (default http://127.0.0.1:8000)
|
||||
TEST_WIKI_CATEGORY (default "Übungen")
|
||||
TEST_WIKI_TITLE (optional Fallback-Titel, z. B. "Affenklatschen")
|
||||
WIKI_BOT_USER, WIKI_BOT_PASSWORD (optional; empfohlen)
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import uuid
|
||||
import requests
|
||||
|
||||
BASE = os.getenv("BASE_URL", "http://127.0.0.1:8000").rstrip("/")
|
||||
TEST_WIKI_CATEGORY = os.getenv("TEST_WIKI_CATEGORY", "Übungen")
|
||||
TEST_WIKI_TITLE = os.getenv("TEST_WIKI_TITLE")
|
||||
WIKI_USER = os.getenv("WIKI_BOT_USER")
|
||||
WIKI_PASS = os.getenv("WIKI_BOT_PASSWORD")
|
||||
|
||||
print("\nConfig:")
|
||||
print(" BASE_URL=", BASE)
|
||||
print(" TEST_WIKI_CATEGORY=", TEST_WIKI_CATEGORY)
|
||||
print(" TEST_WIKI_TITLE=", TEST_WIKI_TITLE or "<none>")
|
||||
print(" WIKI_BOT_USER set=", bool(WIKI_USER))
|
||||
|
||||
# ---- helpers ----
|
||||
|
||||
def fail(msg, resp: requests.Response | None = None):
|
||||
print("✗", msg)
|
||||
if resp is not None:
|
||||
try:
|
||||
print(" → status:", resp.status_code)
|
||||
print(" → body:", resp.text[:800])
|
||||
except Exception:
|
||||
pass
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def ok(msg):
|
||||
print("✓", msg)
|
||||
|
||||
|
||||
# ---- basic ----
|
||||
|
||||
def test_openapi():
|
||||
r = requests.get(f"{BASE}/openapi.json")
|
||||
if r.status_code != 200:
|
||||
fail("/openapi.json nicht erreichbar", r)
|
||||
ok("OpenAPI erreichbar")
|
||||
|
||||
|
||||
# ---- wiki ----
|
||||
|
||||
def test_wiki_health():
|
||||
r = requests.get(f"{BASE}/import/wiki/health", params={"verbose": 1})
|
||||
if r.status_code != 200 or r.json().get("status") != "ok":
|
||||
fail("/import/wiki/health fehlgeschlagen", r)
|
||||
ok("Wiki /health ok")
|
||||
|
||||
|
||||
def test_wiki_login_if_env():
|
||||
if not (WIKI_USER and WIKI_PASS):
|
||||
print("⚠️ Wiki /login übersprungen (WIKI_BOT_USER/PASSWORD nicht gesetzt)")
|
||||
return False
|
||||
r = requests.post(f"{BASE}/import/wiki/login", json={"username": WIKI_USER, "password": WIKI_PASS})
|
||||
if r.status_code != 200 or r.json().get("status") != "success":
|
||||
fail("/import/wiki/login fehlgeschlagen", r)
|
||||
ok("Wiki /login ok")
|
||||
return True
|
||||
|
||||
|
||||
def get_wiki_title_and_pageid() -> tuple[str, int]:
|
||||
# 1) Versuch über semantic/pages
|
||||
r = requests.get(f"{BASE}/import/wiki/semantic/pages", params={"category": TEST_WIKI_CATEGORY})
|
||||
if r.status_code != 200:
|
||||
fail("/import/wiki/semantic/pages fehlgeschlagen", r)
|
||||
data = r.json()
|
||||
if isinstance(data, dict) and data:
|
||||
title = next(iter(data.keys()))
|
||||
entry = data[title]
|
||||
pid = entry.get("pageid")
|
||||
if not pid:
|
||||
fail("/semantic/pages ohne pageid nach Enrichment", r)
|
||||
ok(f"Wiki /semantic/pages ok – Beispiel: '{title}' (pageid={pid})")
|
||||
return title, int(pid)
|
||||
|
||||
print("ℹ️ /semantic/pages lieferte keine Titel ({}). Versuche Fallback...")
|
||||
# 2) Fallback über TEST_WIKI_TITLE
|
||||
if not TEST_WIKI_TITLE:
|
||||
fail("Kein TEST_WIKI_TITLE gesetzt und /semantic/pages leer – kann keinen Titel testen.")
|
||||
r2 = requests.get(f"{BASE}/import/wiki/info", params={"title": TEST_WIKI_TITLE})
|
||||
if r2.status_code != 200:
|
||||
fail("/import/wiki/info(Fallback) fehlgeschlagen", r2)
|
||||
js = r2.json()
|
||||
pid = js.get("pageid")
|
||||
if not pid:
|
||||
fail("/info(Fallback) ohne pageid", r2)
|
||||
ok(f"Wiki /info Fallback ok – '{TEST_WIKI_TITLE}' (pageid={pid})")
|
||||
return TEST_WIKI_TITLE, int(pid)
|
||||
|
||||
|
||||
def test_wiki_info(title: str):
|
||||
r = requests.get(f"{BASE}/import/wiki/info", params={"title": title})
|
||||
if r.status_code != 200:
|
||||
fail("/import/wiki/info fehlgeschlagen", r)
|
||||
js = r.json()
|
||||
if not js.get("pageid"):
|
||||
fail("/info ohne pageid", r)
|
||||
ok("Wiki /info ok")
|
||||
|
||||
|
||||
def test_wiki_parse(pageid: int, title: str):
|
||||
r = requests.get(f"{BASE}/import/wiki/parsepage", params={"pageid": pageid, "title": title})
|
||||
if r.status_code != 200:
|
||||
fail("/import/wiki/parsepage fehlgeschlagen", r)
|
||||
js = r.json()
|
||||
if not isinstance(js.get("wikitext"), str):
|
||||
fail("/parsepage ohne wikitext", r)
|
||||
ok("Wiki /parsepage ok")
|
||||
|
||||
|
||||
# ---- exercise ----
|
||||
|
||||
def make_exercise_payload(external_id: str):
|
||||
return {
|
||||
"external_id": external_id,
|
||||
"fingerprint": "unit-test-sha",
|
||||
"source": "UnitTest",
|
||||
"title": "Testübung Reaktion",
|
||||
"summary": "Kurzbeschreibung für Test.",
|
||||
"short_description": "Kurzbeschreibung für Test.",
|
||||
"keywords": ["Reaktion", "Bälle", "Bälle"],
|
||||
"link": "http://example.local",
|
||||
"discipline": "Karate",
|
||||
"group": "5",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"min_participants": 1,
|
||||
"duration_minutes": 10,
|
||||
"capabilities": {"Reaktionsfähigkeit": 2, "Kopplungsfähigkeit": 1},
|
||||
"category": "Übungen",
|
||||
"purpose": "Aufwärmen",
|
||||
"execution": "Einfacher Ablauf.",
|
||||
"notes": "Hinweise.",
|
||||
"preparation": "Bälle holen.",
|
||||
"method": "Frontale Methode",
|
||||
"equipment": ["Bälle", "Pratze"]
|
||||
}
|
||||
|
||||
|
||||
def test_exercise_upsert_and_idempotence():
|
||||
import time
|
||||
ext = f"ut:{uuid.uuid4()}"
|
||||
payload = make_exercise_payload(ext)
|
||||
|
||||
# create
|
||||
r1 = requests.post(f"{BASE}/exercise", json=payload)
|
||||
if r1.status_code != 200:
|
||||
fail("POST /exercise (create) fehlgeschlagen", r1)
|
||||
id1 = r1.json().get("id")
|
||||
if not id1:
|
||||
fail("POST /exercise lieferte keine id", r1)
|
||||
|
||||
# update (idempotent)
|
||||
r2 = requests.post(f"{BASE}/exercise", json=payload)
|
||||
if r2.status_code != 200:
|
||||
fail("POST /exercise (update) fehlgeschlagen", r2)
|
||||
id2 = r2.json().get("id")
|
||||
if id2 != id1:
|
||||
fail("Idempotenz verletzt: id hat sich geändert")
|
||||
|
||||
# by-external-id
|
||||
r3 = requests.get(f"{BASE}/exercise/by-external-id", params={"external_id": ext})
|
||||
if r3.status_code != 200:
|
||||
fail("GET /exercise/by-external-id fehlgeschlagen", r3)
|
||||
id3 = r3.json().get("id")
|
||||
if id3 != id1:
|
||||
fail("Lookup by external_id liefert andere id")
|
||||
|
||||
ok("Exercise Upsert & Idempotenz ok")
|
||||
return ext, id1
|
||||
|
||||
|
||||
def test_exercise_search_filter(ext_id: str):
|
||||
req = {
|
||||
"discipline": "Karate",
|
||||
"equipment_all": ["Bälle"],
|
||||
"capability_names": ["Reaktionsfähigkeit"],
|
||||
"capability_ge_level": 2,
|
||||
"limit": 10,
|
||||
}
|
||||
r = requests.post(f"{BASE}/exercise/search", json=req)
|
||||
if r.status_code != 200:
|
||||
fail("POST /exercise/search (Filter) fehlgeschlagen", r)
|
||||
js = r.json(); hits = js.get("hits", [])
|
||||
if not isinstance(hits, list) or not hits:
|
||||
fail("/exercise/search liefert keine Treffer (Filter)", r)
|
||||
if not any(h.get("payload", {}).get("external_id") == ext_id for h in hits):
|
||||
ok("Exercise Search (Filter) ok – Testpunkt nicht zwingend in Top-N")
|
||||
else:
|
||||
ok("Exercise Search (Filter) ok – Testpunkt gefunden")
|
||||
|
||||
|
||||
def test_exercise_search_vector():
|
||||
req = {
|
||||
"query": "Aufwärmen 10min, Reaktionsfähigkeit, Teenager, Bälle",
|
||||
"discipline": "Karate",
|
||||
"limit": 5
|
||||
}
|
||||
r = requests.post(f"{BASE}/exercise/search", json=req)
|
||||
if r.status_code != 200:
|
||||
fail("POST /exercise/search (Vector) fehlgeschlagen", r)
|
||||
js = r.json(); hits = js.get("hits", [])
|
||||
if not isinstance(hits, list):
|
||||
fail("/exercise/search (Vector) liefert ungültige Struktur", r)
|
||||
ok(f"Exercise Search (Vector) ok – {len(hits)} Treffer")
|
||||
|
||||
|
||||
def test_exercise_delete(ext_id: str):
|
||||
r = requests.delete(f"{BASE}/exercise/delete-by-external-id", params={"external_id": ext_id})
|
||||
if r.status_code != 200:
|
||||
fail("DELETE /exercise/delete-by-external-id fehlgeschlagen", r)
|
||||
ok("Exercise Delete-by-external-id ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("\nStarte API-Tests...\n")
|
||||
test_openapi()
|
||||
test_wiki_health()
|
||||
|
||||
# Login nun VOR Semantic Query
|
||||
did_login = test_wiki_login_if_env()
|
||||
|
||||
# Titel ermitteln (semantic/pages oder Fallback)
|
||||
title, pageid = get_wiki_title_and_pageid()
|
||||
test_wiki_info(title)
|
||||
test_wiki_parse(pageid, title)
|
||||
|
||||
# Exercise
|
||||
ext, _id = test_exercise_upsert_and_idempotence()
|
||||
test_exercise_search_filter(ext)
|
||||
test_exercise_search_vector()
|
||||
test_exercise_delete(ext)
|
||||
|
||||
print("\n🎉 Alle Tests erfolgreich durchlaufen!\n")
|
||||
@@ -0,0 +1,241 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Unit-, Integrations- und E2E-Tests für WP-15 (v1.2.0)."""
|
||||
import os
|
||||
import json
|
||||
import requests
|
||||
import pytest
|
||||
from datetime import datetime, timezone
|
||||
|
||||
BASE = os.getenv("BASE_URL", "http://127.0.0.1:8000").rstrip("/")
|
||||
QDRANT = os.getenv("QDRANT_BASE", "http://127.0.0.1:6333").rstrip("/")
|
||||
TPL_COLL = os.getenv("PLAN_TEMPLATE_COLLECTION", "plan_templates")
|
||||
|
||||
# ---------- Helpers ----------
|
||||
|
||||
def _fp_local(plan_payload: dict) -> str:
|
||||
import hashlib
|
||||
core = {
|
||||
"title": plan_payload["title"],
|
||||
"total_minutes": int(plan_payload["total_minutes"]),
|
||||
"items": [
|
||||
{"exercise_external_id": it["exercise_external_id"], "duration": int(it["duration"])}
|
||||
for sec in plan_payload["sections"]
|
||||
for it in sec.get("items", [])
|
||||
],
|
||||
}
|
||||
raw = json.dumps(core, sort_keys=True, ensure_ascii=False)
|
||||
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _unique_len_lower(xs):
|
||||
seen = set()
|
||||
out = []
|
||||
for x in xs:
|
||||
k = x.casefold()
|
||||
if k not in seen:
|
||||
seen.add(k)
|
||||
out.append(x)
|
||||
return len(out)
|
||||
|
||||
# ---------- Unit ----------
|
||||
|
||||
def test_fingerprint_unit_v12():
|
||||
p = {
|
||||
"title": "Montag – Reaktion",
|
||||
"total_minutes": 90,
|
||||
"sections": [
|
||||
{"name": "Warmup", "minutes": 15, "items": [
|
||||
{"exercise_external_id": "ex:001", "duration": 10, "why": "Aufwärmen"}
|
||||
]}
|
||||
],
|
||||
}
|
||||
fp1 = _fp_local(p)
|
||||
p2 = json.loads(json.dumps(p, ensure_ascii=False))
|
||||
fp2 = _fp_local(p2)
|
||||
assert fp1 == fp2
|
||||
|
||||
# ---------- Integration: Templates mit mehreren Sections + ideal/supplement ----------
|
||||
|
||||
def test_template_sections_ideal_supplement_roundtrip():
|
||||
tpl = {
|
||||
"name": "Std 90 v1.1",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 90,
|
||||
"sections": [
|
||||
{
|
||||
"name": "Warmup",
|
||||
"target_minutes": 15,
|
||||
"must_keywords": ["Reaktion", "reaktion"], # Duplikat in anderer Schreibweise
|
||||
"ideal_keywords": ["Koordination", "koordination"], # Duplikat in anderer Schreibweise
|
||||
"supplement_keywords": ["Teamspiel", " Teamspiel "], # Duplikat mit Whitespace
|
||||
"forbid_keywords": [],
|
||||
"capability_targets": {"Reaktionsfähigkeit": 2, "Mobilität": 1}
|
||||
},
|
||||
{
|
||||
"name": "Technikblock",
|
||||
"target_minutes": 30,
|
||||
"must_keywords": ["Mae-Geri"],
|
||||
"ideal_keywords": ["Timing"],
|
||||
"supplement_keywords": ["Partnerarbeit"],
|
||||
"forbid_keywords": ["Bodenarbeit"],
|
||||
"capability_targets": {"Technikpräzision": 2, "Schnelligkeit": 1}
|
||||
}
|
||||
],
|
||||
"goals": ["Technik", "Kondition"],
|
||||
"equipment_allowed": ["Bälle"],
|
||||
"created_by": "tester",
|
||||
"version": "1.1"
|
||||
}
|
||||
|
||||
r = requests.post(f"{BASE}/plan_templates", json=tpl)
|
||||
assert r.status_code == 200, r.text
|
||||
tpl_id = r.json()["id"]
|
||||
|
||||
r2 = requests.get(f"{BASE}/plan_templates/{tpl_id}")
|
||||
assert r2.status_code == 200, r2.text
|
||||
got = r2.json()
|
||||
|
||||
# Prüfen: Beide Sections vorhanden
|
||||
assert len(got["sections"]) == 2
|
||||
|
||||
s1 = got["sections"][0]
|
||||
# Normalisierung: Duplikate entfernt (case-insensitive), Whitespace getrimmt
|
||||
assert _unique_len_lower(s1["must_keywords"]) == len(s1["must_keywords"]) == 1
|
||||
assert _unique_len_lower(s1["ideal_keywords"]) == len(s1["ideal_keywords"]) == 1
|
||||
assert _unique_len_lower(s1["supplement_keywords"]) == len(s1["supplement_keywords"]) == 1
|
||||
|
||||
# ---------- Optional: Qdrant-Payload-Check (materialisierte Felder) ----------
|
||||
|
||||
def test_qdrant_materialized_fields_template():
|
||||
"""
|
||||
Robust: Erst frisches Template anlegen (mit neuen Feldern), dann Qdrant-Scroll
|
||||
mit Filter auf genau diese Template-ID. So treffen wir sicher einen Punkt,
|
||||
der die materialisierten Felder enthält – unabhängig von älteren Datensätzen.
|
||||
"""
|
||||
# 1) Frisches Template erzeugen (mit ideal/supplement)
|
||||
tpl = {
|
||||
"name": "Std 90 v1.1 – materialized-check",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 90,
|
||||
"sections": [
|
||||
{
|
||||
"name": "Warmup",
|
||||
"target_minutes": 15,
|
||||
"must_keywords": ["Reaktion"],
|
||||
"ideal_keywords": ["Koordination"],
|
||||
"supplement_keywords": ["Teamspiel"],
|
||||
"forbid_keywords": [],
|
||||
"capability_targets": {"Reaktionsfähigkeit": 2}
|
||||
}
|
||||
],
|
||||
"goals": ["Technik"],
|
||||
"equipment_allowed": ["Bälle"],
|
||||
"created_by": "tester",
|
||||
"version": "1.1"
|
||||
}
|
||||
r = requests.post(f"{BASE}/plan_templates", json=tpl)
|
||||
assert r.status_code == 200, r.text
|
||||
tpl_id = r.json()["id"]
|
||||
|
||||
# 2) Qdrant gezielt nach genau diesem Punkt scrollen (Payload enthält id)
|
||||
try:
|
||||
rq = requests.post(
|
||||
f"{QDRANT}/collections/{TPL_COLL}/points/scroll",
|
||||
json={
|
||||
"with_payload": True,
|
||||
"limit": 1,
|
||||
"filter": {"must": [{"key": "id", "match": {"value": tpl_id}}]}
|
||||
},
|
||||
timeout=2.0,
|
||||
)
|
||||
except Exception:
|
||||
pytest.skip("Qdrant nicht erreichbar – überspringe materialisierte Feldprüfung")
|
||||
|
||||
if rq.status_code != 200:
|
||||
pytest.skip(f"Qdrant-Scroll liefert {rq.status_code}")
|
||||
|
||||
js = rq.json()
|
||||
pts = (js.get("result") or {}).get("points") or []
|
||||
if not pts:
|
||||
pytest.skip("Keine Übereinstimmung in plan_templates – überspringe")
|
||||
|
||||
payload = pts[0].get("payload") or {}
|
||||
for key in [
|
||||
"section_names",
|
||||
"section_must_keywords",
|
||||
"section_ideal_keywords",
|
||||
"section_supplement_keywords",
|
||||
"section_forbid_keywords",
|
||||
]:
|
||||
assert key in payload
|
||||
assert isinstance(payload[key], list)
|
||||
|
||||
# ---------- E2E: Plan anlegen + Idempotenz trotz variierender Nebenfelder ----------
|
||||
|
||||
def test_plan_e2e_idempotence_same_fingerprint():
|
||||
# Template minimal für Bezug
|
||||
tpl = {
|
||||
"name": "Std 90 for plan",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 90,
|
||||
"sections": [
|
||||
{"name": "Warmup", "target_minutes": 15, "must_keywords": [], "forbid_keywords": [], "capability_targets": {}}
|
||||
],
|
||||
"goals": [],
|
||||
"equipment_allowed": [],
|
||||
"created_by": "tester",
|
||||
"version": "1.0"
|
||||
}
|
||||
r1 = requests.post(f"{BASE}/plan_templates", json=tpl)
|
||||
assert r1.status_code == 200
|
||||
tpl_id = r1.json()["id"]
|
||||
|
||||
plan_base = {
|
||||
"template_id": tpl_id,
|
||||
"title": "KW32 – Montag",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 90,
|
||||
"sections": [
|
||||
{"name": "Warmup", "minutes": 15, "items": [
|
||||
{"exercise_external_id": "ex:001", "duration": 10, "why": "Aufwärmen"}
|
||||
]}
|
||||
],
|
||||
"created_by": "tester",
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "test"
|
||||
}
|
||||
|
||||
# Plan A mit goals/capability_summary
|
||||
plan_a = dict(plan_base)
|
||||
plan_a.update({
|
||||
"goals": ["Technik"],
|
||||
"capability_summary": {"Reaktionsfähigkeit": 2}
|
||||
})
|
||||
r2 = requests.post(f"{BASE}/plan", json=plan_a)
|
||||
assert r2.status_code == 200, r2.text
|
||||
plan_id = r2.json()["id"]
|
||||
|
||||
# Plan B – gleicher Fingerprint (gleiche items), aber andere Nebenfelder
|
||||
plan_b = dict(plan_base)
|
||||
plan_b.update({
|
||||
"goals": ["Kondition"],
|
||||
"capability_summary": {"Reaktionsfähigkeit": 3}
|
||||
})
|
||||
r3 = requests.post(f"{BASE}/plan", json=plan_b)
|
||||
assert r3.status_code == 200
|
||||
assert r3.json()["id"] == plan_id, "Idempotenz verletzt: gleicher Fingerprint muss gleiche ID liefern"
|
||||
|
||||
# GET prüfen
|
||||
r4 = requests.get(f"{BASE}/plan/{plan_id}")
|
||||
assert r4.status_code == 200
|
||||
js = r4.json()
|
||||
assert js["title"] == "KW32 – Montag"
|
||||
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Integritätstests für Referenzen (Templates, Exercises, Plans)."""
|
||||
import os, requests, uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
BASE = os.getenv("BASE_URL", "http://127.0.0.1:8000").rstrip("/")
|
||||
|
||||
|
||||
def _make_exercise(ext_id: str):
|
||||
payload = {
|
||||
"external_id": ext_id,
|
||||
"fingerprint": "it-sha",
|
||||
"source": "IntegrityTest",
|
||||
"title": "Übung Dummy",
|
||||
"summary": "",
|
||||
"short_description": "",
|
||||
"keywords": ["Reaktion"],
|
||||
"discipline": "Karate",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"min_participants": 1,
|
||||
"duration_minutes": 5,
|
||||
"capabilities": {"Reaktionsfähigkeit": 1},
|
||||
"category": "Übungen",
|
||||
"purpose": "",
|
||||
"execution": "",
|
||||
"notes": "",
|
||||
"preparation": "",
|
||||
"method": "",
|
||||
"equipment": []
|
||||
}
|
||||
r = requests.post(f"{BASE}/exercise", json=payload)
|
||||
assert r.status_code == 200, r.text
|
||||
|
||||
|
||||
def test_plan_requires_existing_template():
|
||||
# Übung anlegen für gültigen Plan (damit Exercise-Check nicht stört)
|
||||
exid = f"it:{uuid.uuid4()}"; _make_exercise(exid)
|
||||
|
||||
# Plan mit nicht existenter template_id
|
||||
plan = {
|
||||
"template_id": "does-not-exist",
|
||||
"title": "Plan mit falschem Template",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 30,
|
||||
"sections": [{"name": "Block", "minutes": 30, "items": [{"exercise_external_id": exid, "duration": 10, "why": ""}]}],
|
||||
"goals": [],
|
||||
"capability_summary": {},
|
||||
"created_by": "tester",
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "test"
|
||||
}
|
||||
r = requests.post(f"{BASE}/plan", json=plan)
|
||||
assert r.status_code == 422
|
||||
|
||||
|
||||
def test_plan_session_requires_existing_plan():
|
||||
sess = {
|
||||
"plan_id": "does-not-exist",
|
||||
"executed_at": datetime.now(timezone.utc).isoformat(),
|
||||
"location": "Dojo",
|
||||
"coach": "X",
|
||||
"group_label": "Y",
|
||||
"feedback": {"rating": 3, "notes": ""},
|
||||
"used_equipment": []
|
||||
}
|
||||
r = requests.post(f"{BASE}/plan_sessions", json=sess)
|
||||
assert r.status_code == 422
|
||||
|
||||
|
||||
def test_strict_exercises_if_env():
|
||||
# Nur sinnvoll, wenn Server mit PLAN_STRICT_EXERCISES=1 gestartet wurde
|
||||
if os.getenv("PLAN_STRICT_EXERCISES") not in {"1", "true", "yes", "on"}:
|
||||
import pytest; pytest.skip("Strict-Mode nicht aktiv – Test übersprungen")
|
||||
|
||||
plan = {
|
||||
"title": "Plan mit unbekannter Übung",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Teenager",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 10,
|
||||
"sections": [{"name": "Block", "minutes": 10, "items": [{"exercise_external_id": "unknown:xyz", "duration": 5, "why": ""}]}],
|
||||
"goals": [],
|
||||
"capability_summary": {},
|
||||
"created_by": "tester",
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "test"
|
||||
}
|
||||
r = requests.post(f"{BASE}/plan", json=plan)
|
||||
assert r.status_code == 422
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Listen-/Filter-Tests für Templates & Pläne (v0.1.0)."""
|
||||
import os
|
||||
import requests
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
BASE = os.getenv("BASE_URL", "http://127.0.0.1:8000").rstrip("/")
|
||||
|
||||
|
||||
def test_list_plan_templates_filters():
|
||||
# Zwei Templates anlegen (unterschiedliche Sections/Goals)
|
||||
tpl1 = {
|
||||
"name": "ListTpl A", "discipline": "Karate", "age_group": "Teenager", "target_group": "Breitensport", "total_minutes": 60,
|
||||
"sections": [{"name": "Warmup", "target_minutes": 10, "must_keywords": ["Reaktion"], "forbid_keywords": [], "ideal_keywords": ["Koordination"], "supplement_keywords": [], "capability_targets": {}}],
|
||||
"goals": ["Technik"], "equipment_allowed": [], "created_by": "tester", "version": "1.0"
|
||||
}
|
||||
tpl2 = {
|
||||
"name": "ListTpl B", "discipline": "Karate", "age_group": "Erwachsene", "target_group": "Breitensport", "total_minutes": 60,
|
||||
"sections": [{"name": "Technikblock", "target_minutes": 30, "must_keywords": ["Mae-Geri"], "forbid_keywords": [], "ideal_keywords": ["Timing"], "supplement_keywords": ["Partnerarbeit"], "capability_targets": {}}],
|
||||
"goals": ["Kondition"], "equipment_allowed": [], "created_by": "tester", "version": "1.0"
|
||||
}
|
||||
r1 = requests.post(f"{BASE}/plan_templates", json=tpl1); assert r1.status_code == 200
|
||||
r2 = requests.post(f"{BASE}/plan_templates", json=tpl2); assert r2.status_code == 200
|
||||
|
||||
# Filter: discipline=Karate & section=Warmup → sollte tpl1 enthalten
|
||||
r = requests.get(f"{BASE}/plan_templates", params={"discipline": "Karate", "section": "Warmup", "limit": 10, "offset": 0})
|
||||
assert r.status_code == 200, r.text
|
||||
js = r.json(); assert js["count"] >= 1
|
||||
assert any(tpl["name"] == "ListTpl A" for tpl in js["items"])
|
||||
|
||||
# Filter: keyword=Timing → sollte tpl2 treffen (ideal_keywords)
|
||||
r = requests.get(f"{BASE}/plan_templates", params={"keyword": "Timing"})
|
||||
assert r.status_code == 200
|
||||
js = r.json(); names = [t["name"] for t in js["items"]]
|
||||
assert "ListTpl B" in names
|
||||
|
||||
|
||||
def test_list_plans_filters_and_window():
|
||||
# Plan A (Teenager), Plan B (Erwachsene)
|
||||
base_tpl = {
|
||||
"name": "ListTpl PlanBase", "discipline": "Karate", "age_group": "Teenager", "target_group": "Breitensport", "total_minutes": 45,
|
||||
"sections": [{"name": "Warmup", "target_minutes": 10, "must_keywords": [], "forbid_keywords": [], "capability_targets": {}}],
|
||||
"goals": ["Technik"], "equipment_allowed": [], "created_by": "tester", "version": "1.0"
|
||||
}
|
||||
rt = requests.post(f"{BASE}/plan_templates", json=base_tpl); assert rt.status_code == 200
|
||||
tpl_id = rt.json()["id"]
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
plan_a = {
|
||||
"template_id": tpl_id, "title": "List Plan A", "discipline": "Karate", "age_group": "Teenager", "target_group": "Breitensport", "total_minutes": 45,
|
||||
"sections": [{"name": "Warmup", "minutes": 10, "items": []}], "goals": ["Technik"], "capability_summary": {}, "created_by": "tester",
|
||||
"created_at": now.isoformat(), "source": "test"
|
||||
}
|
||||
plan_b = {
|
||||
"template_id": tpl_id, "title": "List Plan B", "discipline": "Karate", "age_group": "Erwachsene", "target_group": "Breitensport", "total_minutes": 45,
|
||||
"sections": [{"name": "Technikblock", "minutes": 30, "items": []}], "goals": ["Kondition"], "capability_summary": {}, "created_by": "tester",
|
||||
"created_at": (now + timedelta(seconds=1)).isoformat(), "source": "test"
|
||||
}
|
||||
ra = requests.post(f"{BASE}/plan", json=plan_a); assert ra.status_code == 200
|
||||
rb = requests.post(f"{BASE}/plan", json=plan_b); assert rb.status_code == 200
|
||||
|
||||
# Filter: age_group=Teenager & section=Warmup → sollte Plan A enthalten
|
||||
r = requests.get(f"{BASE}/plans", params={"age_group": "Teenager", "section": "Warmup"})
|
||||
assert r.status_code == 200
|
||||
js = r.json(); assert js["count"] >= 1
|
||||
assert any(p["title"] == "List Plan A" for p in js["items"])
|
||||
|
||||
# Zeitfenster: created_from nach Plan A → sollte Plan B enthalten
|
||||
r = requests.get(f"{BASE}/plans", params={"created_from": (now + timedelta(milliseconds=500)).isoformat()})
|
||||
assert r.status_code == 200
|
||||
js = r.json(); titles = [p["title"] for p in js["items"]]
|
||||
assert "List Plan B" in titles
|
||||
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Integration/E2E-Tests für plan_sessions (v0.1.0)."""
|
||||
import os
|
||||
import requests
|
||||
from datetime import datetime, timezone
|
||||
|
||||
BASE = os.getenv("BASE_URL", "http://127.0.0.1:8000").rstrip("/")
|
||||
|
||||
|
||||
def test_plan_session_create_read():
|
||||
# 1) Template minimal
|
||||
tpl = {
|
||||
"name": "Std 60 for sessions",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Erwachsene",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 60,
|
||||
"sections": [
|
||||
{"name": "Warmup", "target_minutes": 10, "must_keywords": [], "forbid_keywords": [], "capability_targets": {}}
|
||||
],
|
||||
"goals": [],
|
||||
"equipment_allowed": [],
|
||||
"created_by": "tester",
|
||||
"version": "1.0"
|
||||
}
|
||||
r1 = requests.post(f"{BASE}/plan_templates", json=tpl)
|
||||
assert r1.status_code == 200, r1.text
|
||||
|
||||
# 2) Plan minimal
|
||||
plan = {
|
||||
"template_id": r1.json()["id"],
|
||||
"title": "Session‑Plan",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Erwachsene",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 60,
|
||||
"sections": [
|
||||
{"name": "Warmup", "minutes": 10, "items": []}
|
||||
],
|
||||
"goals": [],
|
||||
"capability_summary": {},
|
||||
"created_by": "tester",
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "test"
|
||||
}
|
||||
r2 = requests.post(f"{BASE}/plan", json=plan)
|
||||
assert r2.status_code == 200, r2.text
|
||||
plan_id = r2.json()["id"]
|
||||
|
||||
# 3) Session anlegen
|
||||
session = {
|
||||
"plan_id": plan_id,
|
||||
"executed_at": datetime.now(timezone.utc).isoformat(),
|
||||
"location": "Dojo A",
|
||||
"coach": "Sensei K.",
|
||||
"group_label": "Montag 18:00",
|
||||
"feedback": {"rating": 4, "notes": "Gute Energie, Warmup etwas zu kurz."},
|
||||
"used_equipment": [" Pratzen ", "Bälle", "pratzen"]
|
||||
}
|
||||
r3 = requests.post(f"{BASE}/plan_sessions", json=session)
|
||||
assert r3.status_code == 200, r3.text
|
||||
sess_id = r3.json()["id"]
|
||||
|
||||
# 4) Session lesen & Normalisierung prüfen
|
||||
r4 = requests.get(f"{BASE}/plan_sessions/{sess_id}")
|
||||
assert r4.status_code == 200
|
||||
js = r4.json()
|
||||
# used_equipment dedupliziert/trimmed/casefolded → len == 2
|
||||
assert len(js["used_equipment"]) == 2
|
||||
assert "Pratzen" in js["used_equipment"] or "pratzen" in [x.lower() for x in js["used_equipment"]]
|
||||
|
||||
|
||||
def test_plan_session_invalid_rating():
|
||||
# Minimaler Plan für Referenz
|
||||
tpl = {
|
||||
"name": "Std 45 for sessions",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Erwachsene",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 45,
|
||||
"sections": [
|
||||
{"name": "Warmup", "target_minutes": 10, "must_keywords": [], "forbid_keywords": [], "capability_targets": {}}
|
||||
],
|
||||
"goals": [],
|
||||
"equipment_allowed": [],
|
||||
"created_by": "tester",
|
||||
"version": "1.0"
|
||||
}
|
||||
r1 = requests.post(f"{BASE}/plan_templates", json=tpl)
|
||||
assert r1.status_code == 200
|
||||
plan = {
|
||||
"template_id": r1.json()["id"],
|
||||
"title": "Fehlerfall",
|
||||
"discipline": "Karate",
|
||||
"age_group": "Erwachsene",
|
||||
"target_group": "Breitensport",
|
||||
"total_minutes": 45,
|
||||
"sections": [
|
||||
{"name": "Warmup", "minutes": 10, "items": []}
|
||||
],
|
||||
"goals": [],
|
||||
"capability_summary": {},
|
||||
"created_by": "tester",
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "test"
|
||||
}
|
||||
r2 = requests.post(f"{BASE}/plan", json=plan)
|
||||
assert r2.status_code == 200
|
||||
|
||||
bad_session = {
|
||||
"plan_id": r2.json()["id"],
|
||||
"executed_at": datetime.now(timezone.utc).isoformat(),
|
||||
"location": "Dojo B",
|
||||
"coach": "Assist.",
|
||||
"group_label": "Dienstag 19:00",
|
||||
"feedback": {"rating": 7, "notes": "invalid"}, # ungültig (1..5)
|
||||
"used_equipment": []
|
||||
}
|
||||
r_bad = requests.post(f"{BASE}/plan_sessions", json=bad_session)
|
||||
assert r_bad.status_code == 422
|
||||
Reference in new issue
Block a user