修改新增加文件数量的查询功能
This commit is contained in:
@ -255,6 +255,7 @@ def devices_overview():
|
||||
data_list = []
|
||||
|
||||
for d in devices:
|
||||
# 关键:d.to_dict() 在 models.py 中应包含 file_count
|
||||
item = d.to_dict()
|
||||
|
||||
# 强制格式化时间
|
||||
@ -422,6 +423,10 @@ def run_monitor():
|
||||
device.latest_time = target_time
|
||||
device.check_time = current_time
|
||||
|
||||
# ✅ [核心修改] 获取爬虫返回的文件数量并保存
|
||||
f_count = item.get('num_files', 0)
|
||||
device.file_count = f_count
|
||||
|
||||
old_json = {}
|
||||
try:
|
||||
if device.json_data:
|
||||
@ -436,12 +441,14 @@ def run_monitor():
|
||||
device.json_data = json.dumps(old_json, ensure_ascii=False)
|
||||
device.offset = calculate_offset(device.latest_time)
|
||||
|
||||
# ✅ [核心修改] 写入历史记录时包含 file_count
|
||||
new_history = DeviceHistory(
|
||||
device_id=device.id,
|
||||
status=item.get('status'),
|
||||
result_data=item.get('value'),
|
||||
data_time=target_time,
|
||||
json_data=device.json_data
|
||||
json_data=device.json_data,
|
||||
file_count=f_count # 确保历史数据也记录文件数
|
||||
)
|
||||
db.session.add(new_history)
|
||||
count_crawler += 1
|
||||
@ -636,4 +643,54 @@ def delete_log_entry():
|
||||
db.session.delete(log)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200})
|
||||
return jsonify({'code': 404})
|
||||
return jsonify({'code': 404})
|
||||
|
||||
|
||||
@api_bp.route('/device_history_list', methods=['GET'])
|
||||
def get_device_history_list():
|
||||
try:
|
||||
name = request.args.get('name')
|
||||
page = int(request.args.get('page', 1))
|
||||
limit = int(request.args.get('limit', 10))
|
||||
|
||||
if not name:
|
||||
return jsonify({'code': 400, 'message': '缺少设备名称'})
|
||||
|
||||
# 1. 找到设备ID
|
||||
device = Device.query.filter_by(name=name).first()
|
||||
if not device:
|
||||
return jsonify({'code': 404, 'message': '设备不存在'})
|
||||
|
||||
# 2. 查询历史记录 (按时间倒序)
|
||||
query = DeviceHistory.query.filter_by(device_id=device.id).order_by(desc(DeviceHistory.data_time))
|
||||
|
||||
# 3. 获取总数
|
||||
total = query.count()
|
||||
|
||||
# 4. 分页切片
|
||||
history_list = query.offset((page - 1) * limit).limit(limit).all()
|
||||
|
||||
# 5. 格式化返回数据
|
||||
data = []
|
||||
for h in history_list:
|
||||
# 简单处理日期格式,只取日期部分,或者保留完整时间视需求而定
|
||||
# 这里假设 data_time 格式为 "YYYY-MM-DD HH:MM:SS" 或 "YYYY_MM_DD..."
|
||||
date_str = h.data_time
|
||||
if not date_str:
|
||||
date_str = h.recorded_at.strftime("%Y-%m-%d %H:%M:%S") if h.recorded_at else "未知"
|
||||
|
||||
data.append({
|
||||
'date': date_str,
|
||||
'count': h.file_count or 0
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'code': 200,
|
||||
'data': data,
|
||||
'total': total,
|
||||
'page': page,
|
||||
'limit': limit
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
Reference in New Issue
Block a user