feat: improve stock detail view and update docs

This commit is contained in:
wanghep
2026-05-02 18:27:36 +08:00
parent a492c73cc6
commit 597aef1271
21 changed files with 1739 additions and 1481 deletions

View File

@ -7,7 +7,7 @@ from _bootstrap import add_src_to_path
add_src_to_path()
from lhbfx.after_market import SHANGHAI_TZ, default_trade_date, run_after_market_update
from lhbfx.after_market import default_trade_date, run_after_market_update
def parse_args() -> argparse.Namespace:

View File

@ -1,7 +1,6 @@
from __future__ import annotations
import argparse
from pathlib import Path
from _bootstrap import add_src_to_path

View File

@ -19,6 +19,7 @@ from .pipeline import (
)
from .queries import (
fetch_stock_detail,
search_stocks,
fetch_summary,
fetch_trader_actions,
fetch_trader_detail,
@ -128,6 +129,12 @@ def api_trader_detail(trader_id: int):
return data
@app.get("/api/stocks/search")
def api_stock_search(q: str, limit: int = 8):
safe_limit = min(max(limit, 1), 20)
return search_stocks(q, limit=safe_limit)
@app.get("/api/stocks/{stock_code}")
def api_stock_detail(stock_code: str):
data = fetch_stock_detail(stock_code)

View File

@ -176,6 +176,43 @@ def fetch_watchlist(include_archived: bool = False) -> list[dict[str, Any]]:
return [_normalize_row(row) for row in cursor.fetchall()]
def search_stocks(query: str, limit: int = 8) -> list[dict[str, Any]]:
keyword = query.strip()
if not keyword:
return []
prefix = f"{keyword}%"
contains = f"%{keyword}%"
with db_cursor() as (_, cursor):
cursor.execute(
"""
SELECT
stock_code,
stock_name,
market,
industry
FROM stocks
WHERE stock_code LIKE %s
OR stock_name LIKE %s
OR stock_name LIKE %s
ORDER BY
CASE
WHEN stock_code = %s THEN 0
WHEN stock_name = %s THEN 1
WHEN stock_code LIKE %s THEN 2
WHEN stock_name LIKE %s THEN 3
ELSE 4
END,
updated_at DESC,
stock_code
LIMIT %s
""",
(prefix, prefix, contains, keyword, keyword, prefix, prefix, limit),
)
return [_normalize_row(row) for row in cursor.fetchall()]
def upsert_watchlist_item(
stock_code: str,
stock_name: str,

View File

@ -1,7 +1,6 @@
from __future__ import annotations
import json
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import Any

View File

@ -4,7 +4,6 @@ import logging
import threading
import time
from datetime import datetime
from typing import Any
from .after_market import SHANGHAI_TZ, run_after_market_update
from .config import AppConfig, load_config