Blame view

api/routes/admin.py 3.43 KB
be52af70   tangwang   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  """
  Admin API routes for configuration and management.
  """
  
  from fastapi import APIRouter, HTTPException
  from typing import Dict
  
  from ..models import HealthResponse, ErrorResponse
  
  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():
      """
37e994bb   tangwang   命名修改、代码清理
44
      Get current search configuration (sanitized).
be52af70   tangwang   first commit
45
46
      """
      try:
a406638e   tangwang   up
47
          from ..app import get_config
be52af70   tangwang   first commit
48
49
50
51
  
          config = get_config()
  
          return {
be52af70   tangwang   first commit
52
              "es_index_name": config.es_index_name,
33839b37   tangwang   属性值参与搜索:
53
              "num_field_boosts": len(config.field_boosts),
be52af70   tangwang   first commit
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
              "num_indexes": len(config.indexes),
              "supported_languages": config.query_config.supported_languages,
              "ranking_expression": config.ranking.expression,
              "spu_enabled": config.spu_config.enabled
          }
  
      except Exception as e:
          raise HTTPException(status_code=500, detail=str(e))
  
  
  @router.post("/rewrite-rules")
  async def update_rewrite_rules(rules: Dict[str, str]):
      """
      Update query rewrite rules.
  
      Args:
          rules: Dictionary of pattern -> replacement mappings
      """
      try:
a406638e   tangwang   up
73
          from ..app import get_query_parser
be52af70   tangwang   first commit
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  
          query_parser = get_query_parser()
          query_parser.update_rewrite_rules(rules)
  
          return {
              "status": "success",
              "message": f"Updated {len(rules)} rewrite rules"
          }
  
      except Exception as e:
          raise HTTPException(status_code=500, detail=str(e))
  
  
  @router.get("/rewrite-rules")
  async def get_rewrite_rules():
      """
      Get current query rewrite rules.
      """
      try:
a406638e   tangwang   up
93
          from ..app import get_query_parser
be52af70   tangwang   first commit
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  
          query_parser = get_query_parser()
          rules = query_parser.get_rewrite_rules()
  
          return {
              "rules": rules,
              "count": len(rules)
          }
  
      except Exception as e:
          raise HTTPException(status_code=500, detail=str(e))
  
  
  @router.get("/stats")
  async def get_index_stats():
      """
      Get index statistics.
      """
      try:
a406638e   tangwang   up
113
          from ..app import get_es_client, get_config
be52af70   tangwang   first commit
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  
          es_client = get_es_client()
          config = get_config()
  
          # Get document count
          doc_count = es_client.count(config.es_index_name)
  
          # Get index size (if available)
          try:
              stats = es_client.client.indices.stats(index=config.es_index_name)
              size_in_bytes = stats['indices'][config.es_index_name]['total']['store']['size_in_bytes']
              size_mb = size_in_bytes / (1024 * 1024)
          except:
              size_mb = None
  
          return {
              "index_name": config.es_index_name,
              "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))