Spaces:
Sleeping
Sleeping
File size: 74,525 Bytes
74e7808 703c9df 74e7808 703c9df 74e7808 703c9df bedfe2d 703c9df c8f883a 703c9df c8f883a f0ebe10 29975ae c8f883a 29975ae c8f883a 29975ae f0ebe10 703c9df bedfe2d 703c9df c8f883a 703c9df bedfe2d 703c9df 74e7808 703c9df 74e7808 | 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 | # -*- coding: utf-8 -*-
"""
===================================
A股自选股智能分析系统 - 存储层
===================================
职责:
1. 管理 SQLite 数据库连接(单例模式)
2. 定义 ORM 数据模型
3. 提供数据存取接口
4. 实现智能更新逻辑(断点续传)
"""
import atexit
from contextlib import contextmanager
import hashlib
import json
import logging
import os
import re
from datetime import datetime, date, timedelta
from pathlib import Path
from typing import Optional, List, Dict, Any, TYPE_CHECKING, Tuple
import pandas as pd
from sqlalchemy import (
create_engine,
Column,
String,
Float,
Boolean,
Date,
DateTime,
Integer,
ForeignKey,
Index,
UniqueConstraint,
Text,
select,
and_,
or_,
delete,
desc,
func,
)
from sqlalchemy.orm import (
declarative_base,
sessionmaker,
Session,
)
from sqlalchemy.exc import IntegrityError
from src.config import get_config
logger = logging.getLogger(__name__)
# SQLAlchemy ORM 基类
Base = declarative_base()
if TYPE_CHECKING:
from src.search_service import SearchResponse
# === 数据模型定义 ===
class StockDaily(Base):
"""
股票日线数据模型
存储每日行情数据和计算的技术指标
支持多股票、多日期的唯一约束
"""
__tablename__ = 'stock_daily'
# 主键
id = Column(Integer, primary_key=True, autoincrement=True)
# 股票代码(如 600519, 000001)
code = Column(String(10), nullable=False, index=True)
# 交易日期
date = Column(Date, nullable=False, index=True)
# OHLC 数据
open = Column(Float)
high = Column(Float)
low = Column(Float)
close = Column(Float)
# 成交数据
volume = Column(Float) # 成交量(股)
amount = Column(Float) # 成交额(元)
pct_chg = Column(Float) # 涨跌幅(%)
# 技术指标
ma5 = Column(Float)
ma10 = Column(Float)
ma20 = Column(Float)
volume_ratio = Column(Float) # 量比
# 数据来源
data_source = Column(String(50)) # 记录数据来源(如 AkshareFetcher)
# 更新时间
created_at = Column(DateTime, default=datetime.now)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
# 唯一约束:同一股票同一日期只能有一条数据
__table_args__ = (
UniqueConstraint('code', 'date', name='uix_code_date'),
Index('ix_code_date', 'code', 'date'),
)
def __repr__(self):
return f"<StockDaily(code={self.code}, date={self.date}, close={self.close})>"
def to_dict(self) -> Dict[str, Any]:
"""转换为字典"""
return {
'code': self.code,
'date': self.date,
'open': self.open,
'high': self.high,
'low': self.low,
'close': self.close,
'volume': self.volume,
'amount': self.amount,
'pct_chg': self.pct_chg,
'ma5': self.ma5,
'ma10': self.ma10,
'ma20': self.ma20,
'volume_ratio': self.volume_ratio,
'data_source': self.data_source,
}
class NewsIntel(Base):
"""
新闻情报数据模型
存储搜索到的新闻情报条目,用于后续分析与查询
"""
__tablename__ = 'news_intel'
id = Column(Integer, primary_key=True, autoincrement=True)
# 关联用户查询操作
query_id = Column(String(64), index=True)
# 股票信息
code = Column(String(10), nullable=False, index=True)
name = Column(String(50))
# 搜索上下文
dimension = Column(String(32), index=True) # latest_news / risk_check / earnings / market_analysis / industry
query = Column(String(255))
provider = Column(String(32), index=True)
# 新闻内容
title = Column(String(300), nullable=False)
snippet = Column(Text)
url = Column(String(1000), nullable=False)
source = Column(String(100))
published_date = Column(DateTime, index=True)
# 入库时间
fetched_at = Column(DateTime, default=datetime.now, index=True)
query_source = Column(String(32), index=True) # bot/web/cli/system
requester_platform = Column(String(20))
requester_user_id = Column(String(64))
requester_user_name = Column(String(64))
requester_chat_id = Column(String(64))
requester_message_id = Column(String(64))
requester_query = Column(String(255))
__table_args__ = (
UniqueConstraint('url', name='uix_news_url'),
Index('ix_news_code_pub', 'code', 'published_date'),
)
def __repr__(self) -> str:
return f"<NewsIntel(code={self.code}, title={self.title[:20]}...)>"
class FundamentalSnapshot(Base):
"""
基本面上下文快照(P0 write-only)。
仅用于写入,主链路不依赖读取该表,便于后续回测/画像扩展。
"""
__tablename__ = 'fundamental_snapshot'
id = Column(Integer, primary_key=True, autoincrement=True)
query_id = Column(String(64), nullable=False, index=True)
code = Column(String(10), nullable=False, index=True)
payload = Column(Text, nullable=False)
source_chain = Column(Text)
coverage = Column(Text)
created_at = Column(DateTime, default=datetime.now, index=True)
__table_args__ = (
Index('ix_fundamental_snapshot_query_code', 'query_id', 'code'),
Index('ix_fundamental_snapshot_created', 'created_at'),
)
def __repr__(self) -> str:
return f"<FundamentalSnapshot(query_id={self.query_id}, code={self.code})>"
class AnalysisHistory(Base):
"""
分析结果历史记录模型
保存每次分析结果,支持按 query_id/股票代码检索
"""
__tablename__ = 'analysis_history'
id = Column(Integer, primary_key=True, autoincrement=True)
# 关联查询链路
query_id = Column(String(64), index=True)
# 股票信息
code = Column(String(10), nullable=False, index=True)
name = Column(String(50))
report_type = Column(String(16), index=True)
# 核心结论
sentiment_score = Column(Integer)
operation_advice = Column(String(20))
trend_prediction = Column(String(50))
analysis_summary = Column(Text)
# 详细数据
raw_result = Column(Text)
news_content = Column(Text)
context_snapshot = Column(Text)
# 狙击点位(用于回测)
ideal_buy = Column(Float)
secondary_buy = Column(Float)
stop_loss = Column(Float)
take_profit = Column(Float)
created_at = Column(DateTime, default=datetime.now, index=True)
__table_args__ = (
Index('ix_analysis_code_time', 'code', 'created_at'),
)
def to_dict(self) -> Dict[str, Any]:
"""转换为字典"""
return {
'id': self.id,
'query_id': self.query_id,
'code': self.code,
'name': self.name,
'report_type': self.report_type,
'sentiment_score': self.sentiment_score,
'operation_advice': self.operation_advice,
'trend_prediction': self.trend_prediction,
'analysis_summary': self.analysis_summary,
'raw_result': self.raw_result,
'news_content': self.news_content,
'context_snapshot': self.context_snapshot,
'ideal_buy': self.ideal_buy,
'secondary_buy': self.secondary_buy,
'stop_loss': self.stop_loss,
'take_profit': self.take_profit,
'created_at': self.created_at.isoformat() if self.created_at else None,
}
class BacktestResult(Base):
"""单条分析记录的回测结果。"""
__tablename__ = 'backtest_results'
id = Column(Integer, primary_key=True, autoincrement=True)
analysis_history_id = Column(
Integer,
ForeignKey('analysis_history.id'),
nullable=False,
index=True,
)
# 冗余字段,便于按股票筛选
code = Column(String(10), nullable=False, index=True)
analysis_date = Column(Date, index=True)
# 回测参数
eval_window_days = Column(Integer, nullable=False, default=10)
engine_version = Column(String(16), nullable=False, default='v1')
# 状态
eval_status = Column(String(16), nullable=False, default='pending')
evaluated_at = Column(DateTime, default=datetime.now, index=True)
# 建议快照(避免未来分析字段变化导致回测不可解释)
operation_advice = Column(String(20))
position_recommendation = Column(String(8)) # long/cash
# 价格与收益
start_price = Column(Float)
end_close = Column(Float)
max_high = Column(Float)
min_low = Column(Float)
stock_return_pct = Column(Float)
# 方向与结果
direction_expected = Column(String(16)) # up/down/flat/not_down
direction_correct = Column(Boolean, nullable=True)
outcome = Column(String(16)) # win/loss/neutral
# 目标价命中(仅 long 且配置了止盈/止损时有意义)
stop_loss = Column(Float)
take_profit = Column(Float)
hit_stop_loss = Column(Boolean)
hit_take_profit = Column(Boolean)
first_hit = Column(String(16)) # take_profit/stop_loss/ambiguous/neither/not_applicable
first_hit_date = Column(Date)
first_hit_trading_days = Column(Integer)
# 模拟执行(long-only)
simulated_entry_price = Column(Float)
simulated_exit_price = Column(Float)
simulated_exit_reason = Column(String(24)) # stop_loss/take_profit/window_end/cash/ambiguous_stop_loss
simulated_return_pct = Column(Float)
__table_args__ = (
UniqueConstraint(
'analysis_history_id',
'eval_window_days',
'engine_version',
name='uix_backtest_analysis_window_version',
),
Index('ix_backtest_code_date', 'code', 'analysis_date'),
)
class BacktestSummary(Base):
"""回测汇总指标(按股票或全局)。"""
__tablename__ = 'backtest_summaries'
id = Column(Integer, primary_key=True, autoincrement=True)
scope = Column(String(16), nullable=False, index=True) # overall/stock
code = Column(String(16), index=True)
eval_window_days = Column(Integer, nullable=False, default=10)
engine_version = Column(String(16), nullable=False, default='v1')
computed_at = Column(DateTime, default=datetime.now, index=True)
# 计数
total_evaluations = Column(Integer, default=0)
completed_count = Column(Integer, default=0)
insufficient_count = Column(Integer, default=0)
long_count = Column(Integer, default=0)
cash_count = Column(Integer, default=0)
win_count = Column(Integer, default=0)
loss_count = Column(Integer, default=0)
neutral_count = Column(Integer, default=0)
# 准确率/胜率
direction_accuracy_pct = Column(Float)
win_rate_pct = Column(Float)
neutral_rate_pct = Column(Float)
# 收益
avg_stock_return_pct = Column(Float)
avg_simulated_return_pct = Column(Float)
# 目标价触发统计(仅 long 且配置止盈/止损时统计)
stop_loss_trigger_rate = Column(Float)
take_profit_trigger_rate = Column(Float)
ambiguous_rate = Column(Float)
avg_days_to_first_hit = Column(Float)
# 诊断字段(JSON 字符串)
advice_breakdown_json = Column(Text)
diagnostics_json = Column(Text)
__table_args__ = (
UniqueConstraint(
'scope',
'code',
'eval_window_days',
'engine_version',
name='uix_backtest_summary_scope_code_window_version',
),
)
class PortfolioAccount(Base):
"""Portfolio account metadata."""
__tablename__ = 'portfolio_accounts'
id = Column(Integer, primary_key=True, autoincrement=True)
owner_id = Column(String(64), index=True)
name = Column(String(64), nullable=False)
broker = Column(String(64))
market = Column(String(8), nullable=False, default='cn', index=True) # cn/hk/us
base_currency = Column(String(8), nullable=False, default='CNY')
is_active = Column(Boolean, nullable=False, default=True, index=True)
created_at = Column(DateTime, default=datetime.now, index=True)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
__table_args__ = (
Index('ix_portfolio_account_owner_active', 'owner_id', 'is_active'),
)
class PortfolioTrade(Base):
"""Executed trade events used as the source of truth for replay."""
__tablename__ = 'portfolio_trades'
id = Column(Integer, primary_key=True, autoincrement=True)
account_id = Column(Integer, ForeignKey('portfolio_accounts.id'), nullable=False, index=True)
trade_uid = Column(String(128))
symbol = Column(String(16), nullable=False, index=True)
market = Column(String(8), nullable=False, default='cn')
currency = Column(String(8), nullable=False, default='CNY')
trade_date = Column(Date, nullable=False, index=True)
side = Column(String(8), nullable=False) # buy/sell
quantity = Column(Float, nullable=False)
price = Column(Float, nullable=False)
fee = Column(Float, default=0.0)
tax = Column(Float, default=0.0)
note = Column(String(255))
dedup_hash = Column(String(64), index=True)
created_at = Column(DateTime, default=datetime.now, index=True)
__table_args__ = (
UniqueConstraint('account_id', 'trade_uid', name='uix_portfolio_trade_uid'),
UniqueConstraint('account_id', 'dedup_hash', name='uix_portfolio_trade_dedup_hash'),
Index('ix_portfolio_trade_account_date', 'account_id', 'trade_date'),
)
class PortfolioCashLedger(Base):
"""Cash in/out events."""
__tablename__ = 'portfolio_cash_ledger'
id = Column(Integer, primary_key=True, autoincrement=True)
account_id = Column(Integer, ForeignKey('portfolio_accounts.id'), nullable=False, index=True)
event_date = Column(Date, nullable=False, index=True)
direction = Column(String(8), nullable=False) # in/out
amount = Column(Float, nullable=False)
currency = Column(String(8), nullable=False, default='CNY')
note = Column(String(255))
created_at = Column(DateTime, default=datetime.now, index=True)
__table_args__ = (
Index('ix_portfolio_cash_account_date', 'account_id', 'event_date'),
)
class PortfolioCorporateAction(Base):
"""Corporate actions that impact cash or share quantity."""
__tablename__ = 'portfolio_corporate_actions'
id = Column(Integer, primary_key=True, autoincrement=True)
account_id = Column(Integer, ForeignKey('portfolio_accounts.id'), nullable=False, index=True)
symbol = Column(String(16), nullable=False, index=True)
market = Column(String(8), nullable=False, default='cn')
currency = Column(String(8), nullable=False, default='CNY')
effective_date = Column(Date, nullable=False, index=True)
action_type = Column(String(24), nullable=False) # cash_dividend/split_adjustment
cash_dividend_per_share = Column(Float)
split_ratio = Column(Float)
note = Column(String(255))
created_at = Column(DateTime, default=datetime.now, index=True)
__table_args__ = (
Index('ix_portfolio_ca_account_date', 'account_id', 'effective_date'),
)
class PortfolioPosition(Base):
"""Latest replayed position snapshot for each symbol in one account."""
__tablename__ = 'portfolio_positions'
id = Column(Integer, primary_key=True, autoincrement=True)
account_id = Column(Integer, ForeignKey('portfolio_accounts.id'), nullable=False, index=True)
cost_method = Column(String(8), nullable=False, default='fifo')
symbol = Column(String(16), nullable=False, index=True)
market = Column(String(8), nullable=False, default='cn')
currency = Column(String(8), nullable=False, default='CNY')
quantity = Column(Float, nullable=False, default=0.0)
avg_cost = Column(Float, nullable=False, default=0.0)
total_cost = Column(Float, nullable=False, default=0.0)
last_price = Column(Float, nullable=False, default=0.0)
market_value_base = Column(Float, nullable=False, default=0.0)
unrealized_pnl_base = Column(Float, nullable=False, default=0.0)
valuation_currency = Column(String(8), nullable=False, default='CNY')
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, index=True)
__table_args__ = (
UniqueConstraint(
'account_id',
'symbol',
'market',
'currency',
'cost_method',
name='uix_portfolio_position_account_symbol_market_currency',
),
)
class PortfolioPositionLot(Base):
"""Lot-level remaining quantities used by FIFO replay."""
__tablename__ = 'portfolio_position_lots'
id = Column(Integer, primary_key=True, autoincrement=True)
account_id = Column(Integer, ForeignKey('portfolio_accounts.id'), nullable=False, index=True)
cost_method = Column(String(8), nullable=False, default='fifo')
symbol = Column(String(16), nullable=False, index=True)
market = Column(String(8), nullable=False, default='cn')
currency = Column(String(8), nullable=False, default='CNY')
open_date = Column(Date, nullable=False, index=True)
remaining_quantity = Column(Float, nullable=False, default=0.0)
unit_cost = Column(Float, nullable=False, default=0.0)
source_trade_id = Column(Integer, ForeignKey('portfolio_trades.id'))
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, index=True)
__table_args__ = (
Index('ix_portfolio_lot_account_symbol', 'account_id', 'symbol'),
)
class PortfolioDailySnapshot(Base):
"""Daily account snapshot generated by read-time replay."""
__tablename__ = 'portfolio_daily_snapshots'
id = Column(Integer, primary_key=True, autoincrement=True)
account_id = Column(Integer, ForeignKey('portfolio_accounts.id'), nullable=False, index=True)
snapshot_date = Column(Date, nullable=False, index=True)
cost_method = Column(String(8), nullable=False, default='fifo') # fifo/avg
base_currency = Column(String(8), nullable=False, default='CNY')
total_cash = Column(Float, nullable=False, default=0.0)
total_market_value = Column(Float, nullable=False, default=0.0)
total_equity = Column(Float, nullable=False, default=0.0)
unrealized_pnl = Column(Float, nullable=False, default=0.0)
realized_pnl = Column(Float, nullable=False, default=0.0)
fee_total = Column(Float, nullable=False, default=0.0)
tax_total = Column(Float, nullable=False, default=0.0)
fx_stale = Column(Boolean, nullable=False, default=False)
payload = Column(Text)
created_at = Column(DateTime, default=datetime.now, index=True)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
__table_args__ = (
UniqueConstraint(
'account_id',
'snapshot_date',
'cost_method',
name='uix_portfolio_snapshot_account_date_method',
),
)
class PortfolioFxRate(Base):
"""Cached FX rates used for cross-currency portfolio conversion."""
__tablename__ = 'portfolio_fx_rates'
id = Column(Integer, primary_key=True, autoincrement=True)
from_currency = Column(String(8), nullable=False, index=True)
to_currency = Column(String(8), nullable=False, index=True)
rate_date = Column(Date, nullable=False, index=True)
rate = Column(Float, nullable=False)
source = Column(String(32), nullable=False, default='manual')
is_stale = Column(Boolean, nullable=False, default=False)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
__table_args__ = (
UniqueConstraint(
'from_currency',
'to_currency',
'rate_date',
name='uix_portfolio_fx_pair_date',
),
)
class ConversationMessage(Base):
"""
Agent 对话历史记录表
"""
__tablename__ = 'conversation_messages'
id = Column(Integer, primary_key=True, autoincrement=True)
session_id = Column(String(100), index=True, nullable=False)
role = Column(String(20), nullable=False) # user, assistant, system
content = Column(Text, nullable=False)
created_at = Column(DateTime, default=datetime.now, index=True)
class LLMUsage(Base):
"""One row per litellm.completion() call — token-usage audit log."""
__tablename__ = 'llm_usage'
id = Column(Integer, primary_key=True, autoincrement=True)
# 'analysis' | 'agent' | 'market_review'
call_type = Column(String(32), nullable=False, index=True)
model = Column(String(128), nullable=False)
stock_code = Column(String(16), nullable=True)
prompt_tokens = Column(Integer, nullable=False, default=0)
completion_tokens = Column(Integer, nullable=False, default=0)
total_tokens = Column(Integer, nullable=False, default=0)
called_at = Column(DateTime, default=datetime.now, index=True)
class DatabaseManager:
"""
数据库管理器 - 单例模式
职责:
1. 管理数据库连接池
2. 提供 Session 上下文管理
3. 封装数据存取操作
"""
_instance: Optional['DatabaseManager'] = None
_initialized: bool = False
def __new__(cls, *args, **kwargs):
"""单例模式实现"""
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self, db_url: Optional[str] = None):
"""
初始化数据库管理器
Args:
db_url: 数据库连接 URL(可选,默认从配置读取)
"""
if getattr(self, '_initialized', False):
return
turso_url = os.getenv("TURSO_DATABASE_URL")
turso_token = os.getenv("TURSO_AUTH_TOKEN")
if turso_url and turso_token:
# 使用 Turso 云数据库(直连远端,无本地副本,避免 embedded replica 状态问题)
import libsql_experimental as libsql
from sqlalchemy.pool import NullPool
_turso_url = turso_url
_turso_token = turso_token
class _LibSQLCursor:
"""Wraps libsql cursor to suppress SAVEPOINT commands (not supported by Hrana)."""
def __init__(self, cursor):
self._c = cursor
self._noop = False
@property
def description(self):
return None if self._noop else self._c.description
@property
def rowcount(self):
return -1 if self._noop else self._c.rowcount
def execute(self, sql, params=None):
s = sql.strip().upper()
if (s.startswith('SAVEPOINT') or
s.startswith('RELEASE SAVEPOINT') or
s.startswith('ROLLBACK TO SAVEPOINT')):
self._noop = True
return self
self._noop = False
if params is not None:
return self._c.execute(sql, params)
return self._c.execute(sql)
def fetchall(self):
return [] if self._noop else self._c.fetchall()
def fetchone(self):
return None if self._noop else self._c.fetchone()
def fetchmany(self, size=None):
if self._noop:
return []
return self._c.fetchmany(size) if size is not None else self._c.fetchmany()
def close(self):
return self._c.close()
def __iter__(self):
return iter(self._c)
def __getattr__(self, name):
return getattr(self._c, name)
class _LibSQLWrapper:
"""让 libsql 连接兼容 SQLAlchemy pysqlite 方言。"""
def __init__(self, conn):
self._conn = conn
def __getattr__(self, name):
return getattr(self._conn, name)
def create_function(self, name, num_params, func, **kwargs):
pass # libsql 不支持,静默忽略
def cursor(self):
return _LibSQLCursor(self._conn.cursor())
def _creator():
# 每次 session 创建一个新的远端连接,避免连接状态污染
return _LibSQLWrapper(
libsql.connect(_turso_url, auth_token=_turso_token)
)
self._engine = create_engine(
"sqlite://",
creator=_creator,
poolclass=NullPool,
)
logger.info(f"数据库初始化完成 (Turso remote): {turso_url}")
else:
if db_url is None:
config = get_config()
db_url = config.get_db_url()
# 本地 SQLite(默认)
self._engine = create_engine(
db_url,
echo=False,
pool_pre_ping=True,
)
logger.info(f"数据库初始化完成: {db_url}")
# 创建 Session 工厂
self._SessionLocal = sessionmaker(
bind=self._engine,
autocommit=False,
autoflush=False,
)
# 创建所有表
Base.metadata.create_all(self._engine)
self._initialized = True
# 注册退出钩子,确保程序退出时关闭数据库连接
atexit.register(DatabaseManager._cleanup_engine, self._engine)
@classmethod
def get_instance(cls) -> 'DatabaseManager':
"""获取单例实例"""
if cls._instance is None:
cls._instance = cls()
return cls._instance
@classmethod
def reset_instance(cls) -> None:
"""重置单例(用于测试)"""
if cls._instance is not None:
if hasattr(cls._instance, '_engine') and cls._instance._engine is not None:
cls._instance._engine.dispose()
cls._instance._initialized = False
cls._instance = None
@classmethod
def _cleanup_engine(cls, engine) -> None:
"""
清理数据库引擎(atexit 钩子)
确保程序退出时关闭所有数据库连接,避免 ResourceWarning
Args:
engine: SQLAlchemy 引擎对象
"""
try:
if engine is not None:
engine.dispose()
logger.debug("数据库引擎已清理")
except Exception as e:
logger.warning(f"清理数据库引擎时出错: {e}")
def get_session(self) -> Session:
"""
获取数据库 Session
使用示例:
with db.get_session() as session:
# 执行查询
session.commit() # 如果需要
"""
if not getattr(self, '_initialized', False) or not hasattr(self, '_SessionLocal'):
raise RuntimeError(
"DatabaseManager 未正确初始化。"
"请确保通过 DatabaseManager.get_instance() 获取实例。"
)
session = self._SessionLocal()
try:
return session
except Exception:
session.close()
raise
@contextmanager
def session_scope(self):
"""Provide a transactional scope around a series of operations."""
session = self.get_session()
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
def has_today_data(self, code: str, target_date: Optional[date] = None) -> bool:
"""
检查是否已有指定日期的数据
用于断点续传逻辑:如果已有数据则跳过网络请求
Args:
code: 股票代码
target_date: 目标日期(默认今天)
Returns:
是否存在数据
"""
if target_date is None:
target_date = date.today()
# 注意:这里的 target_date 语义是“自然日”,而不是“最新交易日”。
# 在周末/节假日/非交易日运行时,即使数据库已有最新交易日数据,这里也会返回 False。
# 该行为目前保留(按需求不改逻辑)。
with self.get_session() as session:
result = session.execute(
select(StockDaily).where(
and_(
StockDaily.code == code,
StockDaily.date == target_date
)
)
).scalar_one_or_none()
return result is not None
def get_latest_data(
self,
code: str,
days: int = 2
) -> List[StockDaily]:
"""
获取最近 N 天的数据
用于计算"相比昨日"的变化
Args:
code: 股票代码
days: 获取天数
Returns:
StockDaily 对象列表(按日期降序)
"""
with self.get_session() as session:
results = session.execute(
select(StockDaily)
.where(StockDaily.code == code)
.order_by(desc(StockDaily.date))
.limit(days)
).scalars().all()
return list(results)
def save_news_intel(
self,
code: str,
name: str,
dimension: str,
query: str,
response: 'SearchResponse',
query_context: Optional[Dict[str, str]] = None
) -> int:
"""
保存新闻情报到数据库
去重策略:
- 优先按 URL 去重(唯一约束)
- URL 缺失时按 title + source + published_date 进行软去重
关联策略:
- query_context 记录用户查询信息(平台、用户、会话、原始指令等)
"""
if not response or not response.results:
return 0
saved_count = 0
query_ctx = query_context or {}
current_query_id = (query_ctx.get("query_id") or "").strip()
with self.get_session() as session:
try:
for item in response.results:
title = (item.title or '').strip()
url = (item.url or '').strip()
source = (item.source or '').strip()
snippet = (item.snippet or '').strip()
published_date = self._parse_published_date(item.published_date)
if not title and not url:
continue
url_key = url or self._build_fallback_url_key(
code=code,
title=title,
source=source,
published_date=published_date
)
# 优先按 URL 或兜底键去重
existing = session.execute(
select(NewsIntel).where(NewsIntel.url == url_key)
).scalar_one_or_none()
if existing:
existing.name = name or existing.name
existing.dimension = dimension or existing.dimension
existing.query = query or existing.query
existing.provider = response.provider or existing.provider
existing.snippet = snippet or existing.snippet
existing.source = source or existing.source
existing.published_date = published_date or existing.published_date
existing.fetched_at = datetime.now()
if query_context:
# Keep the first query_id to avoid overwriting historical links.
if not existing.query_id and current_query_id:
existing.query_id = current_query_id
existing.query_source = (
query_context.get("query_source") or existing.query_source
)
existing.requester_platform = (
query_context.get("requester_platform") or existing.requester_platform
)
existing.requester_user_id = (
query_context.get("requester_user_id") or existing.requester_user_id
)
existing.requester_user_name = (
query_context.get("requester_user_name") or existing.requester_user_name
)
existing.requester_chat_id = (
query_context.get("requester_chat_id") or existing.requester_chat_id
)
existing.requester_message_id = (
query_context.get("requester_message_id") or existing.requester_message_id
)
existing.requester_query = (
query_context.get("requester_query") or existing.requester_query
)
else:
try:
with session.begin_nested():
record = NewsIntel(
code=code,
name=name,
dimension=dimension,
query=query,
provider=response.provider,
title=title,
snippet=snippet,
url=url_key,
source=source,
published_date=published_date,
fetched_at=datetime.now(),
query_id=current_query_id or None,
query_source=query_ctx.get("query_source"),
requester_platform=query_ctx.get("requester_platform"),
requester_user_id=query_ctx.get("requester_user_id"),
requester_user_name=query_ctx.get("requester_user_name"),
requester_chat_id=query_ctx.get("requester_chat_id"),
requester_message_id=query_ctx.get("requester_message_id"),
requester_query=query_ctx.get("requester_query"),
)
session.add(record)
session.flush()
saved_count += 1
except IntegrityError:
# 单条 URL 唯一约束冲突(如并发插入),仅跳过本条,保留本批其余成功项
logger.debug("新闻情报重复(已跳过): %s %s", code, url_key)
session.commit()
logger.info(f"保存新闻情报成功: {code}, 新增 {saved_count} 条")
except Exception as e:
session.rollback()
logger.error(f"保存新闻情报失败: {e}")
raise
return saved_count
def save_fundamental_snapshot(
self,
query_id: str,
code: str,
payload: Optional[Dict[str, Any]],
source_chain: Optional[Any] = None,
coverage: Optional[Any] = None,
) -> int:
"""
保存基本面快照(P0 write-only)。失败不抛异常,返回写入条数 0/1。
"""
if not query_id or not code or payload is None:
return 0
with self.get_session() as session:
try:
session.add(
FundamentalSnapshot(
query_id=query_id,
code=code,
payload=self._safe_json_dumps(payload),
source_chain=self._safe_json_dumps(source_chain or []),
coverage=self._safe_json_dumps(coverage or {}),
)
)
session.commit()
return 1
except Exception as e:
session.rollback()
logger.debug(
"基本面快照写入失败(fail-open): query_id=%s code=%s err=%s",
query_id,
code,
e,
)
return 0
def get_latest_fundamental_snapshot(
self,
query_id: str,
code: str,
) -> Optional[Dict[str, Any]]:
"""
获取指定 query_id + code 的最新基本面快照 payload。
读取失败或不存在时返回 None(fail-open)。
"""
if not query_id or not code:
return None
with self.get_session() as session:
try:
row = session.execute(
select(FundamentalSnapshot)
.where(
and_(
FundamentalSnapshot.query_id == query_id,
FundamentalSnapshot.code == code,
)
)
.order_by(desc(FundamentalSnapshot.created_at))
.limit(1)
).scalar_one_or_none()
except Exception as e:
logger.debug(
"基本面快照读取失败(fail-open): query_id=%s code=%s err=%s",
query_id,
code,
e,
)
return None
if row is None:
return None
try:
payload = json.loads(row.payload or "{}")
return payload if isinstance(payload, dict) else None
except Exception:
return None
def get_recent_news(self, code: str, days: int = 7, limit: int = 20) -> List[NewsIntel]:
"""
获取指定股票最近 N 天的新闻情报
"""
cutoff_date = datetime.now() - timedelta(days=days)
with self.get_session() as session:
results = session.execute(
select(NewsIntel)
.where(
and_(
NewsIntel.code == code,
NewsIntel.fetched_at >= cutoff_date
)
)
.order_by(desc(NewsIntel.fetched_at))
.limit(limit)
).scalars().all()
return list(results)
def get_news_intel_by_query_id(self, query_id: str, limit: int = 20) -> List[NewsIntel]:
"""
根据 query_id 获取新闻情报列表
Args:
query_id: 分析记录唯一标识
limit: 返回数量限制
Returns:
NewsIntel 列表(按发布时间或抓取时间倒序)
"""
from sqlalchemy import func
with self.get_session() as session:
results = session.execute(
select(NewsIntel)
.where(NewsIntel.query_id == query_id)
.order_by(
desc(func.coalesce(NewsIntel.published_date, NewsIntel.fetched_at)),
desc(NewsIntel.fetched_at)
)
.limit(limit)
).scalars().all()
return list(results)
def save_analysis_history(
self,
result: Any,
query_id: str,
report_type: str,
news_content: Optional[str],
context_snapshot: Optional[Dict[str, Any]] = None,
save_snapshot: bool = True
) -> int:
"""
保存分析结果历史记录
"""
if result is None:
return 0
sniper_points = self._extract_sniper_points(result)
raw_result = self._build_raw_result(result)
context_text = None
if save_snapshot and context_snapshot is not None:
context_text = self._safe_json_dumps(context_snapshot)
record = AnalysisHistory(
query_id=query_id,
code=result.code,
name=result.name,
report_type=report_type,
sentiment_score=result.sentiment_score,
operation_advice=result.operation_advice,
trend_prediction=result.trend_prediction,
analysis_summary=result.analysis_summary,
raw_result=self._safe_json_dumps(raw_result),
news_content=news_content,
context_snapshot=context_text,
ideal_buy=sniper_points.get("ideal_buy"),
secondary_buy=sniper_points.get("secondary_buy"),
stop_loss=sniper_points.get("stop_loss"),
take_profit=sniper_points.get("take_profit"),
created_at=datetime.now(),
)
with self.get_session() as session:
try:
session.add(record)
session.commit()
return 1
except Exception as e:
session.rollback()
logger.error(f"保存分析历史失败: {e}")
return 0
def get_analysis_history(
self,
code: Optional[str] = None,
query_id: Optional[str] = None,
days: int = 30,
limit: int = 50,
exclude_query_id: Optional[str] = None,
) -> List[AnalysisHistory]:
"""
Query analysis history records.
Notes:
- If query_id is provided, perform exact lookup and ignore days window.
- If query_id is not provided, apply days-based time filtering.
- exclude_query_id: exclude records with this query_id (for history comparison).
"""
cutoff_date = datetime.now() - timedelta(days=days)
with self.get_session() as session:
conditions = []
if query_id:
conditions.append(AnalysisHistory.query_id == query_id)
else:
conditions.append(AnalysisHistory.created_at >= cutoff_date)
if code:
conditions.append(AnalysisHistory.code == code)
# exclude_query_id only applies when not doing exact lookup (query_id is None)
if exclude_query_id and not query_id:
conditions.append(AnalysisHistory.query_id != exclude_query_id)
results = session.execute(
select(AnalysisHistory)
.where(and_(*conditions))
.order_by(desc(AnalysisHistory.created_at))
.limit(limit)
).scalars().all()
return list(results)
def get_analysis_history_paginated(
self,
code: Optional[str] = None,
start_date: Optional[date] = None,
end_date: Optional[date] = None,
offset: int = 0,
limit: int = 20
) -> Tuple[List[AnalysisHistory], int]:
"""
分页查询分析历史记录(带总数)
Args:
code: 股票代码筛选
start_date: 开始日期(含)
end_date: 结束日期(含)
offset: 偏移量(跳过前 N 条)
limit: 每页数量
Returns:
Tuple[List[AnalysisHistory], int]: (记录列表, 总数)
"""
from sqlalchemy import func
with self.get_session() as session:
conditions = []
if code:
conditions.append(AnalysisHistory.code == code)
if start_date:
# created_at >= start_date 00:00:00
conditions.append(AnalysisHistory.created_at >= datetime.combine(start_date, datetime.min.time()))
if end_date:
# created_at < end_date+1 00:00:00 (即 <= end_date 23:59:59)
conditions.append(AnalysisHistory.created_at < datetime.combine(end_date + timedelta(days=1), datetime.min.time()))
# 构建 where 子句
where_clause = and_(*conditions) if conditions else True
# 查询总数
total_query = select(func.count(AnalysisHistory.id)).where(where_clause)
total = session.execute(total_query).scalar() or 0
# 查询分页数据
data_query = (
select(AnalysisHistory)
.where(where_clause)
.order_by(desc(AnalysisHistory.created_at))
.offset(offset)
.limit(limit)
)
results = session.execute(data_query).scalars().all()
return list(results), total
def get_analysis_history_by_id(self, record_id: int) -> Optional[AnalysisHistory]:
"""
根据数据库主键 ID 查询单条分析历史记录
由于 query_id 可能重复(批量分析时多条记录共享同一 query_id),
使用主键 ID 确保精确查询唯一记录。
Args:
record_id: 分析历史记录的主键 ID
Returns:
AnalysisHistory 对象,不存在返回 None
"""
with self.get_session() as session:
result = session.execute(
select(AnalysisHistory).where(AnalysisHistory.id == record_id)
).scalars().first()
return result
def delete_analysis_history_records(self, record_ids: List[int]) -> int:
"""
删除指定的分析历史记录。
同时清理依赖这些历史记录的回测结果,避免外键约束失败。
Args:
record_ids: 要删除的历史记录主键 ID 列表
Returns:
实际删除的历史记录数量
"""
ids = sorted({int(record_id) for record_id in record_ids if record_id is not None})
if not ids:
return 0
with self.session_scope() as session:
session.execute(
delete(BacktestResult).where(BacktestResult.analysis_history_id.in_(ids))
)
result = session.execute(
delete(AnalysisHistory).where(AnalysisHistory.id.in_(ids))
)
return result.rowcount or 0
def get_latest_analysis_by_query_id(self, query_id: str) -> Optional[AnalysisHistory]:
"""
根据 query_id 查询最新一条分析历史记录
query_id 在批量分析时可能重复,故返回最近创建的一条。
Args:
query_id: 分析记录关联的 query_id
Returns:
AnalysisHistory 对象,不存在返回 None
"""
with self.get_session() as session:
result = session.execute(
select(AnalysisHistory)
.where(AnalysisHistory.query_id == query_id)
.order_by(desc(AnalysisHistory.created_at))
.limit(1)
).scalars().first()
return result
def get_data_range(
self,
code: str,
start_date: date,
end_date: date
) -> List[StockDaily]:
"""
获取指定日期范围的数据
Args:
code: 股票代码
start_date: 开始日期
end_date: 结束日期
Returns:
StockDaily 对象列表
"""
with self.get_session() as session:
results = session.execute(
select(StockDaily)
.where(
and_(
StockDaily.code == code,
StockDaily.date >= start_date,
StockDaily.date <= end_date
)
)
.order_by(StockDaily.date)
).scalars().all()
return list(results)
def save_daily_data(
self,
df: pd.DataFrame,
code: str,
data_source: str = "Unknown"
) -> int:
"""
保存日线数据到数据库
策略:
- 使用 UPSERT 逻辑(存在则更新,不存在则插入)
- 跳过已存在的数据,避免重复
Args:
df: 包含日线数据的 DataFrame
code: 股票代码
data_source: 数据来源名称
Returns:
新增/更新的记录数
"""
if df is None or df.empty:
logger.warning(f"保存数据为空,跳过 {code}")
return 0
saved_count = 0
with self.get_session() as session:
try:
for _, row in df.iterrows():
# 解析日期
row_date = row.get('date')
if isinstance(row_date, str):
row_date = datetime.strptime(row_date, '%Y-%m-%d').date()
elif isinstance(row_date, datetime):
row_date = row_date.date()
elif isinstance(row_date, pd.Timestamp):
row_date = row_date.date()
# 检查是否已存在
existing = session.execute(
select(StockDaily).where(
and_(
StockDaily.code == code,
StockDaily.date == row_date
)
)
).scalar_one_or_none()
if existing:
# 更新现有记录
existing.open = row.get('open')
existing.high = row.get('high')
existing.low = row.get('low')
existing.close = row.get('close')
existing.volume = row.get('volume')
existing.amount = row.get('amount')
existing.pct_chg = row.get('pct_chg')
existing.ma5 = row.get('ma5')
existing.ma10 = row.get('ma10')
existing.ma20 = row.get('ma20')
existing.volume_ratio = row.get('volume_ratio')
existing.data_source = data_source
existing.updated_at = datetime.now()
else:
# 创建新记录
record = StockDaily(
code=code,
date=row_date,
open=row.get('open'),
high=row.get('high'),
low=row.get('low'),
close=row.get('close'),
volume=row.get('volume'),
amount=row.get('amount'),
pct_chg=row.get('pct_chg'),
ma5=row.get('ma5'),
ma10=row.get('ma10'),
ma20=row.get('ma20'),
volume_ratio=row.get('volume_ratio'),
data_source=data_source,
)
session.add(record)
saved_count += 1
session.commit()
logger.info(f"保存 {code} 数据成功,新增 {saved_count} 条")
except Exception as e:
session.rollback()
logger.error(f"保存 {code} 数据失败: {e}")
raise
return saved_count
def get_analysis_context(
self,
code: str,
target_date: Optional[date] = None
) -> Optional[Dict[str, Any]]:
"""
获取分析所需的上下文数据
返回今日数据 + 昨日数据的对比信息
Args:
code: 股票代码
target_date: 目标日期(默认今天)
Returns:
包含今日数据、昨日对比等信息的字典
"""
if target_date is None:
target_date = date.today()
# 注意:尽管入参提供了 target_date,但当前实现实际使用的是“最新两天数据”(get_latest_data),
# 并不会按 target_date 精确取当日/前一交易日的上下文。
# 因此若未来需要支持“按历史某天复盘/重算”的可解释性,这里需要调整。
# 该行为目前保留(按需求不改逻辑)。
# 获取最近2天数据
recent_data = self.get_latest_data(code, days=2)
if not recent_data:
logger.warning(f"未找到 {code} 的数据")
return None
today_data = recent_data[0]
yesterday_data = recent_data[1] if len(recent_data) > 1 else None
context = {
'code': code,
'date': today_data.date.isoformat(),
'today': today_data.to_dict(),
}
if yesterday_data:
context['yesterday'] = yesterday_data.to_dict()
# 计算相比昨日的变化
if yesterday_data.volume and yesterday_data.volume > 0:
context['volume_change_ratio'] = round(
today_data.volume / yesterday_data.volume, 2
)
if yesterday_data.close and yesterday_data.close > 0:
context['price_change_ratio'] = round(
(today_data.close - yesterday_data.close) / yesterday_data.close * 100, 2
)
# 均线形态判断
context['ma_status'] = self._analyze_ma_status(today_data)
return context
def _analyze_ma_status(self, data: StockDaily) -> str:
"""
分析均线形态
判断条件:
- 多头排列:close > ma5 > ma10 > ma20
- 空头排列:close < ma5 < ma10 < ma20
- 震荡整理:其他情况
"""
# 注意:这里的均线形态判断基于“close/ma5/ma10/ma20”静态比较,
# 未考虑均线拐点、斜率、或不同数据源复权口径差异。
# 该行为目前保留(按需求不改逻辑)。
close = data.close or 0
ma5 = data.ma5 or 0
ma10 = data.ma10 or 0
ma20 = data.ma20 or 0
if close > ma5 > ma10 > ma20 > 0:
return "多头排列 📈"
elif close < ma5 < ma10 < ma20 and ma20 > 0:
return "空头排列 📉"
elif close > ma5 and ma5 > ma10:
return "短期向好 🔼"
elif close < ma5 and ma5 < ma10:
return "短期走弱 🔽"
else:
return "震荡整理 ↔️"
@staticmethod
def _parse_published_date(value: Optional[str]) -> Optional[datetime]:
"""
解析发布时间字符串(失败返回 None)
"""
if not value:
return None
if isinstance(value, datetime):
return value
text = str(value).strip()
if not text:
return None
# 优先尝试 ISO 格式
try:
return datetime.fromisoformat(text)
except ValueError:
pass
for fmt in (
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d %H:%M",
"%Y-%m-%d",
"%Y/%m/%d %H:%M:%S",
"%Y/%m/%d %H:%M",
"%Y/%m/%d",
):
try:
return datetime.strptime(text, fmt)
except ValueError:
continue
return None
@staticmethod
def _safe_json_dumps(data: Any) -> str:
"""
安全序列化为 JSON 字符串
"""
try:
return json.dumps(data, ensure_ascii=False, default=str)
except Exception:
return json.dumps(str(data), ensure_ascii=False)
@staticmethod
def _build_raw_result(result: Any) -> Dict[str, Any]:
"""
生成完整分析结果字典
"""
data = result.to_dict() if hasattr(result, "to_dict") else {}
data.update({
'data_sources': getattr(result, 'data_sources', ''),
'raw_response': getattr(result, 'raw_response', None),
})
return data
@staticmethod
def _parse_sniper_value(value: Any) -> Optional[float]:
"""
Parse a sniper point value from various formats to float.
Handles: numeric types, plain number strings, Chinese price formats
like "18.50元", range formats like "18.50-19.00", and text with
embedded numbers while filtering out MA indicators.
"""
if value is None:
return None
if isinstance(value, (int, float)):
v = float(value)
return v if v > 0 else None
text = str(value).replace(',', '').replace(',', '').strip()
if not text or text == '-' or text == '—' or text == 'N/A':
return None
# 尝试直接解析纯数字字符串
try:
return float(text)
except ValueError:
pass
# 优先截取 ":" 到 "元" 之间的价格,避免误提取 MA5/MA10 等技术指标数字
colon_pos = max(text.rfind(":"), text.rfind(":"))
yuan_pos = text.find("元", colon_pos + 1 if colon_pos != -1 else 0)
if yuan_pos != -1:
segment_start = colon_pos + 1 if colon_pos != -1 else 0
segment = text[segment_start:yuan_pos]
# 使用 finditer 并过滤掉 MA 开头的数字
matches = list(re.finditer(r"-?\d+(?:\.\d+)?", segment))
valid_numbers = []
for m in matches:
# 检查前面是否是 "MA" (忽略大小写)
start_idx = m.start()
if start_idx >= 2:
prefix = segment[start_idx-2:start_idx].upper()
if prefix == "MA":
continue
valid_numbers.append(m.group())
if valid_numbers:
try:
return abs(float(valid_numbers[-1]))
except ValueError:
pass
# 兜底:无"元"字时,先截去第一个括号后的内容,避免误提取括号内技术指标数字
# 例如 "1.52-1.53 (回踩MA5/10附近)" → 仅在 "1.52-1.53 " 中搜索
paren_pos = len(text)
for paren_char in ('(', '('):
pos = text.find(paren_char)
if pos != -1:
paren_pos = min(paren_pos, pos)
search_text = text[:paren_pos].strip() or text # 括号前为空时降级用全文
valid_numbers = []
for m in re.finditer(r"\d+(?:\.\d+)?", search_text):
start_idx = m.start()
if start_idx >= 2 and search_text[start_idx-2:start_idx].upper() == "MA":
continue
valid_numbers.append(m.group())
if valid_numbers:
try:
return float(valid_numbers[-1])
except ValueError:
pass
return None
def _extract_sniper_points(self, result: Any) -> Dict[str, Optional[float]]:
"""
Extract sniper point values from an AnalysisResult.
Tries multiple extraction paths to handle different dashboard structures:
1. result.get_sniper_points() (standard path)
2. Direct dashboard dict traversal with various nesting levels
3. Fallback from raw_result dict if available
"""
raw_points = {}
# Path 1: standard method
if hasattr(result, "get_sniper_points"):
raw_points = result.get_sniper_points() or {}
# Path 2: direct dashboard traversal when standard path yields empty values
if not any(raw_points.get(k) for k in ("ideal_buy", "secondary_buy", "stop_loss", "take_profit")):
dashboard = getattr(result, "dashboard", None)
if isinstance(dashboard, dict):
raw_points = self._find_sniper_in_dashboard(dashboard) or raw_points
# Path 3: try raw_result for agent mode results
if not any(raw_points.get(k) for k in ("ideal_buy", "secondary_buy", "stop_loss", "take_profit")):
raw_response = getattr(result, "raw_response", None)
if isinstance(raw_response, dict):
raw_points = self._find_sniper_in_dashboard(raw_response) or raw_points
return {
"ideal_buy": self._parse_sniper_value(raw_points.get("ideal_buy")),
"secondary_buy": self._parse_sniper_value(raw_points.get("secondary_buy")),
"stop_loss": self._parse_sniper_value(raw_points.get("stop_loss")),
"take_profit": self._parse_sniper_value(raw_points.get("take_profit")),
}
@staticmethod
def _find_sniper_in_dashboard(d: dict) -> Optional[Dict[str, Any]]:
"""
Recursively search for sniper_points in a dashboard dict.
Handles various nesting: dashboard.battle_plan.sniper_points,
dashboard.dashboard.battle_plan.sniper_points, etc.
"""
if not isinstance(d, dict):
return None
# Direct: d has sniper_points keys at top level
if "ideal_buy" in d:
return d
# d.sniper_points
sp = d.get("sniper_points")
if isinstance(sp, dict) and sp:
return sp
# d.battle_plan.sniper_points
bp = d.get("battle_plan")
if isinstance(bp, dict):
sp = bp.get("sniper_points")
if isinstance(sp, dict) and sp:
return sp
# d.dashboard.battle_plan.sniper_points (double-nested)
inner = d.get("dashboard")
if isinstance(inner, dict):
bp = inner.get("battle_plan")
if isinstance(bp, dict):
sp = bp.get("sniper_points")
if isinstance(sp, dict) and sp:
return sp
return None
@staticmethod
def _build_fallback_url_key(
code: str,
title: str,
source: str,
published_date: Optional[datetime]
) -> str:
"""
生成无 URL 时的去重键(确保稳定且较短)
"""
date_str = published_date.isoformat() if published_date else ""
raw_key = f"{code}|{title}|{source}|{date_str}"
digest = hashlib.md5(raw_key.encode("utf-8")).hexdigest()
return f"no-url:{code}:{digest}"
def save_conversation_message(self, session_id: str, role: str, content: str) -> None:
"""
保存 Agent 对话消息
"""
with self.session_scope() as session:
msg = ConversationMessage(
session_id=session_id,
role=role,
content=content
)
session.add(msg)
def get_conversation_history(self, session_id: str, limit: int = 20) -> List[Dict[str, Any]]:
"""
获取 Agent 对话历史
"""
with self.session_scope() as session:
stmt = select(ConversationMessage).filter(
ConversationMessage.session_id == session_id
).order_by(ConversationMessage.created_at.desc()).limit(limit)
messages = session.execute(stmt).scalars().all()
# 倒序返回,保证时间顺序
return [{"role": msg.role, "content": msg.content} for msg in reversed(messages)]
def conversation_session_exists(self, session_id: str) -> bool:
"""Return True when at least one message exists for the given session."""
with self.session_scope() as session:
stmt = (
select(ConversationMessage.id)
.where(ConversationMessage.session_id == session_id)
.limit(1)
)
return session.execute(stmt).scalar() is not None
def get_chat_sessions(
self,
limit: int = 50,
session_prefix: Optional[str] = None,
extra_session_ids: Optional[List[str]] = None,
) -> List[Dict[str, Any]]:
"""
获取聊天会话列表(从 conversation_messages 聚合)
Args:
limit: Maximum number of sessions to return.
session_prefix: If provided, only return sessions whose session_id
starts with this prefix. Used for per-user isolation (e.g.
``"telegram_12345"``).
extra_session_ids: Optional exact session ids to include in
addition to the scoped prefix.
Returns:
按最近活跃时间倒序的会话列表,每条包含 session_id, title, message_count, last_active
"""
from sqlalchemy import func
with self.session_scope() as session:
normalized_prefix = None
if session_prefix:
normalized_prefix = session_prefix if session_prefix.endswith(":") else f"{session_prefix}:"
exact_ids = [sid for sid in (extra_session_ids or []) if sid]
# 聚合每个 session 的消息数和最后活跃时间
base = (
select(
ConversationMessage.session_id,
func.count(ConversationMessage.id).label("message_count"),
func.min(ConversationMessage.created_at).label("created_at"),
func.max(ConversationMessage.created_at).label("last_active"),
)
)
conditions = []
if normalized_prefix:
conditions.append(ConversationMessage.session_id.startswith(normalized_prefix))
if exact_ids:
conditions.append(ConversationMessage.session_id.in_(exact_ids))
if conditions:
base = base.where(or_(*conditions))
stmt = (
base
.group_by(ConversationMessage.session_id)
.order_by(desc(func.max(ConversationMessage.created_at)))
.limit(limit)
)
rows = session.execute(stmt).all()
results = []
for row in rows:
sid = row.session_id
# 取该会话第一条 user 消息作为标题
first_user_msg = session.execute(
select(ConversationMessage.content)
.where(
and_(
ConversationMessage.session_id == sid,
ConversationMessage.role == "user",
)
)
.order_by(ConversationMessage.created_at)
.limit(1)
).scalar()
title = (first_user_msg or "新对话")[:60]
results.append({
"session_id": sid,
"title": title,
"message_count": row.message_count,
"created_at": row.created_at.isoformat() if row.created_at else None,
"last_active": row.last_active.isoformat() if row.last_active else None,
})
return results
def get_conversation_messages(self, session_id: str, limit: int = 100) -> List[Dict[str, Any]]:
"""
获取单个会话的完整消息列表(用于前端恢复历史)
"""
with self.session_scope() as session:
stmt = (
select(ConversationMessage)
.where(ConversationMessage.session_id == session_id)
.order_by(ConversationMessage.created_at)
.limit(limit)
)
messages = session.execute(stmt).scalars().all()
return [
{
"id": str(msg.id),
"role": msg.role,
"content": msg.content,
"created_at": msg.created_at.isoformat() if msg.created_at else None,
}
for msg in messages
]
def delete_conversation_session(self, session_id: str) -> int:
"""
删除指定会话的所有消息
Returns:
删除的消息数
"""
with self.session_scope() as session:
result = session.execute(
delete(ConversationMessage).where(
ConversationMessage.session_id == session_id
)
)
return result.rowcount
# ------------------------------------------------------------------
# LLM usage tracking
# ------------------------------------------------------------------
def record_llm_usage(
self,
call_type: str,
model: str,
prompt_tokens: int,
completion_tokens: int,
total_tokens: int,
stock_code: Optional[str] = None,
) -> None:
"""Append one LLM call record to llm_usage."""
row = LLMUsage(
call_type=call_type,
model=model or "unknown",
stock_code=stock_code,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=total_tokens,
)
with self.session_scope() as session:
session.add(row)
def get_llm_usage_summary(
self,
from_dt: datetime,
to_dt: datetime,
) -> Dict[str, Any]:
"""Return aggregated token usage between from_dt and to_dt.
Returns a dict with keys:
total_calls, total_tokens,
by_call_type: list of {call_type, calls, total_tokens},
by_model: list of {model, calls, total_tokens}
"""
with self.session_scope() as session:
base_filter = and_(
LLMUsage.called_at >= from_dt,
LLMUsage.called_at <= to_dt,
)
# Overall totals
totals = session.execute(
select(
func.count(LLMUsage.id).label("calls"),
func.coalesce(func.sum(LLMUsage.total_tokens), 0).label("tokens"),
).where(base_filter)
).one()
# Breakdown by call_type
by_type_rows = session.execute(
select(
LLMUsage.call_type,
func.count(LLMUsage.id).label("calls"),
func.coalesce(func.sum(LLMUsage.total_tokens), 0).label("tokens"),
)
.where(base_filter)
.group_by(LLMUsage.call_type)
.order_by(desc(func.sum(LLMUsage.total_tokens)))
).all()
# Breakdown by model
by_model_rows = session.execute(
select(
LLMUsage.model,
func.count(LLMUsage.id).label("calls"),
func.coalesce(func.sum(LLMUsage.total_tokens), 0).label("tokens"),
)
.where(base_filter)
.group_by(LLMUsage.model)
.order_by(desc(func.sum(LLMUsage.total_tokens)))
).all()
return {
"total_calls": totals.calls,
"total_tokens": totals.tokens,
"by_call_type": [
{"call_type": r.call_type, "calls": r.calls, "total_tokens": r.tokens}
for r in by_type_rows
],
"by_model": [
{"model": r.model, "calls": r.calls, "total_tokens": r.tokens}
for r in by_model_rows
],
}
# 便捷函数
def get_db() -> DatabaseManager:
"""获取数据库管理器实例的快捷方式"""
return DatabaseManager.get_instance()
def persist_llm_usage(
usage: Dict[str, Any],
model: str,
call_type: str,
stock_code: Optional[str] = None,
) -> None:
"""Fire-and-forget: write one LLM call record to llm_usage. Never raises."""
try:
db = DatabaseManager.get_instance()
db.record_llm_usage(
call_type=call_type,
model=model,
prompt_tokens=usage.get("prompt_tokens", 0) or 0,
completion_tokens=usage.get("completion_tokens", 0) or 0,
total_tokens=usage.get("total_tokens", 0) or 0,
stock_code=stock_code,
)
except Exception as exc:
logging.getLogger(__name__).warning("[LLM usage] failed to persist usage record: %s", exc)
if __name__ == "__main__":
# 测试代码
logging.basicConfig(level=logging.DEBUG)
db = get_db()
print("=== 数据库测试 ===")
print(f"数据库初始化成功")
# 测试检查今日数据
has_data = db.has_today_data('600519')
print(f"茅台今日是否有数据: {has_data}")
# 测试保存数据
test_df = pd.DataFrame({
'date': [date.today()],
'open': [1800.0],
'high': [1850.0],
'low': [1780.0],
'close': [1820.0],
'volume': [10000000],
'amount': [18200000000],
'pct_chg': [1.5],
'ma5': [1810.0],
'ma10': [1800.0],
'ma20': [1790.0],
'volume_ratio': [1.2],
})
saved = db.save_daily_data(test_df, '600519', 'TestSource')
print(f"保存测试数据: {saved} 条")
# 测试获取上下文
context = db.get_analysis_context('600519')
print(f"分析上下文: {context}")
|