File size: 7,787 Bytes
9c7d451
 
 
 
 
175746c
9c7d451
 
175746c
9c7d451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""JSONL file storage implementation for Headroom SDK."""

from __future__ import annotations

import json
from collections.abc import Iterator
from datetime import datetime
from pathlib import Path
from typing import Any

from ..config import RequestMetrics
from ..utils import format_timestamp, parse_timestamp
from .base import Storage


class JSONLStorage(Storage):
    """JSONL file-based metrics storage."""

    def __init__(self, file_path: str):
        """
        Initialize JSONL storage.

        Args:
            file_path: Path to JSONL file.
        """
        self.file_path = file_path
        self._ensure_file_exists()

    def _ensure_file_exists(self) -> None:
        """Create file and parent directories if they don't exist."""
        path = Path(self.file_path)
        path.parent.mkdir(parents=True, exist_ok=True)
        if not path.exists():
            path.touch()

    def _metrics_to_dict(self, metrics: RequestMetrics) -> dict[str, Any]:
        """Convert RequestMetrics to serializable dict."""
        return {
            "id": metrics.request_id,
            "timestamp": format_timestamp(metrics.timestamp),
            "model": metrics.model,
            "stream": metrics.stream,
            "mode": metrics.mode,
            "tokens_input_before": metrics.tokens_input_before,
            "tokens_input_after": metrics.tokens_input_after,
            "tokens_output": metrics.tokens_output,
            "block_breakdown": metrics.block_breakdown,
            "waste_signals": metrics.waste_signals,
            "stable_prefix_hash": metrics.stable_prefix_hash,
            "cache_alignment_score": metrics.cache_alignment_score,
            "cached_tokens": metrics.cached_tokens,
            "transforms_applied": metrics.transforms_applied,
            "tool_units_dropped": metrics.tool_units_dropped,
            "turns_dropped": metrics.turns_dropped,
            "messages_hash": metrics.messages_hash,
            "error": metrics.error,
        }

    def _dict_to_metrics(self, data: dict[str, Any]) -> RequestMetrics:
        """Convert dict to RequestMetrics."""
        return RequestMetrics(
            request_id=data["id"],
            timestamp=parse_timestamp(data["timestamp"]),
            model=data["model"],
            stream=data["stream"],
            mode=data["mode"],
            tokens_input_before=data["tokens_input_before"],
            tokens_input_after=data["tokens_input_after"],
            tokens_output=data.get("tokens_output"),
            block_breakdown=data.get("block_breakdown", {}),
            waste_signals=data.get("waste_signals", {}),
            stable_prefix_hash=data.get("stable_prefix_hash", ""),
            cache_alignment_score=data.get("cache_alignment_score", 0.0),
            cached_tokens=data.get("cached_tokens"),
            transforms_applied=data.get("transforms_applied", []),
            tool_units_dropped=data.get("tool_units_dropped", 0),
            turns_dropped=data.get("turns_dropped", 0),
            messages_hash=data.get("messages_hash", ""),
            error=data.get("error"),
        )

    def save(self, metrics: RequestMetrics) -> None:
        """Save request metrics."""
        data = self._metrics_to_dict(metrics)
        with open(self.file_path, "a") as f:
            f.write(json.dumps(data) + "\n")

    def get(self, request_id: str) -> RequestMetrics | None:
        """Get metrics by request ID."""
        for metrics in self.iter_all():
            if metrics.request_id == request_id:
                return metrics
        return None

    def query(
        self,
        start_time: datetime | None = None,
        end_time: datetime | None = None,
        model: str | None = None,
        mode: str | None = None,
        limit: int = 100,
        offset: int = 0,
    ) -> list[RequestMetrics]:
        """Query metrics with filters."""
        results: list[RequestMetrics] = []
        skipped = 0

        for metrics in self.iter_all():
            # Apply filters
            if start_time is not None and metrics.timestamp < start_time:
                continue
            if end_time is not None and metrics.timestamp > end_time:
                continue
            if model is not None and metrics.model != model:
                continue
            if mode is not None and metrics.mode != mode:
                continue

            # Handle offset
            if skipped < offset:
                skipped += 1
                continue

            results.append(metrics)

            # Handle limit
            if len(results) >= limit:
                break

        # Sort by timestamp descending
        results.sort(key=lambda m: m.timestamp, reverse=True)
        return results

    def count(
        self,
        start_time: datetime | None = None,
        end_time: datetime | None = None,
        model: str | None = None,
        mode: str | None = None,
    ) -> int:
        """Count metrics matching filters."""
        count = 0

        for metrics in self.iter_all():
            if start_time is not None and metrics.timestamp < start_time:
                continue
            if end_time is not None and metrics.timestamp > end_time:
                continue
            if model is not None and metrics.model != model:
                continue
            if mode is not None and metrics.mode != mode:
                continue
            count += 1

        return count

    def iter_all(self) -> Iterator[RequestMetrics]:
        """Iterate over all stored metrics."""
        if not Path(self.file_path).exists():
            return

        with open(self.file_path) as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                try:
                    data = json.loads(line)
                    yield self._dict_to_metrics(data)
                except json.JSONDecodeError:
                    # Skip malformed lines
                    continue

    def get_summary_stats(
        self,
        start_time: datetime | None = None,
        end_time: datetime | None = None,
    ) -> dict[str, Any]:
        """Get summary statistics."""
        total_requests = 0
        total_tokens_before = 0
        total_tokens_after = 0
        total_cache_alignment = 0.0
        audit_count = 0
        optimize_count = 0

        for metrics in self.iter_all():
            if start_time is not None and metrics.timestamp < start_time:
                continue
            if end_time is not None and metrics.timestamp > end_time:
                continue

            total_requests += 1
            total_tokens_before += metrics.tokens_input_before
            total_tokens_after += metrics.tokens_input_after
            total_cache_alignment += metrics.cache_alignment_score

            if metrics.mode == "audit":
                audit_count += 1
            elif metrics.mode == "optimize":
                optimize_count += 1

        total_tokens_saved = total_tokens_before - total_tokens_after
        avg_tokens_saved = total_tokens_saved / total_requests if total_requests > 0 else 0
        avg_cache_alignment = total_cache_alignment / total_requests if total_requests > 0 else 0

        return {
            "total_requests": total_requests,
            "total_tokens_before": total_tokens_before,
            "total_tokens_after": total_tokens_after,
            "total_tokens_saved": total_tokens_saved,
            "avg_tokens_saved": avg_tokens_saved,
            "avg_cache_alignment": avg_cache_alignment,
            "audit_count": audit_count,
            "optimize_count": optimize_count,
        }

    def close(self) -> None:
        """No-op for file storage."""
        pass