File size: 2,566 Bytes
3fe0726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import os
import logging
from datetime import datetime

logger = logging.getLogger(__name__)

class PerformanceLogger:
    def __init__(self, filename="news_processing_performance.csv"):
        self.filename = filename
        self._initialize_csv()

    def _initialize_csv(self):
        """Initialize the CSV file with headers if it doesn't exist."""
        if not os.path.exists(self.filename):
            try:
                with open(self.filename, mode='w', newline='', encoding='utf-8') as file:
                    writer = csv.writer(file)
                    headers = [
                        "timestamp",
                        "ticker",
                        "headline",
                        "handler1_duration_sec",
                        "handler2_duration_sec",
                        "handler3_duration_sec",
                        "handler4_duration_sec",
                        "handler5_duration_sec",
                        "marketaux_processing_times_sec"
                    ]
                    writer.writerow(headers)
                logger.info(f"[PERFORMANCE] Initialized log file: {self.filename}")
            except Exception as e:
                logger.error(f"[PERFORMANCE] Failed to initialize log file: {e}")

    def log_metrics(self, metrics: dict):
        """
        Log a dictionary of metrics to the CSV file.
        
        Args:
            metrics (dict): Dictionary containing metric values.
        """
        try:
            with open(self.filename, mode='a', newline='', encoding='utf-8') as file:
                writer = csv.writer(file)
                
                # Extract values with defaults
                row = [
                    datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                    metrics.get('ticker', 'N/A'),
                    metrics.get('headline', 'N/A'),
                    f"{metrics.get('handler1_duration', 0):.4f}",
                    f"{metrics.get('handler2_duration', 0):.4f}",
                    f"{metrics.get('handler3_duration', 0):.4f}",
                    f"{metrics.get('handler4_duration', 0):.4f}",
                    f"{metrics.get('handler5_duration', 0):.4f}",
                    str(metrics.get('marketaux_processing_times', []))
                ]
                
                writer.writerow(row)
                logger.info(f"[PERFORMANCE] Logged metrics for {metrics.get('ticker', 'N/A')}")
                
        except Exception as e:
            logger.error(f"[PERFORMANCE] Failed to log metrics: {e}")