fix:优化参数指数
This commit is contained in:
@@ -14,6 +14,13 @@ from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from crawler.indicator_smooth import (
|
||||
clamp as _indicator_clamp,
|
||||
smooth_retaliation as _smooth_retaliation,
|
||||
smooth_wall_street as _smooth_wall_street,
|
||||
wall_street_should_append as _wall_street_should_append,
|
||||
)
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
DB_PATH = os.environ.get("DB_PATH", str(PROJECT_ROOT / "server" / "data.db"))
|
||||
|
||||
@@ -26,8 +33,7 @@ MAX_DELTA_PER_MERGE = {
|
||||
"civilian_ships": 20, "airport_port": 10,
|
||||
}
|
||||
|
||||
# 反击情绪 / 华尔街:合理区间,避免爬虫单条提取 0 或 100 导致指标归零或打满
|
||||
RETALIATION_SMOOTH_WEIGHT = 0.6 # 当前值权重,1 - 此值为新值权重,使更新平滑
|
||||
# 反击情绪 / 华尔街:限幅与平滑见 crawler.indicator_smooth
|
||||
RETALIATION_HISTORY_MAX_ROWS = 300 # 反击历史条数上限,供前端曲线与回放使用
|
||||
WALL_STREET_TREND_MAX_ROWS = 200 # 趋势表保留最近条数,避免无限增长
|
||||
VALUE_CLAMP_MIN, VALUE_CLAMP_MAX = 1, 99 # 0/100 视为异常,写入前夹在 [1,99]
|
||||
@@ -206,17 +212,13 @@ def merge(extracted: Dict[str, Any], db_path: Optional[str] = None) -> bool:
|
||||
updated = True
|
||||
except Exception:
|
||||
pass
|
||||
# retaliation:平滑更新,避免单条新闻 0/100 导致指标归零或打满
|
||||
# retaliation:由 indicator_smooth 计算平滑值 + 单步变化上限,避免爬虫连续更新导致剧烈波动
|
||||
if "retaliation" in extracted:
|
||||
r = extracted["retaliation"]
|
||||
raw = max(VALUE_CLAMP_MIN, min(VALUE_CLAMP_MAX, int(r.get("value", 50))))
|
||||
raw = _indicator_clamp(int(r.get("value", 50)))
|
||||
row = conn.execute("SELECT value FROM retaliation_current WHERE id = 1").fetchone()
|
||||
current = int(row[0]) if row else 50
|
||||
current = max(VALUE_CLAMP_MIN, min(VALUE_CLAMP_MAX, current))
|
||||
new_val = round(
|
||||
RETALIATION_SMOOTH_WEIGHT * current + (1 - RETALIATION_SMOOTH_WEIGHT) * raw
|
||||
)
|
||||
new_val = max(VALUE_CLAMP_MIN, min(VALUE_CLAMP_MAX, new_val))
|
||||
new_val = _smooth_retaliation(raw, current)
|
||||
ts = (r.get("time") or datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z"))[:25]
|
||||
conn.execute("INSERT OR REPLACE INTO retaliation_current (id, value) VALUES (1, ?)", (new_val,))
|
||||
conn.execute("INSERT INTO retaliation_history (time, value) VALUES (?, ?)", (ts, new_val))
|
||||
@@ -227,13 +229,19 @@ def merge(extracted: Dict[str, Any], db_path: Optional[str] = None) -> bool:
|
||||
(n_ret - RETALIATION_HISTORY_MAX_ROWS,),
|
||||
)
|
||||
updated = True
|
||||
# wall_street_trend:限幅后写入,并保留最近 N 条避免表无限增长
|
||||
# wall_street_trend:由 indicator_smooth 与上一点平滑 + 最小写入间隔,抑制密集报道导致的锯齿
|
||||
if "wall_street" in extracted:
|
||||
w = extracted["wall_street"]
|
||||
raw = int(w.get("value", 50))
|
||||
val = max(VALUE_CLAMP_MIN, min(VALUE_CLAMP_MAX, raw))
|
||||
ts = (w.get("time") or datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z"))[:25]
|
||||
conn.execute("INSERT INTO wall_street_trend (time, value) VALUES (?, ?)", (ts, val))
|
||||
last_row = conn.execute(
|
||||
"SELECT time, value FROM wall_street_trend ORDER BY time DESC LIMIT 1"
|
||||
).fetchone()
|
||||
last_time = last_row[0] if last_row else None
|
||||
last_val = int(last_row[1]) if last_row else None
|
||||
if _wall_street_should_append(last_time, ts):
|
||||
val = _smooth_wall_street(raw, last_val)
|
||||
conn.execute("INSERT INTO wall_street_trend (time, value) VALUES (?, ?)", (ts, val))
|
||||
n = conn.execute("SELECT COUNT(*) FROM wall_street_trend").fetchone()[0]
|
||||
if n > WALL_STREET_TREND_MAX_ROWS:
|
||||
conn.execute(
|
||||
|
||||
Reference in New Issue
Block a user