File size: 2,259 Bytes
98b5a78
 
 
 
 
 
 
c988aaf
98b5a78
 
 
c988aaf
98b5a78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c988aaf
 
 
 
 
 
 
 
 
 
98b5a78
 
c988aaf
98b5a78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c988aaf
 
 
 
 
 
 
 
 
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
"""Feedback endpoints for user feedback collection."""

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Optional
import logging
from datetime import datetime
from src.database import FeedbackStore

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/feedback", tags=["feedback"])
feedback_store = FeedbackStore()


class FeedbackRequest(BaseModel):
    """User feedback request model."""
    query: str
    response: str
    rating: Optional[int] = None  # 1-5 star rating
    comments: Optional[str] = None
    user_id: Optional[str] = None


class FeedbackResponse(BaseModel):
    """Response model for feedback submission."""
    status: str
    message: str
    feedback_id: str


@router.post("", response_model=FeedbackResponse)
async def submit_feedback(feedback: FeedbackRequest):
    """
    Submit user feedback.

    Args:
        feedback: User feedback data

    Returns:
        Feedback submission confirmation
    """
    try:
        feedback_data = {
            "query": feedback.query,
            "response": feedback.response,
            "rating": feedback.rating,
            "comments": feedback.comments,
            "user_id": feedback.user_id,
            "created_at": datetime.utcnow()
        }

        feedback_id = feedback_store.save_feedback(feedback_data)

        logger.info(
            f"Feedback stored: {feedback_id} | "
            f"Rating: {feedback.rating} | "
            f"User: {feedback.user_id}"
        )

        return FeedbackResponse(
            status="success",
            message="Feedback received successfully",
            feedback_id=feedback_id
        )
    except Exception as e:
        logger.error(f"Error submitting feedback: {e}")
        raise HTTPException(status_code=500, detail="Failed to submit feedback")


@router.get("/stats")
async def get_feedback_stats():
    """Get feedback statistics."""
    try:
        stats = feedback_store.get_feedback_stats()
        return {
            "status": "success",
            "data": stats
        }
    except Exception as e:
        logger.error(f"Error getting feedback stats: {e}")
        raise HTTPException(status_code=500, detail="Failed to get feedback statistics")