File size: 11,377 Bytes
35d7096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
CQRS API endpoints for command and query operations.

This module provides RESTful endpoints that use the CQRS pattern
for better scalability and separation of concerns.
"""

from typing import Dict, Any, Optional
from datetime import datetime

from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
from pydantic import BaseModel

from src.core import get_logger
from src.api.auth import get_current_user
from src.infrastructure.cqrs.commands import (
    CommandBus,
    CreateInvestigationCommand,
    UpdateInvestigationCommand,
    CancelInvestigationCommand,
    ExecuteAgentTaskCommand,
    SendChatMessageCommand
)
from src.infrastructure.cqrs.queries import (
    QueryBus,
    GetInvestigationByIdQuery,
    SearchInvestigationsQuery,
    GetInvestigationStatsQuery,
    SearchContractsQuery,
    GetAgentPerformanceQuery
)
from src.infrastructure.events.event_bus import get_event_bus

logger = get_logger(__name__)

router = APIRouter(prefix="/api/v1/cqrs", tags=["CQRS"])


# Request/Response models
class CreateInvestigationRequest(BaseModel):
    """Request to create investigation."""
    query: str
    data_sources: Optional[list[str]] = None
    priority: str = "medium"


class UpdateInvestigationRequest(BaseModel):
    """Request to update investigation."""
    status: str
    results: Optional[Dict[str, Any]] = None


class SearchInvestigationsRequest(BaseModel):
    """Request to search investigations."""
    filters: Dict[str, Any] = {}
    sort_by: str = "created_at"
    sort_order: str = "desc"
    limit: int = 20
    offset: int = 0


class SearchContractsRequest(BaseModel):
    """Request to search contracts."""
    search_term: Optional[str] = None
    orgao: Optional[str] = None
    min_value: Optional[float] = None
    max_value: Optional[float] = None
    year: Optional[int] = None
    limit: int = 50
    offset: int = 0


class ExecuteAgentTaskRequest(BaseModel):
    """Request to execute agent task."""
    agent_name: str
    task_type: str
    payload: Dict[str, Any]
    timeout: Optional[float] = None


# Global instances
command_bus: Optional[CommandBus] = None
query_bus: Optional[QueryBus] = None


async def get_command_bus() -> CommandBus:
    """Get command bus instance."""
    global command_bus
    if command_bus is None:
        event_bus = await get_event_bus()
        command_bus = CommandBus(event_bus)
    return command_bus


async def get_query_bus() -> QueryBus:
    """Get query bus instance."""
    global query_bus
    if query_bus is None:
        query_bus = QueryBus()
    return query_bus


# Command endpoints
@router.post("/investigations", response_model=Dict[str, Any])
async def create_investigation(
    request: CreateInvestigationRequest,
    background_tasks: BackgroundTasks,
    current_user = Depends(get_current_user),
    cmd_bus: CommandBus = Depends(get_command_bus)
):
    """
    Create a new investigation using CQRS command.
    
    This endpoint demonstrates the command side of CQRS:
    - Accepts write operations
    - Publishes events
    - Returns minimal response
    """
    command = CreateInvestigationCommand(
        user_id=current_user["sub"],
        query=request.query,
        data_sources=request.data_sources,
        priority=request.priority
    )
    
    result = await cmd_bus.execute(command)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {
        "investigation_id": result.data["investigation_id"],
        "command_id": result.command_id,
        "events_published": result.events_published
    }


@router.put("/investigations/{investigation_id}", response_model=Dict[str, Any])
async def update_investigation(
    investigation_id: str,
    request: UpdateInvestigationRequest,
    current_user = Depends(get_current_user),
    cmd_bus: CommandBus = Depends(get_command_bus)
):
    """Update investigation status."""
    command = UpdateInvestigationCommand(
        user_id=current_user["sub"],
        investigation_id=investigation_id,
        status=request.status,
        results=request.results
    )
    
    result = await cmd_bus.execute(command)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {"success": True, "command_id": result.command_id}


@router.delete("/investigations/{investigation_id}", response_model=Dict[str, Any])
async def cancel_investigation(
    investigation_id: str,
    reason: Optional[str] = None,
    current_user = Depends(get_current_user),
    cmd_bus: CommandBus = Depends(get_command_bus)
):
    """Cancel an investigation."""
    command = CancelInvestigationCommand(
        user_id=current_user["sub"],
        investigation_id=investigation_id,
        reason=reason
    )
    
    result = await cmd_bus.execute(command)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {"success": True, "command_id": result.command_id}


@router.post("/agents/execute", response_model=Dict[str, Any])
async def execute_agent_task(
    request: ExecuteAgentTaskRequest,
    background_tasks: BackgroundTasks,
    current_user = Depends(get_current_user),
    cmd_bus: CommandBus = Depends(get_command_bus)
):
    """Execute an agent task."""
    command = ExecuteAgentTaskCommand(
        user_id=current_user["sub"],
        agent_name=request.agent_name,
        task_type=request.task_type,
        payload=request.payload,
        timeout=request.timeout
    )
    
    result = await cmd_bus.execute(command)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {
        "success": True,
        "command_id": result.command_id,
        "task_id": result.data.get("task_id") if result.data else None
    }


# Query endpoints
@router.get("/investigations/{investigation_id}", response_model=Dict[str, Any])
async def get_investigation(
    investigation_id: str,
    include_findings: bool = True,
    include_anomalies: bool = True,
    current_user = Depends(get_current_user),
    q_bus: QueryBus = Depends(get_query_bus)
):
    """
    Get investigation by ID using CQRS query.
    
    This endpoint demonstrates the query side of CQRS:
    - Optimized for reads
    - Uses caching
    - Returns denormalized data
    """
    query = GetInvestigationByIdQuery(
        user_id=current_user["sub"],
        investigation_id=investigation_id,
        include_findings=include_findings,
        include_anomalies=include_anomalies
    )
    
    result = await q_bus.execute(query)
    
    if not result.success:
        raise HTTPException(status_code=404, detail=result.error)
    
    return {
        "investigation": result.data,
        "from_cache": result.from_cache,
        "execution_time_ms": result.execution_time_ms
    }


@router.post("/investigations/search", response_model=Dict[str, Any])
async def search_investigations(
    request: SearchInvestigationsRequest,
    current_user = Depends(get_current_user),
    q_bus: QueryBus = Depends(get_query_bus)
):
    """Search investigations with filters."""
    query = SearchInvestigationsQuery(
        user_id=current_user["sub"],
        filters=request.filters,
        sort_by=request.sort_by,
        sort_order=request.sort_order,
        limit=request.limit,
        offset=request.offset
    )
    
    result = await q_bus.execute(query)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {
        "investigations": result.data,
        "metadata": result.metadata,
        "execution_time_ms": result.execution_time_ms
    }


@router.get("/investigations/stats", response_model=Dict[str, Any])
async def get_investigation_stats(
    date_from: Optional[datetime] = None,
    date_to: Optional[datetime] = None,
    current_user = Depends(get_current_user),
    q_bus: QueryBus = Depends(get_query_bus)
):
    """Get investigation statistics."""
    query = GetInvestigationStatsQuery(
        user_id=current_user["sub"],
        date_from=date_from,
        date_to=date_to
    )
    
    result = await q_bus.execute(query)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {
        "stats": result.data,
        "from_cache": result.from_cache,
        "execution_time_ms": result.execution_time_ms
    }


@router.post("/contracts/search", response_model=Dict[str, Any])
async def search_contracts(
    request: SearchContractsRequest,
    current_user = Depends(get_current_user),
    q_bus: QueryBus = Depends(get_query_bus)
):
    """Search contracts with filters."""
    query = SearchContractsQuery(
        user_id=current_user["sub"],
        search_term=request.search_term,
        orgao=request.orgao,
        min_value=request.min_value,
        max_value=request.max_value,
        year=request.year,
        limit=request.limit,
        offset=request.offset,
        use_cache=True,
        cache_ttl=300  # 5 minutes
    )
    
    result = await q_bus.execute(query)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {
        "contracts": result.data,
        "from_cache": result.from_cache,
        "execution_time_ms": result.execution_time_ms
    }


@router.get("/agents/performance", response_model=Dict[str, Any])
async def get_agent_performance(
    agent_name: Optional[str] = None,
    time_period: str = "1h",
    current_user = Depends(get_current_user),
    q_bus: QueryBus = Depends(get_query_bus)
):
    """Get agent performance metrics."""
    query = GetAgentPerformanceQuery(
        user_id=current_user["sub"],
        agent_name=agent_name,
        time_period=time_period,
        use_cache=True,
        cache_ttl=60  # 1 minute for recent metrics
    )
    
    result = await q_bus.execute(query)
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.error)
    
    return {
        "performance": result.data,
        "from_cache": result.from_cache,
        "execution_time_ms": result.execution_time_ms
    }


# Bus statistics endpoints
@router.get("/stats/commands", response_model=Dict[str, Any])
async def get_command_bus_stats(
    current_user = Depends(get_current_user),
    cmd_bus: CommandBus = Depends(get_command_bus)
):
    """Get command bus statistics."""
    return cmd_bus.get_stats()


@router.get("/stats/queries", response_model=Dict[str, Any])
async def get_query_bus_stats(
    current_user = Depends(get_current_user),
    q_bus: QueryBus = Depends(get_query_bus)
):
    """Get query bus statistics."""
    return q_bus.get_stats()


# Health check
@router.get("/health", response_model=Dict[str, Any])
async def cqrs_health_check():
    """Check CQRS system health."""
    try:
        cmd_bus = await get_command_bus()
        q_bus = await get_query_bus()
        event_bus = await get_event_bus()
        
        return {
            "status": "healthy",
            "command_bus": "ready",
            "query_bus": "ready",
            "event_bus": "ready",
            "event_bus_stats": event_bus.get_stats()
        }
    except Exception as e:
        return {
            "status": "unhealthy",
            "error": str(e)
        }