Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
383 lines (318 loc) · 11.7 KB
/
Copy pathmodels.py
File metadata and controls
383 lines (318 loc) · 11.7 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from __future__ import annotations
import json
from datetime import datetime, timezone
from typing import Any
from uuid import uuid4
from backend.app.db.session import get_connection, init_db
from core.utils.yield_metrics import apply_yield_fields
def _utc_now() -> str:
return datetime.now(timezone.utc).isoformat()
def upsert_product(
normalized: dict[str, Any],
raw_text: str | None,
source_kind: str,
source_file_path: str | None,
source_file_hash_sha256: str | None,
) -> str:
init_db()
product_id = normalized.get("id") or str(uuid4())
normalized["id"] = product_id
apply_yield_fields(normalized)
isin = normalized.get("isin", {}).get("value") if isinstance(normalized.get("isin"), dict) else None
valor = normalized.get("valor_number", {}).get("value") if isinstance(normalized.get("valor_number"), dict) else None
issuer = normalized.get("issuer_name", {}).get("value") if isinstance(normalized.get("issuer_name"), dict) else None
product_type = normalized.get("product_type", {}).get("value") if isinstance(normalized.get("product_type"), dict) else None
currency = normalized.get("currency", {}).get("value") if isinstance(normalized.get("currency"), dict) else None
maturity = normalized.get("maturity_date", {}).get("value") if isinstance(normalized.get("maturity_date"), dict) else None
now = _utc_now()
with get_connection() as conn:
conn.execute(
"""
INSERT INTO products (
id, isin, valor_number, issuer_name, product_type, currency, maturity_date,
review_status, source_kind, normalized_json, raw_text, source_file_path,
source_file_hash_sha256, created_at, updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(source_file_hash_sha256) DO UPDATE SET
isin = excluded.isin,
valor_number = excluded.valor_number,
issuer_name = excluded.issuer_name,
product_type = excluded.product_type,
currency = excluded.currency,
maturity_date = excluded.maturity_date,
source_kind = excluded.source_kind,
normalized_json = excluded.normalized_json,
raw_text = excluded.raw_text,
source_file_path = excluded.source_file_path,
updated_at = excluded.updated_at
""",
(
product_id,
isin,
valor,
issuer,
product_type,
currency,
maturity,
"not_reviewed",
source_kind,
json.dumps(normalized),
raw_text,
source_file_path,
source_file_hash_sha256,
now,
now,
),
)
conn.commit()
return product_id
def list_products(
limit: int = 200,
offset: int = 0,
source_kind: str | None = None,
product_type: str | None = None
) -> list[dict[str, Any]]:
"""
List products with optional filtering.
Args:
limit: Maximum number of products to return
offset: Number of products to skip
source_kind: Filter by data source (e.g., 'akb_finanzportal', 'leonteq_api')
product_type: Filter by product type (partial match, case-insensitive)
Returns:
List of product records
"""
init_db()
query = "SELECT * FROM products WHERE 1=1"
params: list[Any] = []
if source_kind:
query += " AND source_kind = ?"
params.append(source_kind)
if product_type:
query += " AND product_type LIKE ?"
params.append(f"%{product_type}%")
query += " ORDER BY updated_at DESC LIMIT ? OFFSET ?"
params.extend([limit, offset])
with get_connection() as conn:
rows = conn.execute(query, params).fetchall()
return [dict(row) for row in rows]
def count_products(
source_kind: str | None = None,
product_type: str | None = None
) -> int:
"""
Count products with optional filtering.
Args:
source_kind: Filter by data source
product_type: Filter by product type (partial match, case-insensitive)
Returns:
Total count of matching products
"""
init_db()
query = "SELECT COUNT(*) AS count FROM products WHERE 1=1"
params: list[Any] = []
if source_kind:
query += " AND source_kind = ?"
params.append(source_kind)
if product_type:
query += " AND product_type LIKE ?"
params.append(f"%{product_type}%")
with get_connection() as conn:
row = conn.execute(query, params).fetchone()
return int(row["count"])
def clear_products() -> None:
init_db()
with get_connection() as conn:
conn.execute("DELETE FROM products")
conn.commit()
def get_product(product_id: str) -> dict[str, Any] | None:
init_db()
with get_connection() as conn:
row = conn.execute("SELECT * FROM products WHERE id = ?", (product_id,)).fetchone()
return dict(row) if row else None
def update_review_status(product_id: str, status: str) -> None:
init_db()
with get_connection() as conn:
conn.execute(
"UPDATE products SET review_status = ?, updated_at = ? WHERE id = ?",
(status, _utc_now(), product_id),
)
conn.commit()
def update_source_file_path(product_id: str, source_file_path: str | None) -> None:
init_db()
with get_connection() as conn:
conn.execute(
"UPDATE products SET source_file_path = ?, updated_at = ? WHERE id = ?",
(source_file_path, _utc_now(), product_id),
)
conn.commit()
def create_crawl_run(name: str) -> str:
init_db()
run_id = str(uuid4())
now = _utc_now()
with get_connection() as conn:
conn.execute(
"""
INSERT INTO crawl_runs (id, name, status, total, completed, errors_count, started_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(run_id, name, "running", 0, 0, 0, now, now),
)
conn.commit()
return run_id
def get_crawl_run(run_id: str) -> dict[str, Any] | None:
init_db()
with get_connection() as conn:
row = conn.execute("SELECT * FROM crawl_runs WHERE id = ?", (run_id,)).fetchone()
return dict(row) if row else None
def update_crawl_run(
run_id: str,
*,
status: str | None = None,
total: int | None = None,
completed: int | None = None,
errors_count: int | None = None,
last_error: str | None = None,
checkpoint_offset: int | None = None,
) -> None:
init_db()
updates = []
params: list[Any] = []
if status is not None:
updates.append("status = ?")
params.append(status)
if total is not None:
updates.append("total = ?")
params.append(total)
if completed is not None:
updates.append("completed = ?")
params.append(completed)
if errors_count is not None:
updates.append("errors_count = ?")
params.append(errors_count)
if last_error is not None:
updates.append("last_error = ?")
params.append(last_error)
if checkpoint_offset is not None:
updates.append("checkpoint_offset = ?")
params.append(checkpoint_offset)
updates.append("updated_at = ?")
params.append(_utc_now())
if status in {"completed", "failed"}:
updates.append("ended_at = ?")
params.append(_utc_now())
params.append(run_id)
with get_connection() as conn:
conn.execute(f"UPDATE crawl_runs SET {', '.join(updates)} WHERE id = ?", params)
conn.commit()
def increment_crawl_completed(run_id: str, delta: int = 1) -> None:
init_db()
with get_connection() as conn:
conn.execute(
"UPDATE crawl_runs SET completed = completed + ?, updated_at = ? WHERE id = ?",
(delta, _utc_now(), run_id),
)
conn.commit()
def increment_crawl_errors(run_id: str, error_message: str) -> None:
init_db()
with get_connection() as conn:
conn.execute(
"""
UPDATE crawl_runs
SET errors_count = errors_count + 1, last_error = ?, updated_at = ?
WHERE id = ?
""",
(error_message, _utc_now(), run_id),
)
conn.commit()
def pause_crawl_run(run_id: str) -> None:
"""Pause a running crawl."""
init_db()
with get_connection() as conn:
conn.execute(
"UPDATE crawl_runs SET status = ?, updated_at = ? WHERE id = ? AND status = ?",
("paused", _utc_now(), run_id, "running"),
)
conn.commit()
def resume_crawl_run(run_id: str) -> None:
"""Resume a paused crawl."""
init_db()
with get_connection() as conn:
conn.execute(
"UPDATE crawl_runs SET status = ?, updated_at = ? WHERE id = ? AND status = ?",
("running", _utc_now(), run_id, "paused"),
)
conn.commit()
def cancel_crawl_run(run_id: str) -> None:
"""Cancel a running or paused crawl."""
init_db()
now = _utc_now()
with get_connection() as conn:
conn.execute(
"UPDATE crawl_runs SET status = ?, updated_at = ?, ended_at = ? WHERE id = ? AND status IN (?, ?)",
("cancelled", now, now, run_id, "running", "paused"),
)
conn.commit()
def is_crawl_paused(run_id: str) -> bool:
"""Check if a crawl is paused."""
init_db()
with get_connection() as conn:
row = conn.execute(
"SELECT status FROM crawl_runs WHERE id = ?",
(run_id,),
).fetchone()
return row["status"] == "paused" if row else False
def is_crawl_cancelled(run_id: str) -> bool:
"""Check if a crawl has been cancelled."""
init_db()
with get_connection() as conn:
row = conn.execute(
"SELECT status FROM crawl_runs WHERE id = ?",
(run_id,),
).fetchone()
return row["status"] == "cancelled" if row else False
def get_products_for_pdf_enrichment(source_kind: str = "leonteq_api", limit: int = 100) -> list[dict[str, Any]]:
"""
Get products that need PDF enrichment (missing coupon data).
Args:
source_kind: Data source to filter by
limit: Maximum number of products to return
Returns:
List of product records with id, raw_text, and normalized_json
"""
init_db()
with get_connection() as conn:
rows = conn.execute(
"""
SELECT id, raw_text, normalized_json
FROM products
WHERE source_kind = ?
AND (
json_extract(normalized_json, '$.coupon_rate_pct_pa.value') IS NULL
OR json_extract(normalized_json, '$.barrier_level_pct.value') IS NULL
)
ORDER BY updated_at DESC
LIMIT ?
""",
(source_kind, limit),
).fetchall()
return [dict(row) for row in rows]
def update_product_normalized_json(product_id: int, normalized_json: str) -> None:
"""Update the normalized_json field for a product."""
init_db()
with get_connection() as conn:
conn.execute(
"UPDATE products SET normalized_json = ?, updated_at = ? WHERE id = ?",
(normalized_json, _utc_now(), product_id),
)
conn.commit()
def update_product_raw_text(product_id: int, raw_text: str) -> None:
"""Update the raw_text field for a product."""
init_db()
with get_connection() as conn:
conn.execute(
"UPDATE products SET raw_text = ?, updated_at = ? WHERE id = ?",
(raw_text, _utc_now(), product_id),
)
conn.commit()