feat: complete heaven readings and screener publication
This commit is contained in:
@@ -686,6 +686,102 @@ class ScreenerRepositoryMixin:
|
||||
result.append(payload)
|
||||
return result
|
||||
|
||||
def screener_runs_for_dates(
|
||||
self, user_id: int, trade_dates: list[str], limit: int = 1200,
|
||||
) -> list[dict[str, Any]]:
|
||||
normalized_dates = list(dict.fromkeys(str(item) for item in trade_dates if item))
|
||||
if not normalized_dates:
|
||||
return []
|
||||
safe_limit = max(1, min(2400, int(limit)))
|
||||
owner_clause = "user_id IS NULL" if int(user_id) == 0 else "user_id = ?"
|
||||
parameters: list[Any] = [] if int(user_id) == 0 else [int(user_id)]
|
||||
placeholders = ",".join("?" for _ in normalized_dates)
|
||||
parameters.extend(normalized_dates)
|
||||
parameters.append(safe_limit)
|
||||
with self.connect() as connection:
|
||||
rows = connection.execute(
|
||||
f"""
|
||||
WITH ranked AS (
|
||||
SELECT id, trade_date, regime, mode, strategy_name, result, created_at,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY trade_date, mode, regime, strategy_name
|
||||
ORDER BY id DESC
|
||||
) AS context_rank
|
||||
FROM screener_runs
|
||||
WHERE {owner_clause} AND trade_date IN ({placeholders})
|
||||
)
|
||||
SELECT id, trade_date, regime, mode, strategy_name, result, created_at
|
||||
FROM ranked
|
||||
WHERE context_rank = 1
|
||||
ORDER BY trade_date DESC, id DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
parameters,
|
||||
).fetchall()
|
||||
return [
|
||||
payload
|
||||
for row in rows
|
||||
if (payload := self._screener_run_payload(row)) is not None
|
||||
]
|
||||
|
||||
def recent_screener_runs(
|
||||
self, user_id: int, trade_date: str, mode: str, limit: int = 40,
|
||||
) -> list[dict[str, Any]]:
|
||||
if int(user_id) == 0 or mode not in {"smart", "curated", "quant"}:
|
||||
return []
|
||||
safe_limit = max(1, min(160, int(limit)))
|
||||
with self.connect() as connection:
|
||||
rows = connection.execute(
|
||||
"""
|
||||
WITH ranked AS (
|
||||
SELECT id, trade_date, regime, mode, strategy_name, result, created_at,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY trade_date, mode, regime, strategy_name
|
||||
ORDER BY id DESC
|
||||
) AS context_rank
|
||||
FROM screener_runs
|
||||
WHERE user_id = ? AND trade_date <= ? AND mode = ?
|
||||
)
|
||||
SELECT id, trade_date, regime, mode, strategy_name, result, created_at
|
||||
FROM ranked
|
||||
WHERE context_rank = 1
|
||||
ORDER BY trade_date DESC, id DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
(int(user_id), trade_date, mode, safe_limit),
|
||||
).fetchall()
|
||||
return [
|
||||
payload
|
||||
for row in rows
|
||||
if (payload := self._screener_run_payload(row)) is not None
|
||||
]
|
||||
|
||||
def list_screener_batch_markers(
|
||||
self, end_date: str, limit: int = 30,
|
||||
) -> list[dict[str, Any]]:
|
||||
safe_limit = max(1, min(120, int(limit)))
|
||||
with self.connect() as connection:
|
||||
rows = connection.execute(
|
||||
"""
|
||||
SELECT cache_key, payload, updated_at
|
||||
FROM data_snapshots
|
||||
WHERE kind = 'screener_auto_v1' AND cache_key <= ?
|
||||
ORDER BY cache_key DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
(end_date, safe_limit),
|
||||
).fetchall()
|
||||
result = []
|
||||
for row in rows:
|
||||
try:
|
||||
payload = json.loads(row["payload"])
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
payload.setdefault("trade_date", str(row["cache_key"] or ""))
|
||||
payload.setdefault("updated_at", str(row["updated_at"] or ""))
|
||||
result.append(payload)
|
||||
return result
|
||||
|
||||
def get_screener_run(self, user_id: int, run_id: int) -> dict[str, Any] | None:
|
||||
owner_clause = "user_id IS NULL" if int(user_id) == 0 else "user_id = ?"
|
||||
parameters: tuple[Any, ...] = (int(run_id),)
|
||||
|
||||
Reference in New Issue
Block a user