1.自动更新今日需要抓取数据
2.工况接口返回
This commit is contained in:
159
app/utils/construction_monitor.py
Normal file
159
app/utils/construction_monitor.py
Normal file
@@ -0,0 +1,159 @@
|
||||
from datetime import datetime
|
||||
from typing import List, Dict
|
||||
import warnings
|
||||
import copy
|
||||
|
||||
class ConstructionMonitorUtils:
|
||||
def __init__(self):
|
||||
# 原始工况周期映射表(保持不变)
|
||||
self.base_periods = {
|
||||
"仰拱(底板)施工完成后,第1个月": 7,
|
||||
"仰拱(底板)施工完成后,第2至3个月": 14,
|
||||
"仰拱(底板)施工完成后,3个月以后": 30,
|
||||
"无砟轨道铺设后,第1至3个月": 30,
|
||||
"无砟轨道铺设后,4至12个月": 90,
|
||||
"无砟轨道铺设后,12个月以后": 180,
|
||||
"墩台施工到一定高度": 30,
|
||||
"墩台混凝土施工": 30,
|
||||
"预制梁桥,架梁前": 30,
|
||||
"预制梁桥,预制梁架设前": 1,
|
||||
"预制梁桥,预制梁架设后": 7,
|
||||
"桥位施工桥梁,制梁前": 30,
|
||||
"桥位施工桥梁,上部结构施工中": 1,
|
||||
"架桥机(运梁车)通过": 7,
|
||||
"桥梁主体工程完工后,第1至3个月": 7,
|
||||
"桥梁主体工程完工后,第4至6个月": 14,
|
||||
"桥梁主体工程完工后,6个月以后": 30,
|
||||
"轨道铺设期间,前": 30,
|
||||
"轨道铺设期间,后": 14,
|
||||
"轨道铺设完成后,第1个月": 14,
|
||||
"轨道铺设完成后,2至3个月": 30,
|
||||
"轨道铺设完成后,4至12个月": 90,
|
||||
"轨道铺设完成后,12个月以后": 180,
|
||||
"填筑或堆载,一般情况": 1,
|
||||
"填筑或堆载,沉降量突变情况": 1,
|
||||
"填筑或堆载,两次填筑间隔时间较长情况": 3,
|
||||
"堆载预压或路基填筑完成,第1至3个月": 7,
|
||||
"堆载预压或路基填筑完成,第4至6个月": 14,
|
||||
"堆载预压或路基填筑完成,6个月以后": 30,
|
||||
"架桥机(运梁车)首次通过前": 1,
|
||||
"架桥机(运梁车)首次通过后,前3天": 1,
|
||||
"架桥机(运梁车)首次通过后": 7,
|
||||
"轨道板(道床)铺设后,第1个月": 14,
|
||||
"轨道板(道床)铺设后,第2至3个月": 30,
|
||||
"轨道板(道床)铺设后,3个月以后": 90
|
||||
}
|
||||
# 构建中英文括号兼容映射表
|
||||
self.compatible_periods = self._build_compatible_brackets_map()
|
||||
|
||||
def _build_compatible_brackets_map(self) -> Dict[str, int]:
|
||||
"""构建支持中英文括号的兼容映射表"""
|
||||
compatible_map = {}
|
||||
for original_key, period in self.base_periods.items():
|
||||
compatible_map[original_key] = period
|
||||
# 生成中文括号版key并添加到映射表
|
||||
chinese_bracket_key = original_key.replace("(", "(").replace(")", ")")
|
||||
if chinese_bracket_key != original_key:
|
||||
compatible_map[chinese_bracket_key] = period
|
||||
return compatible_map
|
||||
|
||||
def get_due_data(self, input_data: List[List[Dict]], start: int = 0, end: int = 1, current_date: datetime = None) -> Dict[str, List[Dict]]:
|
||||
result = {"winter": [], "data": [], "error_data": []}
|
||||
if not input_data:
|
||||
return result
|
||||
|
||||
calc_date = current_date.date() if current_date else datetime.now().date()
|
||||
|
||||
# ------------------------------
|
||||
# 原有核心逻辑(完全不变)
|
||||
# ------------------------------
|
||||
for point_idx, point_data in enumerate(input_data):
|
||||
if not point_data:
|
||||
continue
|
||||
|
||||
latest_item = point_data[0]
|
||||
latest_condition = latest_item.get("workinfoname")
|
||||
if not latest_condition:
|
||||
result["error_data"].append(latest_item)
|
||||
warnings.warn(f"【数据错误】测点{point_idx}的最新数据缺少'workinfoname'字段", UserWarning)
|
||||
continue
|
||||
|
||||
base_condition = None
|
||||
if latest_condition != "冬休":
|
||||
if latest_condition not in self.compatible_periods:
|
||||
result["error_data"].append(latest_item)
|
||||
warnings.warn(f"【数据错误】测点{point_idx}最新数据存在未定义工况: {latest_condition}", UserWarning)
|
||||
continue
|
||||
base_condition = latest_condition
|
||||
else:
|
||||
for history_item in point_data[1:]:
|
||||
history_condition = history_item.get("workinfoname")
|
||||
if not history_condition:
|
||||
result["error_data"].append(history_item)
|
||||
warnings.warn(f"【数据错误】测点{point_idx}的历史数据缺少'workinfoname'字段", UserWarning)
|
||||
continue
|
||||
if history_condition != "冬休":
|
||||
if history_condition not in self.compatible_periods:
|
||||
result["error_data"].append(history_item)
|
||||
warnings.warn(f"【数据错误】测点{point_idx}历史数据存在未定义工况: {history_condition}", UserWarning)
|
||||
base_condition = None
|
||||
break
|
||||
base_condition = history_condition
|
||||
break
|
||||
|
||||
item_copy = copy.deepcopy(latest_item)
|
||||
create_date_val = latest_item.get("createdate")
|
||||
if not create_date_val:
|
||||
result["error_data"].append(item_copy)
|
||||
warnings.warn(f"【数据错误】测点{point_idx}的最新数据缺少'createdate'字段", UserWarning)
|
||||
continue
|
||||
|
||||
try:
|
||||
if isinstance(create_date_val, datetime):
|
||||
create_date = create_date_val.date()
|
||||
else:
|
||||
create_date = datetime.strptime(create_date_val, "%Y-%m-%d %H:%M:%S").date()
|
||||
except ValueError as e:
|
||||
result["error_data"].append(item_copy)
|
||||
warnings.warn(f"【数据错误】测点{point_idx}最新数据的日期格式错误:{create_date_val},错误:{str(e)}", UserWarning)
|
||||
continue
|
||||
|
||||
if not base_condition:
|
||||
result["winter"].append(item_copy)
|
||||
continue
|
||||
|
||||
period = self.compatible_periods[base_condition]
|
||||
days_passed = (calc_date - create_date).days
|
||||
due_days = period - days_passed
|
||||
|
||||
if due_days < 0:
|
||||
item_copy["overdue"] = abs(due_days)
|
||||
result["error_data"].append(item_copy)
|
||||
warn_msg = (
|
||||
f"【超期警报】测点{point_idx} 最新工况'{latest_condition}'({create_date})"
|
||||
f"已超期{abs(due_days)}天!基准工况:{base_condition},周期{period}天"
|
||||
)
|
||||
warnings.warn(warn_msg, UserWarning)
|
||||
elif start <= due_days <= end:
|
||||
item_copy["remaining"] = due_days
|
||||
result["data"].append(item_copy)
|
||||
|
||||
# ------------------------------
|
||||
# 新增步骤:data中相同NYID保留剩余天数最少的记录
|
||||
# ------------------------------
|
||||
if result["data"]:
|
||||
# 用字典临时存储:key=NYID,value=该NYID下剩余天数最少的记录
|
||||
nyid_min_remaining = {}
|
||||
for record in result["data"]:
|
||||
nyid = record.get("NYID") # 假设NYID字段名是"NYID",若实际不同需调整
|
||||
if not nyid:
|
||||
continue # 无NYID的记录直接保留(或按需求处理)
|
||||
|
||||
# 若该NYID未存储,或当前记录剩余天数更少,则更新
|
||||
if nyid not in nyid_min_remaining or record["remaining"] < nyid_min_remaining[nyid]["remaining"]:
|
||||
nyid_min_remaining[nyid] = record
|
||||
|
||||
# 将字典 values 转换为列表,作为去重后的data
|
||||
result["data"] = list(nyid_min_remaining.values())
|
||||
|
||||
return result
|
||||
@@ -3,9 +3,20 @@ from apscheduler.executors.pool import ThreadPoolExecutor
|
||||
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
|
||||
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
|
||||
from ..core.config import settings
|
||||
from sqlalchemy.orm import Session
|
||||
from ..core.database import SessionLocal
|
||||
from ..core.logging_config import get_logger
|
||||
from ..models.account import Account
|
||||
# from ..services.daily import get_nyid_by_point_id
|
||||
from ..services.daily import DailyDataService
|
||||
from ..services.level_data import LevelDataService
|
||||
from ..services.checkpoint import CheckpointService
|
||||
from ..services.section_data import SectionDataService
|
||||
from ..services.account import AccountService
|
||||
from ..models.daily import DailyData
|
||||
from ..models.settlement_data import SettlementData
|
||||
from typing import List
|
||||
from ..utils.construction_monitor import ConstructionMonitorUtils
|
||||
|
||||
# 获取日志记录器
|
||||
logger = get_logger(__name__)
|
||||
@@ -70,6 +81,7 @@ class TaskScheduler:
|
||||
# 添加每天午夜12点重置today_updated字段的任务
|
||||
self.scheduler.add_job(
|
||||
reset_today_updated_task,
|
||||
scheduled_get_max_nyid_by_point_id,
|
||||
'cron',
|
||||
id='daily_reset_today_updated',
|
||||
hour=0,
|
||||
@@ -174,6 +186,7 @@ def reset_today_updated_task():
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# 示例定时任务函数
|
||||
def example_task():
|
||||
"""示例定时任务"""
|
||||
@@ -185,4 +198,106 @@ def database_cleanup_task():
|
||||
"""数据库清理任务示例"""
|
||||
logger.info("执行数据库清理任务")
|
||||
# 这里可以添加数据库清理逻辑
|
||||
return "数据库清理完成"
|
||||
return "数据库清理完成"
|
||||
|
||||
# 每日自动写入获取最新工况信息
|
||||
def scheduled_get_max_nyid_by_point_id():
|
||||
"""定时任务:获取max NYID关联数据并批量创建DailyData记录"""
|
||||
db: Session = None
|
||||
try:
|
||||
# 初始化数据库会话(替代接口的Depends依赖)
|
||||
db = SessionLocal()
|
||||
logger.info("定时任务开始执行:获取max NYID关联数据并处理")
|
||||
# 核心新增:清空DailyData表所有数据
|
||||
delete_count = db.query(DailyData).delete()
|
||||
db.commit()
|
||||
logger.info(f"DailyData表清空完成,共删除{delete_count}条历史记录")
|
||||
|
||||
# 1. 获取沉降数据(返回 List[List[dict]])
|
||||
daily_service = DailyDataService()
|
||||
result = daily_service.get_nyid_by_point_id(db, [], 1)
|
||||
|
||||
# 2. 计算到期数据
|
||||
monitor = ConstructionMonitorUtils()
|
||||
daily_data = monitor.get_due_data(result)
|
||||
data = daily_data['data']
|
||||
error_data = daily_data['error_data']
|
||||
winters = daily_data['winter']
|
||||
logger.info(f"首次获取数据完成,共{len(result)}条记录")
|
||||
|
||||
# 3. 循环处理冬休数据,追溯历史非冬休记录
|
||||
max_num = 1
|
||||
while winters:
|
||||
max_num += 1
|
||||
# 提取冬休数据的point_id列表
|
||||
new_list = [w['point_id'] for w in winters]
|
||||
# 获取更多历史记录
|
||||
nyid_list = daily_service.get_nyid_by_point_id(db, new_list, max_num)
|
||||
w_list = monitor.get_due_data(nyid_list)
|
||||
# 更新冬休、待处理、错误数据
|
||||
winters = w_list['winter']
|
||||
data.extend(w_list['data'])
|
||||
error_data.extend(w_list['error_data'])
|
||||
|
||||
# 4. 初始化服务实例
|
||||
level_service = LevelDataService()
|
||||
checkpoint_db = CheckpointService()
|
||||
section_db = SectionDataService()
|
||||
account_service = AccountService()
|
||||
|
||||
# 5. 关联其他表数据(核心逻辑保留)
|
||||
for d in data:
|
||||
# 处理 LevelData(假设返回列表,取第一条)
|
||||
level_results = level_service.get_by_nyid(db, d['NYID'])
|
||||
level_instance = level_results[0] if isinstance(level_results, list) and level_results else level_results
|
||||
d['level_data'] = level_instance.to_dict() if level_instance else None
|
||||
|
||||
# 处理 CheckpointData(返回单实例,直接使用)
|
||||
checkpoint_instance = checkpoint_db.get_by_point_id(db, d['point_id'])
|
||||
d['checkpoint_data'] = checkpoint_instance.to_dict() if checkpoint_instance else None
|
||||
|
||||
# 处理 SectionData(根据checkpoint_data关联)
|
||||
if d['checkpoint_data']:
|
||||
section_instance = section_db.get_by_section_id(db, d['checkpoint_data']['section_id'])
|
||||
d['section_data'] = section_instance.to_dict() if section_instance else None
|
||||
else:
|
||||
d['section_data'] = None
|
||||
|
||||
# 处理 AccountData
|
||||
if d.get('section_data') and d['section_data'].get('account_id'):
|
||||
account_response = account_service.get_account(db, account_id=d['section_data']['account_id'])
|
||||
d['account_data'] = account_response.__dict__ if account_response else None
|
||||
else:
|
||||
d['account_data'] = None
|
||||
|
||||
# 6. 构造DailyData数据并批量创建
|
||||
daily_create_data = []
|
||||
for d in data:
|
||||
# 过滤无效数据(避免缺失关键字段报错)
|
||||
if all(key in d for key in ['NYID', 'point_id']) and d.get('level_data') and d.get('account_data') and d.get('section_data'):
|
||||
tem = {
|
||||
'NYID': d['NYID'],
|
||||
'point_id': d['point_id'],
|
||||
'linecode': d['level_data']['linecode'],
|
||||
'account_id': d['account_data']['account_id'],
|
||||
'section_id': d['section_data']['id']
|
||||
}
|
||||
daily_create_data.append(tem)
|
||||
|
||||
# 批量创建记录
|
||||
if daily_create_data:
|
||||
created_records = daily_service.batch_create_by_account_nyid(db, daily_create_data)
|
||||
logger.info(f"定时任务完成:成功创建{len(created_records)}条DailyData记录,共处理{len(data)}个point_id数据")
|
||||
else:
|
||||
logger.warning("定时任务完成:无有效数据可创建DailyData记录")
|
||||
|
||||
except Exception as e:
|
||||
# 异常时回滚事务
|
||||
if db:
|
||||
db.rollback()
|
||||
logger.error(f"定时任务执行失败:{str(e)}", exc_info=True)
|
||||
raise e # 抛出异常便于定时框架捕获告警
|
||||
finally:
|
||||
# 确保数据库会话关闭
|
||||
if db:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user