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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""
Usage Tracking for MCP Server

Provides decorators and utilities for tracking deployment usage statistics.
Tracks request counts, response times, tool usage, and client information.
"""

import time
import functools
from typing import Optional, Callable, Any, Dict
from datetime import datetime

from sqlalchemy.orm import Session

from .database import get_db, db_transaction
from .models import UsageEvent, Deployment


# ============================================================================
# Usage Tracking Decorator
# ============================================================================


def track_usage(
    deployment_id: Optional[str] = None,
    tool_name: Optional[str] = None,
    client_id_getter: Optional[Callable] = None,
):
    """
    Decorator to track usage of MCP server functions.

    Automatically records:
    - Execution time
    - Success/failure status
    - Tool name
    - Client identifier

    Args:
        deployment_id: Deployment ID (can be None if extracted from function args)
        tool_name: Name of the tool/function being tracked
        client_id_getter: Optional function to extract client ID from request

    Example:
        >>> @track_usage(tool_name="get_cat_facts")
        >>> def get_cat_facts(deployment_id: str, count: int = 5):
        >>>     # Function implementation
        >>>     pass

        >>> @track_usage(
        >>>     tool_name="custom_tool",
        >>>     client_id_getter=lambda req: req.headers.get("X-Client-ID")
        >>> )
        >>> def custom_tool(request, deployment_id: str):
        >>>     # Function implementation
        >>>     pass
    """

    def decorator(func: Callable) -> Callable:
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # Extract deployment_id from arguments if not provided
            dep_id = deployment_id
            if dep_id is None:
                # Try to get from kwargs
                dep_id = kwargs.get("deployment_id")
                # Try to get from first positional arg if it's a string
                if dep_id is None and args and isinstance(args[0], str):
                    dep_id = args[0]

            # Extract client_id if getter provided
            client_id = None
            if client_id_getter:
                try:
                    # Try to get client_id from args/kwargs
                    if args:
                        client_id = client_id_getter(args[0])
                    elif kwargs:
                        client_id = client_id_getter(kwargs)
                except Exception:
                    client_id = None

            # Start timing
            start_time = time.time()
            success = True
            error_msg = None
            result = None

            try:
                # Execute the function
                result = func(*args, **kwargs)
                return result

            except Exception as e:
                success = False
                error_msg = str(e)
                raise

            finally:
                # Calculate duration
                duration_ms = int((time.time() - start_time) * 1000)

                # Record usage asynchronously (non-blocking)
                if dep_id:
                    try:
                        record_usage_event(
                            deployment_id=dep_id,
                            tool_name=tool_name or func.__name__,
                            client_id=client_id,
                            duration_ms=duration_ms,
                            success=success,
                            error_message=error_msg,
                        )
                    except Exception as tracking_error:
                        # Don't let tracking errors affect the main function
                        print(f"Warning: Failed to record usage: {tracking_error}")

        return wrapper

    return decorator


# ============================================================================
# Usage Recording Functions
# ============================================================================


def record_usage_event(
    deployment_id: str,
    tool_name: Optional[str] = None,
    client_id: Optional[str] = None,
    duration_ms: Optional[int] = None,
    success: bool = True,
    error_message: Optional[str] = None,
    metadata: Optional[Dict[str, Any]] = None,
) -> bool:
    """
    Record a usage event in the database.

    Args:
        deployment_id: Deployment identifier
        tool_name: Name of tool/function called
        client_id: Client identifier
        duration_ms: Request duration in milliseconds
        success: Whether request succeeded
        error_message: Error message if failed
        metadata: Additional metadata

    Returns:
        bool: True if recorded successfully, False otherwise

    Example:
        >>> record_usage_event(
        >>>     deployment_id="deploy-mcp-example-123456",
        >>>     tool_name="get_cat_facts",
        >>>     duration_ms=150,
        >>>     success=True
        >>> )
    """
    try:
        with db_transaction() as db:
            UsageEvent.record_usage(
                db=db,
                deployment_id=deployment_id,
                tool_name=tool_name,
                client_id=client_id,
                duration_ms=duration_ms,
                success=success,
                error_message=error_message,
                metadata=metadata,
            )
        return True
    except Exception as e:
        print(f"Error recording usage event: {e}")
        return False


def increment_deployment_counter(deployment_id: str, duration_ms: Optional[int] = None):
    """
    Increment deployment usage counter and update statistics.

    This is a lightweight alternative to recording full events.
    Updates total_requests, last_used_at, and avg_response_time_ms.

    Args:
        deployment_id: Deployment identifier
        duration_ms: Optional response time to update average

    Returns:
        bool: True if updated successfully, False otherwise

    Example:
        >>> increment_deployment_counter("deploy-mcp-example-123456", 150)
    """
    try:
        with db_transaction() as db:
            deployment = Deployment.get_by_deployment_id(db, deployment_id)
            if deployment:
                if duration_ms is not None:
                    deployment.update_usage_stats(duration_ms)
                else:
                    deployment.total_requests += 1
                    deployment.last_used_at = datetime.utcnow()
        return True
    except Exception as e:
        print(f"Error incrementing deployment counter: {e}")
        return False


# ============================================================================
# Statistics Retrieval
# ============================================================================


def get_deployment_statistics(
    deployment_id: str,
    days: int = 30,
) -> Optional[Dict[str, Any]]:
    """
    Get usage statistics for a deployment.

    Args:
        deployment_id: Deployment identifier
        days: Number of days to look back

    Returns:
        dict: Usage statistics or None if error

    Example:
        >>> stats = get_deployment_statistics("deploy-mcp-example-123456", days=7)
        >>> print(f"Total requests: {stats['total_requests']}")
        >>> print(f"Success rate: {stats['success_rate_percent']}%")
    """
    try:
        with get_db() as db:
            stats = UsageEvent.get_stats(db, deployment_id, days)
            return stats
    except Exception as e:
        print(f"Error getting deployment statistics: {e}")
        return None


def get_tool_usage_breakdown(
    deployment_id: str,
    days: int = 30,
    limit: int = 10,
) -> Optional[list]:
    """
    Get breakdown of tool usage for a deployment.

    Args:
        deployment_id: Deployment identifier
        days: Number of days to look back
        limit: Maximum number of tools to return

    Returns:
        list: List of dicts with tool_name and count

    Example:
        >>> tools = get_tool_usage_breakdown("deploy-mcp-example-123456")
        >>> for tool in tools:
        >>>     print(f"{tool['tool_name']}: {tool['count']} requests")
    """
    try:
        from sqlalchemy import and_, func
        from datetime import datetime, timedelta

        with get_db() as db:
            cutoff_date = datetime.utcnow() - timedelta(days=days)

            tool_stats = (
                db.query(
                    UsageEvent.tool_name,
                    func.count(UsageEvent.id).label("count"),
                )
                .filter(
                    and_(
                        UsageEvent.deployment_id == deployment_id,
                        UsageEvent.timestamp >= cutoff_date,
                        UsageEvent.tool_name.isnot(None),
                    )
                )
                .group_by(UsageEvent.tool_name)
                .order_by(func.count(UsageEvent.id).desc())
                .limit(limit)
                .all()
            )

            return [
                {"tool_name": tool, "count": count}
                for tool, count in tool_stats
            ]
    except Exception as e:
        print(f"Error getting tool usage breakdown: {e}")
        return None


def get_usage_timeline(
    deployment_id: str,
    days: int = 7,
    granularity: str = "day",
) -> Optional[list]:
    """
    Get usage timeline for a deployment.

    Args:
        deployment_id: Deployment identifier
        days: Number of days to look back
        granularity: 'hour' or 'day'

    Returns:
        list: List of dicts with timestamp and count

    Example:
        >>> timeline = get_usage_timeline("deploy-mcp-example-123456", days=7)
        >>> for entry in timeline:
        >>>     print(f"{entry['date']}: {entry['requests']} requests")
    """
    try:
        from sqlalchemy import and_, func
        from datetime import datetime, timedelta

        with get_db() as db:
            cutoff_date = datetime.utcnow() - timedelta(days=days)

            # Choose date truncation based on granularity
            if granularity == "hour":
                time_bucket = func.date_trunc("hour", UsageEvent.timestamp)
            else:
                time_bucket = func.date_trunc("day", UsageEvent.timestamp)

            timeline_data = (
                db.query(
                    time_bucket.label("time_bucket"),
                    func.count(UsageEvent.id).label("count"),
                )
                .filter(
                    and_(
                        UsageEvent.deployment_id == deployment_id,
                        UsageEvent.timestamp >= cutoff_date,
                    )
                )
                .group_by(time_bucket)
                .order_by(time_bucket)
                .all()
            )

            return [
                {
                    "timestamp": bucket.isoformat() if bucket else None,
                    "requests": count,
                }
                for bucket, count in timeline_data
            ]
    except Exception as e:
        print(f"Error getting usage timeline: {e}")
        return None


def get_client_statistics(
    deployment_id: str,
    days: int = 30,
    limit: int = 10,
) -> Optional[list]:
    """
    Get client usage statistics for a deployment.

    Args:
        deployment_id: Deployment identifier
        days: Number of days to look back
        limit: Maximum number of clients to return

    Returns:
        list: List of dicts with client_id and count

    Example:
        >>> clients = get_client_statistics("deploy-mcp-example-123456")
        >>> for client in clients:
        >>>     print(f"Client {client['client_id']}: {client['count']} requests")
    """
    try:
        from sqlalchemy import and_, func
        from datetime import datetime, timedelta

        with get_db() as db:
            cutoff_date = datetime.utcnow() - timedelta(days=days)

            client_stats = (
                db.query(
                    UsageEvent.client_id,
                    func.count(UsageEvent.id).label("count"),
                )
                .filter(
                    and_(
                        UsageEvent.deployment_id == deployment_id,
                        UsageEvent.timestamp >= cutoff_date,
                        UsageEvent.client_id.isnot(None),
                    )
                )
                .group_by(UsageEvent.client_id)
                .order_by(func.count(UsageEvent.id).desc())
                .limit(limit)
                .all()
            )

            return [
                {"client_id": client, "count": count}
                for client, count in client_stats
            ]
    except Exception as e:
        print(f"Error getting client statistics: {e}")
        return None


# ============================================================================
# Utility Functions
# ============================================================================


def get_all_deployments_stats() -> Optional[list]:
    """
    Get quick statistics for all active deployments.

    Returns:
        list: List of dicts with deployment info and stats

    Example:
        >>> all_stats = get_all_deployments_stats()
        >>> for deployment in all_stats:
        >>>     print(f"{deployment['server_name']}: {deployment['total_requests']} requests")
    """
    try:
        with get_db() as db:
            deployments = Deployment.get_active_deployments(db)
            return [
                {
                    "deployment_id": dep.deployment_id,
                    "server_name": dep.server_name,
                    "total_requests": dep.total_requests or 0,
                    "last_used_at": dep.last_used_at.isoformat() if dep.last_used_at else None,
                    "avg_response_time_ms": dep.avg_response_time_ms,
                    "status": dep.status,
                }
                for dep in deployments
            ]
    except Exception as e:
        print(f"Error getting all deployments stats: {e}")
        return None