Spaces:
Runtime error

File size: 4,151 Bytes
856c308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
性能监控工具
记录远程云服务的调用时间
"""
import time
import logging
from functools import wraps
from contextlib import contextmanager
from typing import Callable, Optional

logger = logging.getLogger(__name__)


class PerformanceMonitor:
    """性能监控器"""
    
    @staticmethod
    def log_operation(service_name: str, operation: str, duration: float, 
                     success: bool = True, details: Optional[str] = None):
        """
        记录操作性能
        
        Args:
            service_name: 服务名称(如 "Supabase Storage", "Pinecone", "PostgreSQL")
            operation: 操作名称(如 "upload_file", "query", "execute_query")
            duration: 耗时(秒)
            success: 是否成功
            details: 额外详情
        """
        # 性能监控日志已注释
        # status = "✅" if success else "❌"
        # duration_ms = duration * 1000
        # 
        # log_msg = f"[性能监控] {status} {service_name} | {operation} | {duration_ms:.2f}ms"
        # if details:
        #     log_msg += f" | {details}"
        # 
        # # 根据耗时选择日志级别
        # if duration > 5.0:
        #     logger.warning(log_msg)
        # elif duration > 2.0:
        #     logger.info(log_msg)
        # else:
        #     logger.debug(log_msg)
        pass
    
    @staticmethod
    def monitor_function(service_name: str, operation_name: Optional[str] = None):
        """
        函数性能监控装饰器
        
        用法:
            @PerformanceMonitor.monitor_function("Supabase Storage", "upload_file")
            def upload_file(...):
                ...
        """
        def decorator(func: Callable):
            op_name = operation_name or func.__name__
            
            @wraps(func)
            def wrapper(*args, **kwargs):
                start_time = time.time()
                success = True
                error_msg = None
                
                try:
                    result = func(*args, **kwargs)
                    return result
                except Exception as e:
                    success = False
                    error_msg = str(e)
                    raise
                finally:
                    duration = time.time() - start_time
                    details = f"error: {error_msg}" if error_msg else None
                    PerformanceMonitor.log_operation(
                        service_name, op_name, duration, success, details
                    )
            
            return wrapper
        return decorator
    
    @staticmethod
    @contextmanager
    def monitor_operation(service_name: str, operation: str, details: Optional[str] = None):
        """
        操作性能监控上下文管理器
        
        用法:
            with PerformanceMonitor.monitor_operation("Pinecone", "query", "top_k=5"):
                # 执行操作
                ...
        """
        start_time = time.time()
        success = True
        error_msg = None
        
        try:
            yield
        except Exception as e:
            success = False
            error_msg = str(e)
            raise
        finally:
            duration = time.time() - start_time
            full_details = details
            if error_msg:
                full_details = f"{details} | error: {error_msg}" if details else f"error: {error_msg}"
            PerformanceMonitor.log_operation(
                service_name, operation, duration, success, full_details
            )


# 便捷函数
def monitor_storage(operation: str, details: Optional[str] = None):
    """监控存储服务操作"""
    return PerformanceMonitor.monitor_operation("Supabase Storage", operation, details)


def monitor_vector_db(operation: str, details: Optional[str] = None):
    """监控向量数据库操作"""
    return PerformanceMonitor.monitor_operation("Pinecone", operation, details)


def monitor_database(operation: str, details: Optional[str] = None):
    """监控数据库操作"""
    return PerformanceMonitor.monitor_operation("PostgreSQL", operation, details)