Blame view

api/routes/admin.py 3.59 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  
          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",
              elasticsearch=es_status,
              customer_id=config.customer_id
          )
  
      except Exception as e:
          return HealthResponse(
              status="unhealthy",
              elasticsearch="error",
              customer_id="unknown"
          )
  
  
  @router.get("/config")
  async def get_configuration():
      """
      Get current customer configuration (sanitized).
      """
      try:
a406638e   tangwang   up
49
          from ..app import get_config
be52af70   tangwang   first commit
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  
          config = get_config()
  
          return {
              "customer_id": config.customer_id,
              "customer_name": config.customer_name,
              "es_index_name": config.es_index_name,
              "num_fields": len(config.fields),
              "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
77
          from ..app import get_query_parser
be52af70   tangwang   first commit
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  
          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
97
          from ..app import get_query_parser
be52af70   tangwang   first commit
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  
          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
117
          from ..app import get_es_client, get_config
be52af70   tangwang   first commit
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  
          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))