-
Notifications
You must be signed in to change notification settings - Fork 2
/
readiness_routes.py
137 lines (114 loc) · 3.9 KB
/
readiness_routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
49
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from fastapi import APIRouter, Request
import os
from db_utils import validate_user, get_db_type_creds
from generic_utils import make_request, get_api_key_from_key_name
from defog import Defog
from fastapi.responses import JSONResponse
DEFOG_BASE_URL = os.environ.get("DEFOG_BASE_URL", "https://api.defog.ai")
router = APIRouter()
@router.post("/readiness/basic")
async def check_basic_readiness(request: Request):
params = await request.json()
token = params.get("token")
dev = params.get("dev")
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
key_name = params.get("key_name")
api_key = get_api_key_from_key_name(key_name)
metadata_ready = False
golden_queries_ready = False
glossary_ready = False
r = await make_request(
f"{DEFOG_BASE_URL}/get_metadata", {"api_key": api_key, "dev": dev}
)
if r["table_metadata"]:
metadata_ready = True
if r["glossary"]:
glossary_ready = True
r = await make_request(
f"{DEFOG_BASE_URL}/get_golden_queries", {"api_key": api_key, "dev": dev}
)
if r["golden_queries"] and len(r["golden_queries"]) > 0:
golden_queries_ready = True
return {
"success": True,
"metadata": metadata_ready,
"golden_queries": golden_queries_ready,
"glossary": glossary_ready,
}
@router.post("/readiness/check_golden_queries_validity")
async def check_golden_queries_validity(request: Request):
params = await request.json()
token = params.get("token")
dev = params.get("dev")
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
key_name = params.get("key_name")
api_key = get_api_key_from_key_name(key_name)
res = get_db_type_creds(api_key)
if res:
db_type, db_creds = res
else:
return {"error": "no db creds found"}
resp = await make_request(
f"{DEFOG_BASE_URL}/check_gold_queries_valid",
json={"api_key": api_key, "db_type": db_type, "dev": dev},
)
return resp
@router.post("/readiness/check_instruction_consistency")
async def check_glossary_consistency(request: Request):
params = await request.json()
token = params.get("token")
dev = params.get("dev")
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
key_name = params.get("key_name")
api_key = get_api_key_from_key_name(key_name)
resp = await make_request(
f"{DEFOG_BASE_URL}/check_glossary_consistency",
json={"api_key": api_key, "dev": dev},
)
return resp
@router.post("/readiness/check_golden_query_coverage")
async def check_golden_query_coverage(request: Request):
params = await request.json()
token = params.get("token")
dev = params.get("dev")
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
key_name = params.get("key_name")
api_key = get_api_key_from_key_name(key_name)
res = get_db_type_creds(api_key)
if res:
db_type, db_creds = res
else:
return {"error": "no db creds found"}
resp = await make_request(
f"{DEFOG_BASE_URL}/get_golden_queries_coverage",
json={"api_key": api_key, "dev": dev, "db_type": db_type},
)
return resp