Commit 9b8ec001 by chenxi

零跑、岚图报表首次提交

1 parent a31f8f12
...@@ -10,3 +10,9 @@ ...@@ -10,3 +10,9 @@
/data-process/backend_avita9.log /data-process/backend_avita9.log
/data-process/backend_avita10.log /data-process/backend_avita10.log
/data-process/*.log /data-process/*.log
# VSCode Local History 插件
.history/
# IDE
.idea/
...@@ -4,3 +4,4 @@ __pycache__/ ...@@ -4,3 +4,4 @@ __pycache__/
*.pyc *.pyc
uploads/ uploads/
data/ data/
.venv/
\ No newline at end of file \ No newline at end of file
...@@ -33,6 +33,8 @@ from scheduler import ( ...@@ -33,6 +33,8 @@ from scheduler import (
OutputValueScheduler, OutputValueScheduler,
StoreEffectSummaryScheduler, StoreEffectSummaryScheduler,
AvitaSpecialScheduler, AvitaSpecialScheduler,
VoyahScheduler,
LeapmotorScheduler,
) )
app = Flask(__name__, static_folder='static', static_url_path='') app = Flask(__name__, static_folder='static', static_url_path='')
...@@ -653,7 +655,6 @@ light_reform_inshop_scheduler = LightReformInshopScheduler(app=app) ...@@ -653,7 +655,6 @@ light_reform_inshop_scheduler = LightReformInshopScheduler(app=app)
light_reform_park_scheduler = LightReformParkScheduler(app=app) light_reform_park_scheduler = LightReformParkScheduler(app=app)
output_value_scheduler = OutputValueScheduler(app=app) output_value_scheduler = OutputValueScheduler(app=app)
store_effect_summary_scheduler = StoreEffectSummaryScheduler(app=app) store_effect_summary_scheduler = StoreEffectSummaryScheduler(app=app)
avita_special_scheduler = AvitaSpecialScheduler(app=app)
ALL_SCHEDULERS = [ ALL_SCHEDULERS = [
('budget_achievement', scheduler), ('budget_achievement', scheduler),
...@@ -663,7 +664,6 @@ ALL_SCHEDULERS = [ ...@@ -663,7 +664,6 @@ ALL_SCHEDULERS = [
('light_reform_park', light_reform_park_scheduler), ('light_reform_park', light_reform_park_scheduler),
('output_value', output_value_scheduler), ('output_value', output_value_scheduler),
('store_effect_summary', store_effect_summary_scheduler), ('store_effect_summary', store_effect_summary_scheduler),
('avita_special', avita_special_scheduler),
] ]
......
...@@ -168,3 +168,85 @@ class IndicatorClient: ...@@ -168,3 +168,85 @@ class IndicatorClient:
seen.add(c) seen.add(c)
unique.append(c) unique.append(c)
return self.fetch(start_date, end_date, biz, unique) return self.fetch(start_date, end_date, biz, unique)
# ---------- 按商户 ID 列表查询(岚图/零跑专项等按 merchant_id 匹配的场景)----------
def fetch_by_merchant_ids(self, start_date, end_date, item_codes, merchant_ids, biz=1):
"""
查询指定商户列表在指定周期的指标数据。
与 fetch() 的区别:
- payload 显式携带 merchantIds(多个英文逗号分隔)+ exts=merchantIds + bizs;
- 返回按 merchant_id(而非 merchantName)聚合,便于用商户号直接取值,
避免门店名匹配的歧义。
参数:
- start_date / end_date: str 'YYYY-MM-DD'
- item_codes: list[str],如 ['A075', 'A084']
- merchant_ids: list[str|int],商户 ID 列表(会序列化为英文逗号分隔)
- biz: int,业务线(1=延保),payload 键为 bizs
返回:
dict[merchant_id(str)][item_code] = Decimal
注意粒度为「商户 × 指标」:同一请求传多个 itemCodes 时,每个商户
每个指标各一条数据,本方法已按 (merchant_id, item_code) 建好双层索引。
空 dict 表示查询失败或无数据。
"""
if not item_codes or not merchant_ids:
return {}
payload = {
"dateType": 4,
"startDate": start_date,
"endDate": end_date,
"exts": "merchantIds",
"itemCodes": ",".join(item_codes),
"merchantIds": ",".join(str(m) for m in merchant_ids),
"bizs": 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_by_merchant_id(content)
last_err = content
logger.warning(f"[IndicatorAPI] 第 {attempt} 次失败 err={last_err}")
if attempt <= self.retry:
time.sleep(1 * attempt)
logger.error(f"[指标系统] 按商户查询最终失败: {last_err}")
return {}
@staticmethod
def _aggregate_by_merchant_id(content):
"""
将 API 返回的扁平列表按 merchant_id + itemCode 聚合成双层字典。
输入(商户 × 指标 粒度,每商户每 itemCode 各一条):
content = [{"itemCode":"A075","merchantId":"1001","itemValue":216,...},
{"itemCode":"A084","merchantId":"1001","itemValue":480,...}, ...]
输出:
{"1001": {"A075": Decimal("216"), "A084": Decimal("480")}, ...}
"""
result = {}
for row in content or []:
code = row.get('itemCode')
mid = row.get('merchantId') or row.get('merchant_id')
val = row.get('itemValue')
if not code or not mid or val is None:
continue
try:
val_dec = Decimal(str(val))
except Exception:
val_dec = float(val)
merchant_dict = result.setdefault(str(mid), {})
# 同一 merchant_id + itemCode 出现多条(异常情况)取第一条
merchant_dict.setdefault(code, val_dec)
logger.info(f"[指标系统] 按商户聚合完成:{len(result)} 个商户, "
f"{len(content or [])} 条原始数据")
return result
...@@ -1441,6 +1441,18 @@ class DataProcessor: ...@@ -1441,6 +1441,18 @@ class DataProcessor:
AvitaSpecialProcessor(app=self.app).process(task) AvitaSpecialProcessor(app=self.app).process(task)
return return
if getattr(task, 'data_table_type', None) == 'voyah_special':
from voyah_processor import VoyahProcessor
logger.info(f"[处理任务] 路由 voyah_special 任务 {task_id} 至 VoyahProcessor")
VoyahProcessor(app=self.app).process(task)
return
if getattr(task, 'data_table_type', None) == 'leapmotor_special':
from leapmotor_processor import LeapmotorProcessor
logger.info(f"[处理任务] 路由 leapmotor_special 任务 {task_id} 至 LeapmotorProcessor")
LeapmotorProcessor(app=self.app).process(task)
return
# 类型白名单保护:当前引擎仅支持 SUPPORTED_PROCESS_TYPES # 类型白名单保护:当前引擎仅支持 SUPPORTED_PROCESS_TYPES
if getattr(task, 'data_table_type', None) not in SUPPORTED_PROCESS_TYPES: if getattr(task, 'data_table_type', None) not in SUPPORTED_PROCESS_TYPES:
raise Exception( raise Exception(
......
...@@ -429,3 +429,69 @@ class AvitaSpecialScheduler(NewReportScheduler): ...@@ -429,3 +429,69 @@ class AvitaSpecialScheduler(NewReportScheduler):
finally: finally:
self._is_processing = False self._is_processing = False
self._lock.release() self._lock.release()
class VoyahScheduler(NewReportScheduler):
"""岚图专项报表独立调度器(扫描 voyah_special 类型任务)"""
OUTPUT_CATEGORY = 'voyah_special'
JOB_NAME = 'voyah_special_scan'
def _execute_scan(self):
if not self._lock.acquire(blocking=False):
logger.debug(f"[{self.JOB_NAME}] 上一轮处理尚未完成,跳过本轮扫描")
return
self._is_processing = True
self._last_run_time = datetime.now()
try:
logger.info(f"[{self.JOB_NAME}] 开始扫描 voyah_special 待处理任务")
with self.app.app_context():
from voyah_processor import VoyahProcessor
processor = VoyahProcessor(app=self.app)
result = processor.scan_voyah()
self._last_run_result = result
logger.info(f"[{self.JOB_NAME}] 扫描完成:成功 {result.get('success', 0)} 个,"
f"失败 {result.get('failed', 0)} 个")
except Exception as e:
logger.error(f"[{self.JOB_NAME}] 扫描执行异常: {str(e)}")
self._last_run_result = {'error': str(e)}
finally:
self._is_processing = False
self._lock.release()
class LeapmotorScheduler(NewReportScheduler):
"""零跑专项报表独立调度器(扫描 leapmotor_special 类型任务)"""
OUTPUT_CATEGORY = 'leapmotor_special'
JOB_NAME = 'leapmotor_special_scan'
def _execute_scan(self):
if not self._lock.acquire(blocking=False):
logger.debug(f"[{self.JOB_NAME}] 上一轮处理尚未完成,跳过本轮扫描")
return
self._is_processing = True
self._last_run_time = datetime.now()
try:
logger.info(f"[{self.JOB_NAME}] 开始扫描 leapmotor_special 待处理任务")
with self.app.app_context():
from leapmotor_processor import LeapmotorProcessor
processor = LeapmotorProcessor(app=self.app)
result = processor.scan_leapmotor()
self._last_run_result = result
logger.info(f"[{self.JOB_NAME}] 扫描完成:成功 {result.get('success', 0)} 个,"
f"失败 {result.get('failed', 0)} 个")
except Exception as e:
logger.error(f"[{self.JOB_NAME}] 扫描执行异常: {str(e)}")
self._last_run_result = {'error': str(e)}
finally:
self._is_processing = False
self._lock.release()
\ No newline at end of file \ No newline at end of file
No preview for this file type
No preview for this file type
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!