Blame view

api/routes/admin.py 3.58 KB
be52af70   tangwang   first commit
1
2
3
4
  """
  Admin API routes for configuration and management.
  """
  
26b910bd   tangwang   refactor service ...
5
  from fastapi import APIRouter, HTTPException, Request
be52af70   tangwang   first commit
6
7
  
  from ..models import HealthResponse, ErrorResponse
26b910bd   tangwang   refactor service ...
8
  from indexer.mapping_generator import get_tenant_index_name
be52af70   tangwang   first commit
9
10
11
12
13
14
15
16
17
18
19
20
  
  router = APIRouter(prefix="/admin", tags=["admin"])
  
  
  @router.get("/health", response_model=HealthResponse)
  async def health_check():
      """
      Health check endpoint.
  
      Returns service status and Elasticsearch connectivity.
      """
      try:
a406638e   tangwang   up
21
          from ..app import get_es_client, get_config
be52af70   tangwang   first commit
22
23
24
25
26
27
28
29
30
  
          es_client = get_es_client()
          config = get_config()
  
          # Check ES connectivity
          es_status = "connected" if es_client.ping() else "disconnected"
  
          return HealthResponse(
              status="healthy" if es_status == "connected" else "unhealthy",
4d824a77   tangwang   所有租户共用一套统一配置.tena...
31
              elasticsearch=es_status
be52af70   tangwang   first commit
32
33
34
35
36
          )
  
      except Exception as e:
          return HealthResponse(
              status="unhealthy",
4d824a77   tangwang   所有租户共用一套统一配置.tena...
37
              elasticsearch="error"
be52af70   tangwang   first commit
38
39
40
41
42
43
          )
  
  
  @router.get("/config")
  async def get_configuration():
      """
86d8358b   tangwang   config optimize
44
      Get the effective application configuration (sanitized).
be52af70   tangwang   first commit
45
46
      """
      try:
a406638e   tangwang   up
47
          from ..app import get_config
be52af70   tangwang   first commit
48
  
86d8358b   tangwang   config optimize
49
50
51
52
53
54
          return get_config().sanitized_dict()
  
      except HTTPException:
          raise
      except Exception as e:
          raise HTTPException(status_code=500, detail=str(e))
be52af70   tangwang   first commit
55
  
86d8358b   tangwang   config optimize
56
57
58
59
60
61
62
63
  
  @router.get("/config/meta")
  async def get_configuration_meta():
      """Get configuration metadata for observability."""
      try:
          from ..app import get_config
  
          config = get_config()
be52af70   tangwang   first commit
64
          return {
86d8358b   tangwang   config optimize
65
66
67
68
              "environment": config.runtime.environment,
              "config_hash": config.metadata.config_hash,
              "loaded_files": list(config.metadata.loaded_files),
              "deprecated_keys": list(config.metadata.deprecated_keys),
be52af70   tangwang   first commit
69
          }
26b910bd   tangwang   refactor service ...
70
71
      except HTTPException:
          raise
be52af70   tangwang   first commit
72
73
74
75
      except Exception as e:
          raise HTTPException(status_code=500, detail=str(e))
  
  
be52af70   tangwang   first commit
76
  @router.get("/stats")
26b910bd   tangwang   refactor service ...
77
  async def get_index_stats(http_request: Request):
be52af70   tangwang   first commit
78
79
80
81
      """
      Get index statistics.
      """
      try:
26b910bd   tangwang   refactor service ...
82
83
          from urllib.parse import parse_qs
          from ..app import get_es_client
be52af70   tangwang   first commit
84
85
  
          es_client = get_es_client()
26b910bd   tangwang   refactor service ...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  
          tenant_id = http_request.headers.get("X-Tenant-ID")
          if not tenant_id:
              query_string = http_request.url.query
              if query_string:
                  params = parse_qs(query_string)
                  tenant_id = params.get("tenant_id", [None])[0]
  
          if not tenant_id:
              raise HTTPException(
                  status_code=400,
                  detail="tenant_id is required. Provide it via header 'X-Tenant-ID' or query parameter 'tenant_id'",
              )
  
          index_name = get_tenant_index_name(tenant_id)
          if not es_client.client.indices.exists(index=index_name):
              raise HTTPException(
                  status_code=404,
                  detail=f"Tenant index not found: {index_name}",
              )
be52af70   tangwang   first commit
106
107
  
          # Get document count
26b910bd   tangwang   refactor service ...
108
          doc_count = es_client.client.count(index=index_name).get("count", 0)
be52af70   tangwang   first commit
109
110
111
  
          # Get index size (if available)
          try:
26b910bd   tangwang   refactor service ...
112
113
              stats = es_client.client.indices.stats(index=index_name)
              size_in_bytes = stats["indices"][index_name]["total"]["store"]["size_in_bytes"]
be52af70   tangwang   first commit
114
              size_mb = size_in_bytes / (1024 * 1024)
26b910bd   tangwang   refactor service ...
115
          except Exception:
be52af70   tangwang   first commit
116
117
118
              size_mb = None
  
          return {
26b910bd   tangwang   refactor service ...
119
120
              "tenant_id": str(tenant_id),
              "index_name": index_name,
be52af70   tangwang   first commit
121
122
123
124
125
126
              "document_count": doc_count,
              "size_mb": round(size_mb, 2) if size_mb else None
          }
  
      except Exception as e:
          raise HTTPException(status_code=500, detail=str(e))