File size: 5,342 Bytes
cf699d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Performance optimization utilities for the Todo application
"""
from functools import lru_cache
from typing import Optional
import time


class PerformanceMonitor:
    """Monitor and log performance metrics"""

    def __init__(self):
        self.metrics = {}

    def record_metric(self, operation: str, duration: float):
        """Record a performance metric"""
        if operation not in self.metrics:
            self.metrics[operation] = {
                'count': 0,
                'total_time': 0,
                'min_time': float('inf'),
                'max_time': 0
            }

        self.metrics[operation]['count'] += 1
        self.metrics[operation]['total_time'] += duration
        self.metrics[operation]['min_time'] = min(self.metrics[operation]['min_time'], duration)
        self.metrics[operation]['max_time'] = max(self.metrics[operation]['max_time'], duration)

    def get_metrics(self, operation: Optional[str] = None):
        """Get performance metrics"""
        if operation:
            if operation in self.metrics:
                metric = self.metrics[operation]
                return {
                    'operation': operation,
                    'count': metric['count'],
                    'avg_time': metric['total_time'] / metric['count'],
                    'min_time': metric['min_time'],
                    'max_time': metric['max_time']
                }
            return None

        return {
            op: {
                'count': m['count'],
                'avg_time': m['total_time'] / m['count'],
                'min_time': m['min_time'],
                'max_time': m['max_time']
            }
            for op, m in self.metrics.items()
        }


# Global performance monitor instance
performance_monitor = PerformanceMonitor()


def measure_performance(operation_name: str):
    """Decorator to measure function performance"""
    def decorator(func):
        def wrapper(*args, **kwargs):
            start_time = time.time()
            result = func(*args, **kwargs)
            duration = time.time() - start_time

            performance_monitor.record_metric(operation_name, duration)

            # Log slow operations (> 1 second)
            if duration > 1.0:
                from ..utils.logger import logger
                logger.warning(f"Slow operation detected: {operation_name} took {duration:.3f}s")

            return result
        return wrapper
    return decorator


@lru_cache(maxsize=128)
def cache_user_lookup(user_id: int):
    """Cache user lookups to reduce database queries"""
    # This is a placeholder - actual implementation would query the database
    # The LRU cache will store the most recent 128 user lookups
    pass


class QueryOptimizer:
    """Optimize database queries"""

    @staticmethod
    def get_optimized_task_query(db, user_id: int, filters: dict = None):
        """
        Get optimized task query with eager loading
        """
        from ..models.task import Task

        query = db.query(Task).filter(Task.user_id == user_id)

        # Apply filters if provided
        if filters:
            if 'status' in filters:
                query = query.filter(Task.status == filters['status'])

            if 'created_after' in filters:
                query = query.filter(Task.created_at >= filters['created_after'])

            if 'due_before' in filters:
                query = query.filter(Task.due_date <= filters['due_before'])

        # Order by created_at descending for better performance
        query = query.order_by(Task.created_at.desc())

        return query

    @staticmethod
    def batch_load_tasks(db, user_ids: list):
        """
        Batch load tasks for multiple users to reduce queries
        """
        from ..models.task import Task

        tasks = db.query(Task).filter(Task.user_id.in_(user_ids)).all()

        # Group tasks by user_id
        tasks_by_user = {}
        for task in tasks:
            if task.user_id not in tasks_by_user:
                tasks_by_user[task.user_id] = []
            tasks_by_user[task.user_id].append(task)

        return tasks_by_user


class ResponseOptimizer:
    """Optimize API responses"""

    @staticmethod
    def paginate_results(query, page: int = 1, page_size: int = 20):
        """
        Paginate query results for better performance
        """
        offset = (page - 1) * page_size
        total = query.count()
        items = query.offset(offset).limit(page_size).all()

        return {
            'items': items,
            'total': total,
            'page': page,
            'page_size': page_size,
            'total_pages': (total + page_size - 1) // page_size
        }

    @staticmethod
    def compress_response(data: dict):
        """
        Compress response data by removing null values
        """
        return {k: v for k, v in data.items() if v is not None}


# Connection pooling configuration
DATABASE_POOL_CONFIG = {
    'pool_size': 10,  # Number of connections to maintain
    'max_overflow': 20,  # Maximum number of connections to create beyond pool_size
    'pool_timeout': 30,  # Seconds to wait before giving up on getting a connection
    'pool_recycle': 3600,  # Recycle connections after 1 hour
    'pool_pre_ping': True  # Verify connections before using them
}