1
1
import asyncio
2
- import json
3
2
from typing import AsyncGenerator , List , Optional
4
3
5
4
import requests
6
5
import structlog
7
- from fastapi import APIRouter , Depends , FastAPI
6
+ from fastapi import APIRouter , Depends
8
7
from fastapi .responses import StreamingResponse
8
+ from fastapi .routing import APIRoute
9
9
10
10
from codegate import __version__
11
- from codegate .dashboard .post_processing import (
11
+ from codegate .api . dashboard .post_processing import (
12
12
parse_get_alert_conversation ,
13
13
parse_messages_in_conversations ,
14
14
)
15
- from codegate .dashboard .request_models import AlertConversation , Conversation
15
+ from codegate .api . dashboard .request_models import AlertConversation , Conversation
16
16
from codegate .db .connection import DbReader , alert_queue
17
17
18
18
logger = structlog .get_logger ("codegate" )
19
19
20
- dashboard_router = APIRouter (tags = [ "Dashboard" ] )
20
+ dashboard_router = APIRouter ()
21
21
db_reader = None
22
22
23
+
24
+ def uniq_name (route : APIRoute ):
25
+ return f"v1_{ route .name } "
26
+
27
+
23
28
def get_db_reader ():
24
29
global db_reader
25
30
if db_reader is None :
26
31
db_reader = DbReader ()
27
32
return db_reader
28
33
34
+
29
35
def fetch_latest_version () -> str :
30
36
url = "https://api.github.com/repos/stacklok/codegate/releases/latest"
31
- headers = {
32
- "Accept" : "application/vnd.github+json" ,
33
- "X-GitHub-Api-Version" : "2022-11-28"
34
- }
37
+ headers = {"Accept" : "application/vnd.github+json" , "X-GitHub-Api-Version" : "2022-11-28" }
35
38
response = requests .get (url , headers = headers , timeout = 5 )
36
39
response .raise_for_status ()
37
40
data = response .json ()
38
41
return data .get ("tag_name" , "unknown" )
39
42
40
- @dashboard_router .get ("/dashboard/messages" )
43
+
44
+ @dashboard_router .get (
45
+ "/dashboard/messages" , tags = ["Dashboard" ], generate_unique_id_function = uniq_name
46
+ )
41
47
def get_messages (db_reader : DbReader = Depends (get_db_reader )) -> List [Conversation ]:
42
48
"""
43
49
Get all the messages from the database and return them as a list of conversations.
@@ -47,7 +53,9 @@ def get_messages(db_reader: DbReader = Depends(get_db_reader)) -> List[Conversat
47
53
return asyncio .run (parse_messages_in_conversations (prompts_outputs ))
48
54
49
55
50
- @dashboard_router .get ("/dashboard/alerts" )
56
+ @dashboard_router .get (
57
+ "/dashboard/alerts" , tags = ["Dashboard" ], generate_unique_id_function = uniq_name
58
+ )
51
59
def get_alerts (db_reader : DbReader = Depends (get_db_reader )) -> List [Optional [AlertConversation ]]:
52
60
"""
53
61
Get all the messages from the database and return them as a list of conversations.
@@ -65,21 +73,26 @@ async def generate_sse_events() -> AsyncGenerator[str, None]:
65
73
yield f"data: { message } \n \n "
66
74
67
75
68
- @dashboard_router .get ("/dashboard/alerts_notification" )
76
+ @dashboard_router .get (
77
+ "/dashboard/alerts_notification" , tags = ["Dashboard" ], generate_unique_id_function = uniq_name
78
+ )
69
79
async def stream_sse ():
70
80
"""
71
81
Send alerts event
72
82
"""
73
83
return StreamingResponse (generate_sse_events (), media_type = "text/event-stream" )
74
84
75
- @dashboard_router .get ("/dashboard/version" )
85
+
86
+ @dashboard_router .get (
87
+ "/dashboard/version" , tags = ["Dashboard" ], generate_unique_id_function = uniq_name
88
+ )
76
89
def version_check ():
77
90
try :
78
91
latest_version = fetch_latest_version ()
79
92
80
93
# normalize the versions as github will return them with a 'v' prefix
81
- current_version = __version__ .lstrip ('v' )
82
- latest_version_stripped = latest_version .lstrip ('v' )
94
+ current_version = __version__ .lstrip ("v" )
95
+ latest_version_stripped = latest_version .lstrip ("v" )
83
96
84
97
is_latest : bool = latest_version_stripped == current_version
85
98
@@ -95,28 +108,13 @@ def version_check():
95
108
"current_version" : __version__ ,
96
109
"latest_version" : "unknown" ,
97
110
"is_latest" : None ,
98
- "error" : "An error occurred while fetching the latest version"
111
+ "error" : "An error occurred while fetching the latest version" ,
99
112
}
100
113
except Exception as e :
101
114
logger .error (f"Unexpected error: { str (e )} " )
102
115
return {
103
116
"current_version" : __version__ ,
104
117
"latest_version" : "unknown" ,
105
118
"is_latest" : None ,
106
- "error" : "An unexpected error occurred"
119
+ "error" : "An unexpected error occurred" ,
107
120
}
108
-
109
-
110
- def generate_openapi ():
111
- # Create a temporary FastAPI app instance
112
- app = FastAPI ()
113
-
114
- # Include your defined router
115
- app .include_router (dashboard_router )
116
-
117
- # Generate OpenAPI JSON
118
- openapi_schema = app .openapi ()
119
-
120
- # Convert the schema to JSON string for easier handling or storage
121
- openapi_json = json .dumps (openapi_schema , indent = 2 )
122
- print (openapi_json )
0 commit comments