-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarm_cache.py
More file actions
225 lines (187 loc) · 7.78 KB
/
warm_cache.py
File metadata and controls
225 lines (187 loc) · 7.78 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
#!/usr/bin/env python3
"""
Prumo — Pre-warm the server-side interpret cache for all indicators × lenses × languages.
Calls /api/interpret directly (no browser needed). The backend caches each result
for 30 days. Uses the same 5-year window and quarter quantization as the frontend,
so real users get instant AI responses.
Usage:
python warm_cache.py [--workers N] [--dry-run] [--resume]
Progress: tail -f /tmp/prumo-warm-cache.log
"""
import argparse
import json
import sys
import time
import urllib.request
import urllib.parse
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from pathlib import Path
from threading import Lock
# ── Config ───────────────────────────────────────────────────────
INTERNAL_API = "http://172.20.0.6:8080"
LOG_FILE = "/tmp/prumo-warm-cache.log"
PROGRESS_FILE = "/tmp/prumo-warm-cache-progress.json"
# Match frontend default: 5-year window
_now = datetime.now()
DEFAULT_FROM = f"{_now.year - 5}-{_now.month:02d}"
DEFAULT_TO = f"{_now.year}-{_now.month:02d}"
# Skip 'custom' lens (user-specific ideology text, can't pre-cache)
# kriolu lens removed from site.json for now
LENSES = ["cae", "pcp", "be", "livre", "pan", "ps", "ad", "il", "chega", "neutro"]
LANGUAGES = ["pt", "cv", "fr", "es", "en"]
_log_lock = Lock()
def log(msg):
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
line = f"[{ts}] {msg}"
with _log_lock:
with open(LOG_FILE, "a") as f:
f.write(line + "\n")
print(line, flush=True)
def fetch_indicators():
"""Get all indicators from the catalog API."""
resp = urllib.request.urlopen(f"{INTERNAL_API}/api/catalog", timeout=30)
catalog = json.loads(resp.read())
indicators = []
for src, src_info in catalog.items():
for ind_id, ind_info in src_info.get("indicators", {}).items():
indicators.append({
"source": src,
"indicator": ind_id,
"label": ind_info.get("label", ind_id),
})
return indicators
def fetch_series(source, indicator):
"""Fetch series data for a single indicator."""
url = (f"{INTERNAL_API}/api/series"
f"?source={urllib.parse.quote(source)}"
f"&indicator={urllib.parse.quote(indicator)}"
f"&from={DEFAULT_FROM}&to={DEFAULT_TO}")
try:
resp = urllib.request.urlopen(url, timeout=30)
data = json.loads(resp.read())
return data if data else None
except Exception:
return None
def warm_one(series_data, lens, lang, label=""):
"""Call /api/interpret via HTTP. Runs through uvicorn (needed for OAuth proxy)."""
payload = json.dumps({
"series": series_data,
"from": DEFAULT_FROM,
"to": DEFAULT_TO,
"lang": "pt",
"context": "economia portuguesa",
"lens": lens,
"custom_ideology": None,
"output_language": lang,
}).encode()
req = urllib.request.Request(
f"{INTERNAL_API}/api/interpret",
data=payload,
headers={"Content-Type": "application/json"},
method="POST",
)
try:
resp = urllib.request.urlopen(req, timeout=180)
result = json.loads(resp.read())
has_text = bool(result.get("text"))
return has_text
except Exception as e:
return f"ERROR: {e}"
def main():
parser = argparse.ArgumentParser(description="Pre-warm Prumo interpret cache")
parser.add_argument("--workers", type=int, default=1,
help="Parallel workers (default 1 — Prumo runs 1 uvicorn worker)")
parser.add_argument("--dry-run", action="store_true",
help="Just count combinations, don't call API")
parser.add_argument("--resume", action="store_true",
help="Skip already-completed combos from progress file")
parser.add_argument("--lenses", nargs="*", default=None,
help="Only warm these lenses (default: all)")
parser.add_argument("--languages", nargs="*", default=None,
help="Only warm these languages (default: all)")
args = parser.parse_args()
lenses = args.lenses or LENSES
languages = args.languages or LANGUAGES
log("=" * 60)
log("PRUMO CACHE WARM — STARTING")
log(f"Period: {DEFAULT_FROM} → {DEFAULT_TO}")
log(f"Lenses: {lenses}")
log(f"Languages: {languages}")
log(f"Workers: {args.workers}")
log("=" * 60)
# Fetch indicators
indicators = fetch_indicators()
log(f"Loaded {len(indicators)} indicators from catalog")
total = len(indicators) * len(lenses) * len(languages)
log(f"Total combinations: {len(indicators)} indicators × {len(lenses)} lenses × {len(languages)} langs = {total}")
if args.dry_run:
log("DRY RUN — exiting")
return
# Load resume state
done_keys = set()
if args.resume and Path(PROGRESS_FILE).exists():
done_keys = set(json.loads(Path(PROGRESS_FILE).read_text()))
log(f"Resuming: {len(done_keys)} already completed")
# Pre-fetch all series data (fast, no LLM cost)
log("Fetching series data for all indicators...")
series_cache = {}
for i, ind in enumerate(indicators):
key = f"{ind['source']}/{ind['indicator']}"
data = fetch_series(ind["source"], ind["indicator"])
if data:
series_cache[key] = data
if (i + 1) % 50 == 0:
log(f" Fetched {i+1}/{len(indicators)} series")
log(f"Series data ready: {len(series_cache)}/{len(indicators)} have data")
# Build work queue
work = []
for ind in indicators:
ind_key = f"{ind['source']}/{ind['indicator']}"
if ind_key not in series_cache:
continue
for lens in lenses:
for lang in languages:
combo_key = f"{ind_key}|{lens}|{lang}"
if combo_key not in done_keys:
work.append((ind_key, ind["label"], lens, lang, combo_key))
log(f"Work queue: {len(work)} combos to warm (skipped {total - len(work)})")
ok = fail = skip = 0
start_time = time.time()
def process(item):
ind_key, label, lens, lang, combo_key = item
result = warm_one(series_cache[ind_key], lens, lang, label)
return combo_key, ind_key, lens, lang, result
with ThreadPoolExecutor(max_workers=args.workers) as pool:
futures = {pool.submit(process, item): item for item in work}
for i, future in enumerate(as_completed(futures)):
combo_key, ind_key, lens, lang, result = future.result()
done_keys.add(combo_key)
if result is True:
ok += 1
status = "OK"
elif result is False:
skip += 1
status = "EMPTY"
else:
fail += 1
status = str(result)
completed = ok + fail + skip
if completed % 25 == 0 or status != "OK":
elapsed = time.time() - start_time
rate = completed / elapsed if elapsed > 0 else 0
eta = (len(work) - completed) / rate if rate > 0 else 0
log(f"[{completed}/{len(work)}] {status}: {ind_key} lens={lens} lang={lang}"
f" | {rate:.1f}/s ETA {eta/60:.0f}min | OK={ok} FAIL={fail} EMPTY={skip}")
# Save progress every 50
if completed % 50 == 0:
Path(PROGRESS_FILE).write_text(json.dumps(list(done_keys)))
# Final save
Path(PROGRESS_FILE).write_text(json.dumps(list(done_keys)))
elapsed = time.time() - start_time
log("=" * 60)
log(f"CACHE WARM COMPLETE in {elapsed/60:.1f} min")
log(f"OK={ok} EMPTY={skip} FAIL={fail} Total={ok+fail+skip}")
log("=" * 60)
if __name__ == "__main__":
main()