File size: 11,757 Bytes
d0c8d86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Analytics Module

Tracks usage statistics and performance metrics for the caption generator.
Provides insights into model usage, processing times, and popular styles.
"""

import json
import threading
from pathlib import Path
from typing import Dict, Optional
from datetime import datetime
from dataclasses import dataclass, asdict

from config import analytics_config, style_config


@dataclass
class AnalyticsData:
    """Container for analytics data"""
    total_captions: int = 0
    style_usage: Dict[str, int] = None
    avg_processing_time: float = 0.0
    total_processing_time: float = 0.0
    model_usage: Dict[str, int] = None
    error_count: int = 0
    last_updated: Optional[str] = None
    
    def __post_init__(self):
        if self.style_usage is None:
            self.style_usage = {style: 0 for style in style_config.STYLES.keys()}
        if self.model_usage is None:
            self.model_usage = {"blip": 0, "git": 0}
    
    def to_dict(self) -> dict:
        """Convert to dictionary"""
        return asdict(self)


class AnalyticsManager:
    """
    Thread-safe analytics manager for tracking usage metrics
    
    Features:
    - Real-time metric tracking
    - Persistent storage
    - Thread-safe operations
    - Automatic calculations
    """
    
    def __init__(self, storage_path: Optional[Path] = None):
        """
        Initialize analytics manager
        
        Args:
            storage_path: Path to analytics JSON file
        """
        self.storage_path = storage_path or analytics_config.ANALYTICS_FILE
        self._lock = threading.RLock()
        
        # Load existing data or initialize new
        self.data = self._load_data()
    
    def _load_data(self) -> AnalyticsData:
        """
        Load analytics data from file
        
        Returns:
            AnalyticsData: Loaded or initialized data
        """
        if self.storage_path.exists():
            try:
                with open(self.storage_path, 'r') as f:
                    data_dict = json.load(f)
                    return AnalyticsData(**data_dict)
            except Exception as e:
                print(f"Warning: Failed to load analytics: {e}")
                return AnalyticsData()
        else:
            return AnalyticsData()
    
    def _save_data(self) -> bool:
        """
        Save analytics data to file
        
        Returns:
            bool: True if successful
        """
        try:
            # Ensure directory exists
            self.storage_path.parent.mkdir(parents=True, exist_ok=True)
            
            # Update timestamp
            self.data.last_updated = datetime.now().isoformat()
            
            # Write to file
            with open(self.storage_path, 'w') as f:
                json.dump(self.data.to_dict(), f, indent=4)
            
            return True
        except Exception as e:
            print(f"Error saving analytics: {e}")
            return False
    
    def record_caption_generation(
        self,
        model_name: str,
        style: str,
        processing_time: float,
        success: bool = True
    ) -> None:
        """
        Record a caption generation event
        
        Args:
            model_name: Name of the model used (blip/git)
            style: Style applied
            processing_time: Time taken in seconds
            success: Whether generation was successful
        """
        with self._lock:
            if success:
                # Increment counters
                self.data.total_captions += 1
                
                # Update style usage
                if style in self.data.style_usage:
                    self.data.style_usage[style] += 1
                
                # Update model usage
                model_key = model_name.lower()
                if model_key in self.data.model_usage:
                    self.data.model_usage[model_key] += 1
                
                # Update processing time
                self.data.total_processing_time += processing_time
                self.data.avg_processing_time = (
                    self.data.total_processing_time / self.data.total_captions
                )
            else:
                self.data.error_count += 1
            
            # Save to disk
            self._save_data()
    
    def record_batch_generation(
        self,
        generations: list[dict]
    ) -> None:
        """
        Record multiple caption generations at once
        
        Args:
            generations: List of generation records
                Each record: {model_name, style, processing_time, success}
        """
        with self._lock:
            for gen in generations:
                self.record_caption_generation(
                    model_name=gen.get("model_name", "unknown"),
                    style=gen.get("style", "None"),
                    processing_time=gen.get("processing_time", 0.0),
                    success=gen.get("success", True)
                )
    
    def get_stats(self) -> dict:
        """
        Get current statistics
        
        Returns:
            dict: Current analytics data
        """
        with self._lock:
            return self.data.to_dict()
    
    def get_summary(self) -> dict:
        """
        Get formatted summary of analytics
        
        Returns:
            dict: Human-readable summary
        """
        with self._lock:
            total = self.data.total_captions
            
            # Calculate percentages for styles
            style_percentages = {}
            if total > 0:
                for style, count in self.data.style_usage.items():
                    style_percentages[style] = round((count / total) * 100, 1)
            
            # Calculate percentages for models
            model_percentages = {}
            if total > 0:
                for model, count in self.data.model_usage.items():
                    model_percentages[model] = round((count / total) * 100, 1)
            
            # Find most popular style
            popular_style = max(
                self.data.style_usage.items(),
                key=lambda x: x[1]
            )[0] if self.data.style_usage else "None"
            
            return {
                "total_captions": total,
                "avg_processing_time": round(self.data.avg_processing_time, 2),
                "error_rate": round(
                    (self.data.error_count / (total + self.data.error_count) * 100)
                    if (total + self.data.error_count) > 0 else 0,
                    2
                ),
                "most_popular_style": popular_style,
                "style_distribution": style_percentages,
                "model_distribution": model_percentages,
                "last_updated": self.data.last_updated
            }
    
    def get_display_stats(self) -> str:
        """
        Get formatted stats for UI display
        
        Returns:
            str: Formatted statistics string
        """
        with self._lock:
            summary = self.get_summary()
            
            stats_text = (
                f"📊 Total Captions: {summary['total_captions']} | "
                f"⚡ Avg Time: {summary['avg_processing_time']}s | "
                f"🎨 Popular Style: {summary['most_popular_style']}"
            )
            
            return stats_text
    
    def reset_stats(self) -> bool:
        """
        Reset all statistics
        
        Returns:
            bool: True if successful
        """
        with self._lock:
            self.data = AnalyticsData()
            return self._save_data()
    
    def export_stats(self, export_path: Optional[Path] = None) -> bool:
        """
        Export statistics to a file
        
        Args:
            export_path: Path to export file (default: timestamped file)
            
        Returns:
            bool: True if successful
        """
        with self._lock:
            if export_path is None:
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
                export_path = self.storage_path.parent / f"analytics_export_{timestamp}.json"
            
            try:
                with open(export_path, 'w') as f:
                    export_data = {
                        "exported_at": datetime.now().isoformat(),
                        "statistics": self.data.to_dict(),
                        "summary": self.get_summary()
                    }
                    json.dump(export_data, f, indent=4)
                return True
            except Exception as e:
                print(f"Error exporting analytics: {e}")
                return False


# Singleton instance
_analytics_manager = None
_manager_lock = threading.Lock()


def get_analytics_manager() -> AnalyticsManager:
    """Get singleton AnalyticsManager instance"""
    global _analytics_manager
    if _analytics_manager is None:
        with _manager_lock:
            if _analytics_manager is None:
                _analytics_manager = AnalyticsManager()
    return _analytics_manager


# Convenience functions
def record_generation(
    model_name: str,
    style: str,
    processing_time: float,
    success: bool = True
) -> None:
    """Record a caption generation (convenience function)"""
    get_analytics_manager().record_caption_generation(
        model_name, style, processing_time, success
    )


def get_stats() -> dict:
    """Get current statistics (convenience function)"""
    return get_analytics_manager().get_stats()


def get_summary() -> dict:
    """Get analytics summary (convenience function)"""
    return get_analytics_manager().get_summary()


def get_display_stats() -> str:
    """Get formatted display stats (convenience function)"""
    return get_analytics_manager().get_display_stats()


if __name__ == "__main__":
    # Test the analytics manager
    print("=" * 60)
    print("ANALYTICS MANAGER - TEST MODE")
    print("=" * 60)
    
    # Initialize manager with test path
    test_path = Path("cache/test_analytics.json")
    analytics = AnalyticsManager(storage_path=test_path)
    
    print("\n1. Initial state:")
    print(f"   {analytics.get_display_stats()}")
    
    print("\n2. Recording test generations:")
    analytics.record_caption_generation("blip", "Professional", 2.5, True)
    analytics.record_caption_generation("git", "Creative", 3.2, True)
    analytics.record_caption_generation("blip", "Professional", 2.1, True)
    analytics.record_caption_generation("git", "Social Media", 2.8, True)
    analytics.record_caption_generation("blip", "Technical", 2.3, False)
    print(f"   Recorded 5 generations (4 success, 1 error)")
    
    print("\n3. Current statistics:")
    stats = analytics.get_stats()
    for key, value in stats.items():
        if isinstance(value, dict):
            print(f"   {key}:")
            for k, v in value.items():
                print(f"      {k}: {v}")
        else:
            print(f"   {key}: {value}")
    
    print("\n4. Summary:")
    summary = analytics.get_summary()
    for key, value in summary.items():
        if isinstance(value, dict):
            print(f"   {key}:")
            for k, v in value.items():
                print(f"      {k}: {v}")
        else:
            print(f"   {key}: {value}")
    
    print("\n5. Display format:")
    print(f"   {analytics.get_display_stats()}")
    
    print("\n6. File saved to:")
    print(f"   {test_path}")
    
    print("\n" + "=" * 60)
    print("✓ Analytics manager tests complete")
    print("=" * 60)
    
    # Cleanup test file
    if test_path.exists():
        test_path.unlink()
        print("\n✓ Test file cleaned up")