forked from aqua5230/usage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistory_loader.py
More file actions
227 lines (185 loc) · 6.6 KB
/
history_loader.py
File metadata and controls
227 lines (185 loc) · 6.6 KB
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
from __future__ import annotations
import json
import logging
import math
import os
from collections import OrderedDict
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any
from project_resolver import project_from_encoded_path, resolve_project_name
logger = logging.getLogger(__name__)
_FILE_CACHE_MAXSIZE = 512
_file_cache: OrderedDict[Path, tuple[float, int, list[UsageEntry]]] = OrderedDict()
CLAUDE_PROJECTS_DIR = Path(os.path.expanduser("~/.claude/projects"))
@dataclass(slots=True)
class UsageEntry:
timestamp: datetime
session_id: str
message_id: str
request_id: str
model: str
input_tokens: int
output_tokens: int
cache_creation_tokens: int
cache_read_tokens: int
cost_usd: float | None
project: str
@property
def total_tokens(self) -> int:
return (
self.input_tokens
+ self.output_tokens
+ self.cache_creation_tokens
+ self.cache_read_tokens
)
def load_entries(hours_back: int = 0) -> list[UsageEntry]:
entries: list[UsageEntry] = []
seen: set[str] = set()
cutoff = datetime.now(UTC) - timedelta(hours=hours_back) if hours_back > 0 else None
if not CLAUDE_PROJECTS_DIR.is_dir():
return []
cutoff_ts = cutoff.timestamp() if cutoff else None
for jsonl_path in CLAUDE_PROJECTS_DIR.rglob("*.jsonl"):
if cutoff_ts is not None:
try:
if jsonl_path.stat().st_mtime < cutoff_ts:
continue
except OSError as exc:
logger.warning("failed to stat Claude project log %s: %s", jsonl_path, exc)
continue
project = _project_from_path(jsonl_path)
_load_file(jsonl_path, project, cutoff, seen, entries)
entries.sort(key=lambda entry: entry.timestamp)
return entries
def _load_file(
path: Path,
project: str,
cutoff: datetime | None,
seen: set[str],
entries: list[UsageEntry],
) -> None:
try:
st = path.stat()
except OSError as exc:
logger.warning("failed to stat Claude project log %s: %s", path, exc)
return
cached = _file_cache.get(path)
if cached is not None and cached[0] == st.st_mtime and cached[1] == st.st_size:
_file_cache.move_to_end(path)
for entry in cached[2]:
if cutoff is not None and entry.timestamp < cutoff:
continue
dedup_key = _dedup_key(entry)
if dedup_key in seen:
continue
seen.add(dedup_key)
entries.append(entry)
return
parsed: list[UsageEntry] = []
try:
with path.open(encoding="utf-8", errors="replace") as file:
for line in file:
parsed_entry = _parse_line(line, project)
if parsed_entry is not None:
parsed.append(parsed_entry)
except OSError as exc:
logger.warning("failed to read Claude project log %s: %s", path, exc)
return
if path not in _file_cache and len(_file_cache) >= _FILE_CACHE_MAXSIZE:
_file_cache.popitem(last=False)
_file_cache[path] = (st.st_mtime, st.st_size, parsed)
for entry in parsed:
if cutoff is not None and entry.timestamp < cutoff:
continue
dedup_key = _dedup_key(entry)
if dedup_key in seen:
continue
seen.add(dedup_key)
entries.append(entry)
def _parse_line(line: str, project: str) -> UsageEntry | None:
try:
data = json.loads(line)
except json.JSONDecodeError:
return None
if not isinstance(data, dict) or data.get("type") != "assistant":
return None
message = data.get("message")
if not isinstance(message, dict):
return None
usage = message.get("usage")
if not isinstance(usage, dict):
return None
timestamp = _parse_timestamp(data.get("timestamp"))
if timestamp is None:
return None
input_tokens = _as_int(usage.get("input_tokens"))
output_tokens = _as_int(usage.get("output_tokens"))
cache_creation_tokens = _as_int(usage.get("cache_creation_input_tokens"))
cache_read_tokens = _as_int(usage.get("cache_read_input_tokens"))
if input_tokens + output_tokens + cache_creation_tokens + cache_read_tokens == 0:
return None
cwd = data.get("cwd")
if isinstance(cwd, str) and cwd:
project = _project_from_cwd(cwd)
return UsageEntry(
timestamp=timestamp,
session_id=_as_str(data.get("sessionId")),
message_id=_as_str(message.get("id")),
request_id=_as_str(data.get("requestId")),
model=_as_str(message.get("model")) or "unknown",
input_tokens=input_tokens,
output_tokens=output_tokens,
cache_creation_tokens=cache_creation_tokens,
cache_read_tokens=cache_read_tokens,
cost_usd=_as_optional_float(data.get("costUSD")),
project=project,
)
def _parse_timestamp(value: Any) -> datetime | None:
if not isinstance(value, str) or not value:
return None
try:
timestamp = datetime.fromisoformat(value.replace("Z", "+00:00"))
except ValueError:
return None
if timestamp.tzinfo is None:
return timestamp.replace(tzinfo=UTC)
return timestamp.astimezone(UTC)
def _project_from_path(jsonl_path: Path) -> str:
return project_from_encoded_path(jsonl_path, CLAUDE_PROJECTS_DIR)
def _project_from_cwd(cwd: str) -> str:
return resolve_project_name(cwd)
def _dedup_key(entry: UsageEntry) -> str:
if entry.message_id or entry.request_id:
return f"message:{entry.message_id}:{entry.request_id}"
return (
f"entry:{entry.session_id}:{entry.timestamp.isoformat()}:{entry.model}:"
f"{entry.input_tokens}:{entry.output_tokens}:"
f"{entry.cache_creation_tokens}:{entry.cache_read_tokens}"
)
def _as_int(value: Any) -> int:
if isinstance(value, bool):
return 0
if isinstance(value, int):
return max(0, int(value))
if isinstance(value, str):
normalized = value.strip()
if normalized.isascii() and (
normalized.isdigit()
or (normalized.startswith("+") and normalized[1:].isdigit())
):
return int(normalized)
return 0
def _as_str(value: Any) -> str:
return value if isinstance(value, str) else ""
def _as_optional_float(value: Any) -> float | None:
if isinstance(value, bool):
return None
try:
number = float(value)
except (TypeError, ValueError):
return None
if not math.isfinite(number):
return None
return number