Files
Edu/services/data-ana/src/data_ana/config.py
SpecialX 9db7fd917e feat(data-ana): v2 P6 硬化完成 + 6 新 RPC + Prometheus 监控
P6 硬化(5 项全部完成):

- CDC 多实例水平扩展: _INSTANCE_ID + get_lag() 真实 lag 计算

- ExamCache Redis 化: key data_ana:exam:{exam_id}, TTL 30 天 + 内存 LRU fallback

- ClickHouse TTL 归档: 5 表均加 TTL(1-3 年),分区级删除

- Prometheus 监控: 18 个指标(CDC/CH/ExamCache/DataScope/gRPC/业务)

- readyz 深度硬化: 4 依赖超时检查(CH 1s/Redis 200ms/iam 2s/CDC lag<1000)

v2 新增 6 个 RPC(analytics.proto 扩展为 18 RPC):

- GetStudentGrowth / GetAssignmentAnalysis / GetMasterySummary

- ListDiagnosticReports(占位,待 ai 服务)/ ListErrorBookItems / GetErrorBookStats

监控与可观测性: lifespan 预热 + gRPC ServerInterceptor + CDC 消费者指标

Docker 本地测试 19 项全部通过(healthz/readyz/metrics + 11 HTTP + 10 gRPC + ruff)

nextstep-v2.md: 上游需求对齐 + 下游要求(iam/core-edu/content/ai/SRE)
2026-07-14 18:07:17 +08:00

104 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""配置管理pydantic-settings 完整配置项).
对齐 02-architecture-design.md §15 配置清单.
环境变量前缀 DATA_ANA_如 DATA_ANA_HTTP_PORT=3006.
"""
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""应用配置.
所有外部依赖ClickHouse / Kafka / Redis / iam gRPC均为可选
未配置或不可达时服务进入降级模式,返回骨架数据 + details.degraded: true.
"""
# 服务
service_name: str = "data-ana"
http_port: int = 3006
grpc_port: int = 50055
log_level: str = "INFO"
dev_mode: bool = False
# ClickHouse可选留空则降级模式
clickhouse_host: str = ""
clickhouse_port: int = 8123
clickhouse_database: str = "edu_analytics"
clickhouse_user: str = ""
clickhouse_password: str = ""
clickhouse_connect_timeout_ms: int = 3000
clickhouse_query_timeout_s: int = 3 # P4 退出标准 5s查询 3s 超时降级
# Kafka
kafka_brokers: str = "" # 留空则不启动 CDC 消费者
kafka_consumer_group: str = "data-ana-cdc"
kafka_cdc_topics: str = (
"edu-cdc.next_edu_cloud.core_edu_grades,"
"edu-cdc.next_edu_cloud.core_edu_exams,"
"edu-cdc.next_edu_cloud.core_edu_homework_submissions,"
"edu-cdc.next_edu_cloud.core_edu_attendance,"
"edu-cdc.next_edu_cloud.classes,"
"edu-cdc.next_edu_cloud.iam_users,"
"edu-cdc.next_edu_cloud.content_knowledge_points"
)
kafka_mastery_topic: str = "edu.insight.mastery.updated"
kafka_warning_topic: str = "edu.insight.mastery.updated" # 复用 mastery topic总裁裁决 §2.11
kafka_ai_usage_topic: str = "edu.insight.ai.usage"
kafka_enable_auto_commit: bool = False # v2: 手动 commitat-least-once
kafka_auto_offset_reset: str = "latest"
kafka_producer_transactional_id: str = "data-ana-producer"
# iam gRPC
iam_grpc_endpoint: str = "" # 留空则使用降级兜底(按 role 映射 DataScope
iam_grpc_timeout_s: int = 2
datascope_cache_ttl_s: int = 300 # 5min
# Redis
redis_url: str = "" # 留空则跳过缓存
redis_pool_size: int = 10
redis_socket_timeout_ms: int = 200
# OTel
otel_endpoint: str = "http://localhost:4318"
otel_service_name: str = "data-ana"
# 掌握度算法
mastery_window_size: int = 5
mastery_decay_base: float = 0.6
mastery_forgetting_half_life_days: int = 30
mastery_min_samples: int = 3
# 预警阈值
warning_low_mastery_threshold: float = 0.4
warning_critical_mastery_threshold: float = 0.2
warning_score_drop_percent: float = 0.2
warning_absent_per_week: int = 3
# 降级
degraded_mode_enabled: bool = True
# P6 readyz 深度硬化
readyz_clickhouse_timeout_s: float = 1.0
readyz_redis_timeout_s: float = 0.2
readyz_iam_grpc_timeout_s: float = 2.0
readyz_cdc_lag_threshold: int = 1000 # lag 超过此值判定 not_ready
# 向后兼容:旧代码引用 settings.port / settings.kafka_group_id
@property
def port(self) -> int:
return self.http_port
@property
def kafka_group_id(self) -> str:
return self.kafka_consumer_group
model_config = SettingsConfigDict(
env_file=".env",
env_prefix="DATA_ANA_",
extra="ignore",
)
settings = Settings()