feat(p4): content analysis service with Neo4j knowledge graph and ClickHouse analytics

P4 阶段交付物:
- services/content: 内容资源服务(NestJS)
  - textbooks: 教材 CRUD + 知识图谱绑定
  - config/neo4j.ts: Neo4j driver 单例
  - textbooks.service.ts: MySQL CRUD + Neo4j 知识图谱(createKnowledgeGraph/getPrerequisites)
  - package.json: 补充 @opentelemetry/sdk-node + exporter-trace-otlp-http
- services/data-ana: 数据分析服务(Python FastAPI)
  - main.py: FastAPI + /healthz + class_performance + student_weakness 骨架
  - clickhouse_client.py: ClickHouse 客户端封装
  - config.py: 环境变量配置
- packages/shared-proto/proto/content.proto: TextbookService + KnowledgeGraphService 契约
- packages/shared-proto/proto/analytics.proto: AnalyticsService 契约(class_performance/student_weakness)
This commit is contained in:
SpecialX
2026-07-08 01:38:35 +08:00
parent 23246ade6d
commit 9850bfcfd1
28 changed files with 936 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
FROM python:3.12-slim
WORKDIR /app
RUN pip install uv
COPY pyproject.toml .
RUN uv sync --no-dev
COPY src ./src
EXPOSE 3006
CMD ["uv", "run", "uvicorn", "src.data_ana.main:app", "--host", "0.0.0.0", "--port", "3006"]

View File

@@ -0,0 +1,58 @@
# data-ana 数据分析服务
> 版本0.1P4 骨架)
> 端口3006
## 职责
数据分析限界上下文Python 实现),消费 core-edu 与 content 的领域事件,
构建 ClickHouse 学情宽表,计算知识点掌握度。
对外提供学情仪表盘查询、班级/年级/学校维度报表、个性化推荐数据支撑。
## 技术栈
- Python 3.12+ / FastAPI 0.115+
- clickhouse-connectClickHouse 宽表查询)
- pydantic + pydantic-settings运行时校验与配置
- structlog结构化日志
- prometheus-client指标
- OpenTelemetry分布式追踪
## 开发
```bash
uv sync
uv run uvicorn src.data_ana.main:app --host 0.0.0.0 --port 3006 --reload
```
## 配置
| 变量 | 默认值 | 说明 |
|------|--------|------|
| `PORT` | 3006 | 服务端口 |
| `CLICKHOUSE_HOST` | localhost | ClickHouse 主机 |
| `CLICKHOUSE_PORT` | 8123 | ClickHouse HTTP 端口 |
| `CLICKHOUSE_DATABASE` | edu_analytics | ClickHouse 数据库 |
| `OTEL_ENDPOINT` | http://localhost:4318 | OpenTelemetry 端点 |
| `LOG_LEVEL` | info | 日志级别 |
## 模块结构
```
src/data_ana/
├─ __init__.py
├─ main.py # FastAPI 入口(健康检查 + 分析端点骨架)
├─ config.py # pydantic-settings 配置
└─ clickhouse_client.py # ClickHouse 客户端单例
```
## 关键端点
- `GET /healthz` 健康检查
- `GET /metrics` Prometheus 指标
- `GET /analytics/class/{class_id}/performance` 班级成绩分析P4 骨架)
- `GET /analytics/student/{student_id}/weakness` 学生薄弱知识点分析P4 骨架)
## 对外契约
gRPC 服务 `AnalyticsService` 定义见 `packages/shared-proto/proto/analytics.proto`

View File

@@ -0,0 +1,24 @@
[project]
name = "data-ana-service"
version = "0.1.0"
description = "数据分析服务 - ClickHouse + 学习分析"
requires-python = ">=3.12"
dependencies = [
"fastapi>=0.115.0",
"uvicorn[standard]>=0.30.0",
"clickhouse-connect>=0.7.0",
"pydantic>=2.9.0",
"pydantic-settings>=2.5.0",
"opentelemetry-api>=1.27.0",
"opentelemetry-sdk>=1.27.0",
"opentelemetry-instrumentation-fastapi>=0.48b0",
"prometheus-client>=0.20.0",
"structlog>=24.4.0",
]
[tool.ruff]
line-length = 100
target-version = "py312"
[tool.ruff.lint]
select = ["E", "F", "I", "N", "W", "UP", "B", "SIM"]

View File

@@ -0,0 +1,25 @@
"""ClickHouse 客户端."""
import clickhouse_connect
from .config import settings
_client = None
def get_client():
"""获取 ClickHouse 客户端."""
global _client
if _client is None:
_client = clickhouse_connect.get_client(
host=settings.clickhouse_host,
port=settings.clickhouse_port,
database=settings.clickhouse_database,
)
return _client
async def close_client() -> None:
"""关闭客户端."""
global _client
if _client:
_client.close()
_client = None

View File

@@ -0,0 +1,18 @@
"""配置管理."""
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
"""应用配置."""
port: int = 3006
clickhouse_host: str = "localhost"
clickhouse_port: int = 8123
clickhouse_database: str = "edu_analytics"
otel_endpoint: str = "http://localhost:4318"
log_level: str = "info"
model_config = {"env_file": ".env", "env_prefix": ""}
settings = Settings()

View File

@@ -0,0 +1,75 @@
"""数据分析服务入口."""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from prometheus_client import make_asgi_app
import structlog
logger = structlog.get_logger()
tracer = trace.get_tracer(__name__)
def init_tracer() -> None:
"""初始化 OpenTelemetry."""
provider = TracerProvider()
exporter = OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")
provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(provider)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期."""
init_tracer()
logger.info("data-ana service starting")
yield
logger.info("data-ana service stopping")
app = FastAPI(
title="Data Analytics Service",
version="0.1.0",
lifespan=lifespan,
)
# Prometheus 指标
app.mount("/metrics", make_asgi_app())
@app.get("/healthz")
async def healthz():
"""健康检查."""
return {"status": "ok", "service": "data-ana"}
@app.get("/analytics/class/{class_id}/performance")
async def class_performance(class_id: str):
"""班级成绩分析."""
with tracer.start_as_current_span("class_performance"):
# P4 骨架:从 ClickHouse 查询分析数据
return {
"success": True,
"data": {
"classId": class_id,
"averageScore": 0,
"passRate": 0,
"message": "P4 skeleton - ClickHouse integration pending",
},
}
@app.get("/analytics/student/{student_id}/weakness")
async def student_weakness(student_id: str):
"""学生薄弱知识点分析."""
with tracer.start_as_current_span("student_weakness"):
return {
"success": True,
"data": {
"studentId": student_id,
"weakPoints": [],
"message": "P4 skeleton - weakness analysis pending",
},
}