rebuild(stage-6): deliver emotion and market pools
This commit is contained in:
@@ -126,6 +126,62 @@ class MarketRepository:
|
||||
)
|
||||
)
|
||||
|
||||
def active_stock_count(self, connection: sqlite3.Connection) -> int:
|
||||
row = connection.execute(
|
||||
"""
|
||||
SELECT COUNT(*) AS count FROM market_entities
|
||||
WHERE entity_type = 'stock' AND active = 1
|
||||
"""
|
||||
).fetchone()
|
||||
return int(row["count"] if row else 0)
|
||||
|
||||
def save_summary(
|
||||
self,
|
||||
connection: sqlite3.Connection,
|
||||
*,
|
||||
trade_date: str,
|
||||
observed_at: str,
|
||||
state: str,
|
||||
source: str,
|
||||
coverage: float,
|
||||
payload: dict[str, Any],
|
||||
) -> None:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO market_summaries
|
||||
(trade_date, observed_at, state, source, coverage, payload_json, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(trade_date) DO UPDATE SET
|
||||
observed_at = excluded.observed_at,
|
||||
state = excluded.state,
|
||||
source = excluded.source,
|
||||
coverage = excluded.coverage,
|
||||
payload_json = excluded.payload_json,
|
||||
created_at = excluded.created_at
|
||||
""",
|
||||
(
|
||||
trade_date,
|
||||
observed_at,
|
||||
state,
|
||||
source,
|
||||
coverage,
|
||||
json.dumps(payload, ensure_ascii=False, separators=(",", ":")),
|
||||
datetime.now().astimezone().isoformat(timespec="seconds"),
|
||||
),
|
||||
)
|
||||
|
||||
def summaries(
|
||||
self, connection: sqlite3.Connection, through: str, limit: int = 260
|
||||
) -> tuple[sqlite3.Row, ...]:
|
||||
rows = connection.execute(
|
||||
"""
|
||||
SELECT * FROM market_summaries WHERE trade_date <= ?
|
||||
ORDER BY trade_date DESC LIMIT ?
|
||||
""",
|
||||
(through, limit),
|
||||
).fetchall()
|
||||
return tuple(reversed(rows))
|
||||
|
||||
def latest_summary(self, connection: sqlite3.Connection, through: str) -> sqlite3.Row | None:
|
||||
return connection.execute(
|
||||
"SELECT * FROM market_summaries WHERE trade_date <= ? ORDER BY trade_date DESC LIMIT 1",
|
||||
|
||||
Reference in New Issue
Block a user