Commit 50e877a4 by zhouhaibao

feat(data-report): 添加数据报告模块的日志记录功能

- 在 indicator_client.py 中增加请求参数和重试成功的日志记录
- 在 report_utils.py 的 fetch 方法中添加数据请求的日志记录
- 在 BizSQL 相关方法中添加 SQL 执行预览和执行日志
- 实现延保权益财务数据查询的诊断功能,包括零结果检测和模糊匹配
- 添加 SQL 执行前的预览日志,便于调试和监控
1 parent 9d832189
......@@ -90,12 +90,16 @@ class IndicatorClient:
payload["biz"] = biz
last_err = None
# 请求参数日志
logger.info(f"[IndicatorAPI] POST {self.url} payload={payload}")
for attempt in range(1, self.retry + 2): # 首次 + retry 次
ok, content = self._post(payload)
if ok:
if attempt > 1:
logger.info(f"[IndicatorAPI] 第 {attempt} 次重试成功")
return self._aggregate(content, rate_set)
last_err = content
logger.warning(f"[指标系统] 第 {attempt} 次请求失败: {last_err}")
logger.warning(f"[IndicatorAPI] 第 {attempt} 次失败 err={last_err}")
if attempt <= self.retry:
time.sleep(1 * attempt)
......
......@@ -443,6 +443,7 @@ def fetch_indicator_data(start_str, end_str, biz_code, item_codes):
"""
if not start_str or not end_str or not item_codes:
return {}
logger.info(f"[IndicatorAPI-fetch] dates={start_str}~{end_str} biz_code={biz_code} item_codes={item_codes}")
client = IndicatorClient()
return client.fetch(start_str, end_str, biz_code, item_codes)
......@@ -1323,6 +1324,8 @@ def fetch_biz_sql_aggregated(start_date, end_date, biz_type, store_names=None):
try:
from sqlalchemy import text as sa_text
_sql_preview = sql.strip().replace('\n', ' ')[:500]
logger.info(f"[BizSQL-fetch_biz_sql_aggregated] biz_type={biz_type} dates={start_str}~{end_str} scene_type={scene_type} business_type={business_type} store_cnt={len(store_names) if store_names else 'all'} SQL={_sql_preview}...")
result = conn.execute(sa_text(sql))
rows = result.mappings().all()
out = {}
......@@ -1339,6 +1342,38 @@ def fetch_biz_sql_aggregated(start_date, end_date, biz_type, store_names=None):
'other_cost': Decimal(str(r['other_cost'] or Decimal('0'))).quantize(Decimal('0.01'), ROUND_HALF_UP),
}
logger.info(f"[BizSQL-延保权益财务] biz_type={biz_type} 聚合完成, {len(out)} 个门店")
# === 诊断:带 store_names 过滤返回 0 时,自动再跑一次不带过滤的全量 ===
if store_names and len(out) == 0:
logger.warning(f"[BizSQL-延保权益财务] store_cnt={len(store_names)} 过滤后返回 0,诊断全量...")
# 构造不带 store_names 的 SQL
diag_sql = sql.split("WHERE")[0] + "WHERE {' AND '.join([w for w in where_parts if 'LIKE' not in w])} GROUP BY go.merchant_name"
try:
_diag_preview = diag_sql.strip().replace('\n', ' ')[:400]
logger.info(f"[BizSQL-诊断] 全量 SQL={_diag_preview}...")
diag_rows = conn.execute(sa_text(diag_sql)).mappings().all()
logger.info(f"[BizSQL-诊断] 全量返回 {len(diag_rows)} 个门店")
if diag_rows:
diag_names = [r['merchant_name'] for r in diag_rows[:15]]
logger.info(f"[BizSQL-诊断] merchant_name 样本: {diag_names}")
# 比较 store_names 和 merchant_name 的交集
store_set = set(store_names)
name_set = {r['merchant_name'] for r in diag_rows}
exact_match = store_set & name_set
logger.info(f"[BizSQL-诊断] Excel store_names={len(store_set)}, DB merchant_name={len(name_set)}, 精确匹配={len(exact_match)}")
if exact_match:
logger.info(f"[BizSQL-诊断] 精确匹配样本: {list(exact_match)[:5]}")
# 模糊包含检查
fuzzy_hits = []
for sn in store_set:
for mn in name_set:
if sn and mn and (sn in mn or mn in sn):
fuzzy_hits.append((sn, mn))
break
logger.info(f"[BizSQL-诊断] 模糊包含匹配={len(fuzzy_hits)}")
if fuzzy_hits:
logger.info(f"[BizSQL-诊断] 模糊匹配样本: {fuzzy_hits[:5]}")
except Exception as de:
logger.error(f"[BizSQL-诊断] 全量查询也失败: {de}")
return out
except Exception as e:
logger.error(f"[BizSQL-延保权益财务] 查询异常: {e}")
......@@ -1468,12 +1503,16 @@ def fetch_park_daily_report(start_date, end_date, scene_type=1):
try:
from sqlalchemy import text as sa_text
_sql_preview = sql_delivery.strip().replace('\n', ' ')[:500]
logger.info(f"[BizSQL-fetch_park_daily_report] start_date={start_date}, end_date={end_date}, scene_type={scene_type} SQL={_sql_preview}...")
for r in conn.execute(sa_text(sql_delivery)).mappings().all():
mn = r['merchant_name']
if mn:
out[mn] = out.get(mn, {})
out[mn]['delivery_cnt'] = int(r['delivery_cnt'] or 0)
_sql_preview = sql_arrival.strip().replace('\n', ' ')[:500]
logger.info(f"[BizSQL-fetch_park_daily_report] start_date={start_date}, end_date={end_date}, scene_type={scene_type} SQL={_sql_preview}...")
for r in conn.execute(sa_text(sql_arrival)).mappings().all():
mn = r['merchant_name']
if mn:
......@@ -1642,6 +1681,8 @@ def fetch_light_reform_goods_stats(start_date, end_date):
from sqlalchemy import text as sa_text
from decimal import Decimal
_sql_preview = sql.strip().replace('\n', ' ')[:500]
logger.info(f"[BizSQL-fetch_light_reform_goods_stats] start_date={start_date}, end_date={end_date} SQL={_sql_preview}...")
for r in conn.execute(sa_text(sql)).mappings().all():
mn = r['merchant_name']
if not mn:
......@@ -1709,6 +1750,8 @@ def fetch_est_delivery_count(base_scene_code='100101'):
try:
from sqlalchemy import text as sa_text
_sql_preview = sql.strip().replace('\n', ' ')[:500]
logger.info(f"[BizSQL-fetch_est_delivery_count] base_scene_code={base_scene_code} SQL={_sql_preview}...")
for r in conn.execute(sa_text(sql)).mappings().all():
tn = r['traffic_party_name']
# ORDER BY created_date DESC → 同门店多条时只取最新一条(先遍历到的就是最新)
......@@ -1869,6 +1912,9 @@ def fetch_light_reform_finance_stats(start_date, end_date, scene_type=1):
try:
from sqlalchemy import text as sa_text
_sql_preview = sql.strip().replace('\n', ' ')[:500]
logger.info(f"[BizSQL-fetch_light_reform_finance_stats] start_date={start_date}, end_date={end_date}, scene_type={scene_type} SQL={_sql_preview}...")
result = conn.execute(sa_text(sql))
rows = result.mappings().all()
out = {}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!