Skip to content

Commit

Permalink
feat(server): move dashboard endpoints under api v1 router
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-mcgovern committed Jan 17, 2025
1 parent 4f111e3 commit 23ec130
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 31 deletions.
48 changes: 36 additions & 12 deletions api/openapi.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
{
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
"title": "CodeGate",
"description": "Generative AI CodeGen security gateway",
"version": "0.1.7"
},
"paths": {
"/dashboard/messages": {
"/health": {
"get": {
"tags": [
"System"
],
"summary": "Health Check",
"operationId": "health_check_health_get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
}
}
}
},
"/api/v1/dashboard/messages": {
"get": {
"tags": [
"CodeGate API",
"Dashboard"
],
"summary": "Get Messages",
"description": "Get all the messages from the database and return them as a list of conversations.",
"operationId": "get_messages_dashboard_messages_get",
"operationId": "get_messages_api_v1_dashboard_messages_get",
"responses": {
"200": {
"description": "Successful Response",
Expand All @@ -23,22 +44,23 @@
"$ref": "#/components/schemas/Conversation"
},
"type": "array",
"title": "Response Get Messages Dashboard Messages Get"
"title": "Response Get Messages Api V1 Dashboard Messages Get"
}
}
}
}
}
}
},
"/dashboard/alerts": {
"/api/v1/dashboard/alerts": {
"get": {
"tags": [
"CodeGate API",
"Dashboard"
],
"summary": "Get Alerts",
"description": "Get all the messages from the database and return them as a list of conversations.",
"operationId": "get_alerts_dashboard_alerts_get",
"operationId": "get_alerts_api_v1_dashboard_alerts_get",
"responses": {
"200": {
"description": "Successful Response",
Expand All @@ -56,22 +78,23 @@
]
},
"type": "array",
"title": "Response Get Alerts Dashboard Alerts Get"
"title": "Response Get Alerts Api V1 Dashboard Alerts Get"
}
}
}
}
}
}
},
"/dashboard/alerts_notification": {
"/api/v1/dashboard/alerts_notification": {
"get": {
"tags": [
"CodeGate API",
"Dashboard"
],
"summary": "Stream Sse",
"description": "Send alerts event",
"operationId": "stream_sse_dashboard_alerts_notification_get",
"operationId": "stream_sse_api_v1_dashboard_alerts_notification_get",
"responses": {
"200": {
"description": "Successful Response",
Expand All @@ -84,13 +107,14 @@
}
}
},
"/dashboard/version": {
"/api/v1/dashboard/version": {
"get": {
"tags": [
"CodeGate API",
"Dashboard"
],
"summary": "Version Check",
"operationId": "version_check_dashboard_version_get",
"operationId": "version_check_api_v1_dashboard_version_get",
"responses": {
"200": {
"description": "Successful Response",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import structlog
from fastapi import APIRouter, Depends, FastAPI
from fastapi.responses import StreamingResponse

from codegate import __version__
from codegate.dashboard.post_processing import (

from codegate.api.dashboard.post_processing import (
parse_get_alert_conversation,
parse_messages_in_conversations,
)
from codegate.dashboard.request_models import AlertConversation, Conversation
from codegate.api.dashboard.request_models import AlertConversation, Conversation
from codegate.db.connection import DbReader, alert_queue

logger = structlog.get_logger("codegate")
Expand Down Expand Up @@ -81,7 +81,7 @@ def version_check():
latest_version_stripped = latest_version.lstrip('v')

is_latest: bool = latest_version_stripped == current_version

return {
"current_version": current_version,
"latest_version": latest_version_stripped,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import structlog

from codegate.dashboard.request_models import (
from codegate.api.dashboard.request_models import (
AlertConversation,
ChatMessage,
Conversation,
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions src/codegate/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
from codegate.api import v1_models
from codegate.db.connection import AlreadyExistsError
from codegate.workspaces.crud import WorkspaceCrud
from codegate.api.dashboard.dashboard import dashboard_router

v1 = APIRouter()
v1.include_router(dashboard_router)

wscrud = WorkspaceCrud()


Expand Down
9 changes: 2 additions & 7 deletions src/codegate/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import traceback
from unittest.mock import Mock

import structlog
from fastapi import APIRouter, FastAPI, Request
Expand All @@ -9,7 +10,6 @@

from codegate import __description__, __version__
from codegate.api.v1 import v1
from codegate.dashboard.dashboard import dashboard_router
from codegate.pipeline.factory import PipelineFactory
from codegate.providers.anthropic.provider import AnthropicProvider
from codegate.providers.llamacpp.provider import LlamaCppProvider
Expand Down Expand Up @@ -97,7 +97,6 @@ async def health_check():
return {"status": "healthy"}

app.include_router(system_router)
app.include_router(dashboard_router)

# CodeGate API
app.include_router(v1, prefix="/api/v1", tags=["CodeGate API"])
Expand All @@ -106,11 +105,7 @@ async def health_check():


def generate_openapi():
# Create a temporary FastAPI app instance
app = FastAPI()

app.include_router(dashboard_router)
app.include_router(v1, prefix="/api/v1", tags=["CodeGate API"])
app = init_app(Mock(spec=PipelineFactory))

# Generate OpenAPI JSON
openapi_schema = app.openapi()
Expand Down
8 changes: 4 additions & 4 deletions tests/dashboard/test_post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import pytest

from codegate.dashboard.post_processing import (
from codegate.api.dashboard.post_processing import (
_get_question_answer,
_group_partial_messages,
_is_system_prompt,
parse_output,
parse_request,
)
from codegate.dashboard.request_models import (
from codegate.api.dashboard.request_models import (
PartialQuestions,
)
from codegate.db.models import GetPromptWithOutputsRow
Expand Down Expand Up @@ -162,10 +162,10 @@ async def test_parse_output(output_dict, expected_str):
)
async def test_get_question_answer(request_msg_list, output_msg_str, row):
with patch(
"codegate.dashboard.post_processing.parse_request", new_callable=AsyncMock
"codegate.api.dashboard.post_processing.parse_request", new_callable=AsyncMock
) as mock_parse_request:
with patch(
"codegate.dashboard.post_processing.parse_output", new_callable=AsyncMock
"codegate.api.dashboard.post_processing.parse_output", new_callable=AsyncMock
) as mock_parse_output:
# Set return values for the mocks
mock_parse_request.return_value = request_msg_list
Expand Down
6 changes: 3 additions & 3 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ def test_health_check(test_client: TestClient) -> None:
assert response.status_code == 200
assert response.json() == {"status": "healthy"}

@patch("codegate.dashboard.dashboard.fetch_latest_version", return_value="foo")
@patch("codegate.api.dashboard.fetch_latest_version", return_value="foo")
def test_version_endpoint(mock_fetch_latest_version, test_client: TestClient) -> None:
"""Test the version endpoint."""
response = test_client.get("/dashboard/version")
response = test_client.get("/api/v1/dashboard/version")
assert response.status_code == 200

response_data = response.json()
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_dashboard_routes(mock_pipeline_factory) -> None:
routes = [route.path for route in app.routes]

# Verify dashboard endpoints are included
dashboard_routes = [route for route in routes if route.startswith("/dashboard")]
dashboard_routes = [route for route in routes if route.startswith("/api/v1/dashboard")]
assert len(dashboard_routes) > 0


Expand Down

0 comments on commit 23ec130

Please sign in to comment.