File size: 5,098 Bytes
a475b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Data models for the Image Search SDK.
"""

from dataclasses import dataclass, field
from typing import List, Optional, Dict, Any, Union
from datetime import datetime


@dataclass
class Product:
    """Product model for indexing and search results."""
    
    id: str
    title: str
    image_url: Optional[str] = None
    image_path: Optional[str] = None
    category: Optional[str] = None
    brand: Optional[str] = None
    price: Optional[float] = None
    rating: Optional[float] = None
    description: Optional[str] = None
    metadata: Dict[str, Any] = field(default_factory=dict)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary for API requests."""
        return {
            'id': self.id,
            'title': self.title,
            'image_url': self.image_url,
            'image_path': self.image_path,
            'category': self.category,
            'brand': self.brand,
            'price': self.price,
            'rating': self.rating,
            'description': self.description,
            'metadata': self.metadata
        }
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Product':
        """Create Product from dictionary."""
        return cls(**data)


@dataclass
class SearchResult:
    """Search result model."""
    
    product: Product
    similarity_score: float
    rank: int
    search_time_ms: int
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            'product': self.product.to_dict(),
            'similarity_score': self.similarity_score,
            'rank': self.rank,
            'search_time_ms': self.search_time_ms
        }


@dataclass
class SearchOptions:
    """Search options and filters."""
    
    top_k: int = 10
    min_price: Optional[float] = None
    max_price: Optional[float] = None
    category: Optional[str] = None
    brand: Optional[str] = None
    title_substring: Optional[str] = None
    min_similarity: float = 0.0
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary for API requests."""
        return {
            'top_k': self.top_k,
            'min_price': self.min_price,
            'max_price': self.max_price,
            'category': self.category,
            'brand': self.brand,
            'title_substring': self.title_substring,
            'min_similarity': self.min_similarity
        }


@dataclass
class IndexingStatus:
    """Indexing operation status."""
    
    total_products: int
    indexed_products: int
    failed_products: int
    is_complete: bool
    start_time: datetime
    end_time: Optional[datetime] = None
    errors: List[str] = field(default_factory=list)
    
    @property
    def progress_percentage(self) -> float:
        """Get indexing progress as percentage."""
        if self.total_products == 0:
            return 0.0
        return (self.indexed_products / self.total_products) * 100
    
    @property
    def success_rate(self) -> float:
        """Get success rate as percentage."""
        if self.total_products == 0:
            return 0.0
        return (self.indexed_products / self.total_products) * 100


@dataclass
class SystemStatus:
    """System health and status information."""
    
    is_healthy: bool
    total_products: int
    indexed_products: int
    last_indexing_time: Optional[datetime]
    search_latency_ms: float
    memory_usage_mb: float
    version: str
    uptime_seconds: int
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            'is_healthy': self.is_healthy,
            'total_products': self.total_products,
            'indexed_products': self.indexed_products,
            'last_indexing_time': self.last_indexing_time.isoformat() if self.last_indexing_time else None,
            'search_latency_ms': self.search_latency_ms,
            'memory_usage_mb': self.memory_usage_mb,
            'version': self.version,
            'uptime_seconds': self.uptime_seconds
        }


@dataclass
class DatabaseConfig:
    """Database connection configuration."""
    
    connection_string: str
    table_name: str = "products"
    id_column: str = "id"
    title_column: str = "title"
    image_url_column: str = "image_url"
    category_column: Optional[str] = None
    brand_column: Optional[str] = None
    price_column: Optional[str] = None
    rating_column: Optional[str] = None
    description_column: Optional[str] = None
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            'connection_string': self.connection_string,
            'table_name': self.table_name,
            'id_column': self.id_column,
            'title_column': self.title_column,
            'image_url_column': self.image_url_column,
            'category_column': self.category_column,
            'brand_column': self.brand_column,
            'price_column': self.price_column,
            'rating_column': self.rating_column,
            'description_column': self.description_column
        }