61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
from datetime import datetime, timezone
|
|
from fastapi import APIRouter, Request, HTTPException, Depends
|
|
from fastapi.responses import JSONResponse
|
|
from pydantic import BaseModel
|
|
from llm.models import JiraWebhookPayload
|
|
from shared_store import requests_queue, ProcessingRequest
|
|
from loguru import logger
|
|
|
|
jira_router = APIRouter(
|
|
prefix="/jira",
|
|
tags=["Jira"]
|
|
)
|
|
|
|
queue_router = APIRouter(
|
|
prefix="/queue",
|
|
tags=["Queue"]
|
|
)
|
|
|
|
@jira_router.post("/sendRequest", status_code=201)
|
|
async def send_jira_request(payload: JiraWebhookPayload):
|
|
"""Send requests to add to queue for further processing"""
|
|
request_id = requests_queue.add_request(payload.model_dump())
|
|
return {"request_id": request_id}
|
|
|
|
class GetResponseRequest(BaseModel):
|
|
issueKey: str
|
|
|
|
@jira_router.post("/getResponse")
|
|
async def get_jira_response(request: GetResponseRequest):
|
|
"""Get response attribute provided by ollama for a given issueKey."""
|
|
if requests_queue.are_there_any_open_requests_for_issue_key(request.issueKey):
|
|
raise HTTPException(
|
|
status_code=409,
|
|
detail=f"There are still pending or processing requests for issueKey: {request.issueKey}. Please wait for them to be processed."
|
|
)
|
|
|
|
matched_request = requests_queue.get_latest_completed_by_issue_key(request.issueKey)
|
|
if not matched_request:
|
|
raise HTTPException(status_code=404, detail=f"No completed request found for issueKey: {request.issueKey}")
|
|
return matched_request.response if matched_request.response else "No response yet"
|
|
|
|
|
|
@queue_router.get("/getAll")
|
|
async def get_all_requests_in_queue():
|
|
"""Gets all requests"""
|
|
all_requests = requests_queue.get_all_requests()
|
|
return {"requests": all_requests}
|
|
|
|
@queue_router.get("/getPending")
|
|
async def get_pending_requests_in_queue():
|
|
"""Gets all requests waiting to be processed"""
|
|
all_requests = requests_queue.get_all_requests()
|
|
pending = [req for req in all_requests if req.status == "pending"]
|
|
return {"requests": pending}
|
|
|
|
@queue_router.delete("/clearAll")
|
|
async def clear_all_requests_in_queue():
|
|
"""Clear all the requests from the queue"""
|
|
requests_queue.clear_all_requests()
|
|
return {"status": "cleared"}
|