File size: 7,583 Bytes
0125724
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
TradeSense AI β€” In-Memory Market Data Cache
TTL-enforced caching with rich audit metadata. API credentials are NEVER stored.
"""

import time
from datetime import date, datetime, timezone
from typing import Dict, Optional, Tuple, Any
import pandas as pd

from backend.config.settings import settings
from backend.app.schemas.contracts import DatasetMetadata


class CacheEntryMetadata:
    """
    Audit metadata stored alongside each cached dataset.
    SECURITY: Must never contain API keys, auth headers, or secrets.
    """
    __slots__ = (
        "ticker", "start_date", "end_date", "cutoff_session",
        "dataset_version", "quality_status", "provider",
        "lineage_hash", "created_at", "expires_at",
    )

    def __init__(
        self,
        ticker: str,
        start_date: date,
        end_date: date,
        cutoff_session: date,
        dataset_version: str,
        quality_status: str,
        provider: str,
        lineage_hash: str,
        ttl_seconds: int,
    ):
        self.ticker = ticker
        self.start_date = start_date
        self.end_date = end_date
        self.cutoff_session = cutoff_session
        self.dataset_version = dataset_version
        self.quality_status = quality_status
        self.provider = provider
        self.lineage_hash = lineage_hash
        self.created_at: datetime = datetime.now(timezone.utc)
        self.expires_at: float = time.time() + ttl_seconds

    def is_expired(self) -> bool:
        return time.time() > self.expires_at

    def safe_dict(self) -> Dict[str, Any]:
        """Returns a safe, credential-free audit snapshot of this entry."""
        return {
            "ticker": self.ticker,
            "start_date": str(self.start_date),
            "end_date": str(self.end_date),
            "cutoff_session": str(self.cutoff_session),
            "dataset_version": self.dataset_version,
            "quality_status": self.quality_status,
            "provider": self.provider,
            "lineage_hash": self.lineage_hash,
            "created_at": self.created_at.isoformat(),
            "expires_at": datetime.fromtimestamp(self.expires_at, tz=timezone.utc).isoformat(),
        }


class CacheEntry:
    """Stores a frozen DataFrame + DatasetMetadata pair with TTL enforcement."""

    def __init__(
        self,
        df: pd.DataFrame,
        metadata: DatasetMetadata,
        ttl_seconds: int,
    ):
        self.df = df.copy()
        self.metadata = metadata
        self.cache_meta = CacheEntryMetadata(
            ticker=metadata.ticker,
            start_date=metadata.start_session,
            end_date=metadata.cutoff_session,
            cutoff_session=metadata.cutoff_session,
            dataset_version=metadata.dataset_version,
            quality_status=(
                metadata.quality_report.quality_status.value
                if metadata.quality_report else "UNKNOWN"
            ),
            provider=metadata.source_primary,
            lineage_hash=metadata.sha256_hash,
            ttl_seconds=ttl_seconds,
        )

    def is_expired(self) -> bool:
        return self.cache_meta.is_expired()


class MarketDataCache:
    """
    Thread-safe in-memory cache for canonical OHLCV datasets.
    Enforces:
    - TTL expiry
    - Cutoff T validation on retrieval (cached data cannot bypass temporal isolation)
    - No credential storage
    """

    def __init__(self):
        self._cache: Dict[str, CacheEntry] = {}

    # ─────────────────────────────────────────────────────────────────────
    # Internal helpers
    # ─────────────────────────────────────────────────────────────────────

    @staticmethod
    def _build_key(ticker: str, start_date: Any, end_date: Any, cutoff_session: Any) -> str:
        return f"{ticker.upper()}|{start_date}|{end_date}|{cutoff_session}"

    # ─────────────────────────────────────────────────────────────────────
    # Public API
    # ─────────────────────────────────────────────────────────────────────

    def get(
        self,
        ticker: str,
        start_date: Any,
        end_date: Any,
        cutoff_session: Any,
    ) -> Optional[Tuple[pd.DataFrame, DatasetMetadata]]:
        """
        Returns a cached (DataFrame, DatasetMetadata) pair if:
        - The cache is enabled
        - An unexpired entry exists for this key
        - The cached dataset's cutoff_session matches the requested cutoff (temporal safety)
        """
        if not settings.market_data.cache_enabled:
            return None

        key = self._build_key(ticker, start_date, end_date, cutoff_session)
        entry = self._cache.get(key)
        if entry is None:
            return None

        if entry.is_expired():
            del self._cache[key]
            return None

        # CRITICAL: Validate that the cached cutoff matches the requested cutoff.
        # This prevents stale cache entries from violating temporal isolation.
        if str(entry.cache_meta.cutoff_session) != str(cutoff_session):
            return None

        return entry.df.copy(), entry.metadata

    def set(
        self,
        ticker: str,
        start_date: Any,
        end_date: Any,
        cutoff_session: Any,
        df: pd.DataFrame,
        metadata: DatasetMetadata,
        is_historical: bool = True,
    ) -> None:
        """Stores a canonical dataset under the given key with appropriate TTL."""
        if not settings.market_data.cache_enabled:
            return

        key = self._build_key(ticker, start_date, end_date, cutoff_session)
        ttl = (
            settings.market_data.historical_cache_ttl_seconds
            if is_historical
            else settings.market_data.latest_data_cache_ttl_seconds
        )
        self._cache[key] = CacheEntry(df, metadata, ttl)

    def get_entry_metadata(
        self,
        ticker: str,
        start_date: Any,
        end_date: Any,
        cutoff_session: Any,
    ) -> Optional[Dict[str, Any]]:
        """Returns safe, credential-free metadata for a cached entry."""
        key = self._build_key(ticker, start_date, end_date, cutoff_session)
        entry = self._cache.get(key)
        if entry is None or entry.is_expired():
            return None
        return entry.cache_meta.safe_dict()

    def invalidate(self, ticker: str) -> int:
        """Evicts all entries for a ticker. Returns count evicted."""
        keys_to_drop = [k for k in self._cache if k.startswith(f"{ticker.upper()}|")]
        for k in keys_to_drop:
            del self._cache[k]
        return len(keys_to_drop)

    def clear(self) -> None:
        self._cache.clear()

    def stats(self) -> Dict[str, Any]:
        """Returns aggregate cache statistics (no credentials)."""
        live = sum(1 for e in self._cache.values() if not e.is_expired())
        return {
            "total_entries": len(self._cache),
            "live_entries": live,
            "expired_entries": len(self._cache) - live,
        }


# Global Singleton Market Data Cache
data_cache = MarketDataCache()