File size: 2,721 Bytes
54c2a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564f32e
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
from typing import List, Literal, Union
from pydantic import BaseModel, Field


# --- Schemas for Weekly Trends ---
class AnalysisResultSchema(BaseModel):
    """
    Represents the aggregated analysis counts for an entity.
    """

    total_comments: int = 0
    positive_count: int = 0
    neutral_count: int = 0
    negative_count: int = 0


class WeeklyTrendResponseSchema(BaseModel):
    """
    Represents a single trending entity in the weekly results API response.
    """

    entity_id: str = Field(..., alias="_id")
    keyword: str
    thumbnail_url: str | None = None
    analysis: AnalysisResultSchema


class WeeklyTrendListResponse(BaseModel):
    """
    Represents the top-level API response for the weekly trends endpoint.
    """

    data: List[WeeklyTrendResponseSchema]


# --- Schemas for Trend Detail ---
class InterestOverTimePoint(BaseModel):
    """
    Represents a single data point in the interest over time chart.
    """

    date: str
    value: int


class CommentSchema(BaseModel):
    """
    Represents a single representative comment.
    """

    publish_date: str
    text: str
    author: str


class RepresentativeCommentsSchema(BaseModel):
    """
    Holds lists of representative comments for each sentiment.
    """

    positive: List[CommentSchema]
    neutral: List[CommentSchema]
    negative: List[CommentSchema]


class TrendDetailResponseSchema(WeeklyTrendResponseSchema):
    """
    Represents the full detailed response for a single entity,
    inheriting basic fields from WeeklyTrendResponseSchema.
    """

    representative_video_url: str | None = None
    interest_over_time: List[InterestOverTimePoint]
    representative_comments: RepresentativeCommentsSchema


# --- Schemas for On-Demand Analysis ---
class OnDemandRequestSchema(BaseModel):
    """
    Defines the request body for an on-demand analysis request from a user.
    """

    keyword: str


# Define two distinct response schemas
class QueuedResponseSchema(BaseModel):
    """
    Response when a new job is successfully queued.
    """

    status: Literal["queued"]
    job_id: str


class FoundResponseSchema(BaseModel):
    """
    Response when an existing analysis is found.
    """

    status: Literal["found"]
    entity_id: str


# The final response model is a Union of the two possibilities
OnDemandResponseSchema = Union[QueuedResponseSchema, FoundResponseSchema]


class JobStatusResponseSchema(BaseModel):
    """
    Represents the status of an on-demand job.
    """

    job_id: str = Field(..., alias="_id")
    status: str
    keyword: str
    result: TrendDetailResponseSchema | None = None
    error_message: str | None = None  # To send error messages to the frontend