|
|
# 从app模块导入create_app和db对象
|
|
|
from app import create_app, db
|
|
|
|
|
|
# 从app.models模块导入MonitorPoint和MonitorData模型类
|
|
|
from app.models import MonitorPoint, MonitorData
|
|
|
|
|
|
# 从datetime模块导入datetime和timedelta类,用于处理时间相关操作
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
# 导入random模块,用于生成随机数
|
|
|
import random
|
|
|
|
|
|
# 定义创建历史数据的函数
|
|
|
def create_history_data():
|
|
|
# 创建Flask应用实例
|
|
|
app = create_app()
|
|
|
# 在应用上下文中执行数据库操作
|
|
|
with app.app_context():
|
|
|
# 清除现有的测试数据:先删除MonitorData表中的所有数据
|
|
|
MonitorData.query.delete()
|
|
|
# 再删除MonitorPoint表中的所有数据
|
|
|
MonitorPoint.query.delete()
|
|
|
|
|
|
# 创建测试监控点的数据列表
|
|
|
test_points = [
|
|
|
{
|
|
|
# 监控点名称
|
|
|
'name': '1号养殖池',
|
|
|
# 监控点位置
|
|
|
'location': 'A区-东侧',
|
|
|
# 监控点描述
|
|
|
'description': '草鱼养殖池'
|
|
|
},
|
|
|
{
|
|
|
# 监控点名称
|
|
|
'name': '2号养殖池',
|
|
|
# 监控点位置
|
|
|
'location': 'A区-西侧',
|
|
|
# 监控点描述
|
|
|
'description': '鲫鱼养殖池'
|
|
|
},
|
|
|
{
|
|
|
# 监控点名称
|
|
|
'name': '3号养殖池',
|
|
|
# 监控点位置
|
|
|
'location': 'B区-北侧',
|
|
|
# 监控点描述
|
|
|
'description': '鲤鱼养殖池'
|
|
|
}
|
|
|
]
|
|
|
|
|
|
# 创建空列表用于存储已创建的监控点对象
|
|
|
created_points = []
|
|
|
# 遍历测试监控点数据列表
|
|
|
for point_data in test_points:
|
|
|
# 使用字典解包创建MonitorPoint实例
|
|
|
point = MonitorPoint(**point_data)
|
|
|
# 将新创建的监控点添加到数据库会话
|
|
|
db.session.add(point)
|
|
|
# 将监控点对象添加到已创建点列表中
|
|
|
created_points.append(point)
|
|
|
|
|
|
# 提交数据库事务,保存所有监控点
|
|
|
db.session.commit()
|
|
|
|
|
|
# 为每个监控点生成30天的测试数据
|
|
|
now = datetime.utcnow() # 获取当前UTC时间
|
|
|
# 遍历所有已创建的监控点
|
|
|
for point in created_points:
|
|
|
# 生成过去30天的数据,每天24条数据(每小时一条)
|
|
|
# 从30天前开始,到今天为止,倒序遍历
|
|
|
for days_ago in range(30, -1, -1):
|
|
|
# 计算基础日期(当前时间减去指定天数)
|
|
|
base_date = now - timedelta(days=days_ago)
|
|
|
# 遍历24小时
|
|
|
for hour in range(24):
|
|
|
# 构造具体的时间戳(小时为循环变量,分钟、秒、微秒设为0)
|
|
|
timestamp = base_date.replace(hour=hour, minute=0, second=0, microsecond=0)
|
|
|
|
|
|
# 生成合理范围内的随机数据,添加一些波动性
|
|
|
hour_factor = abs(12 - hour) / 12 # 使数据随时间变化(中午最高)
|
|
|
day_factor = days_ago / 30 # 使数据随天数变化(越久远值越高)
|
|
|
|
|
|
# 基础值加上随机波动
|
|
|
dissolved_oxygen = 6.0 + random.uniform(-1.0, 1.0) + hour_factor # 溶解氧基础值6.0
|
|
|
ph_value = 7.5 + random.uniform(-0.5, 0.5) # pH值基础值7.5
|
|
|
temperature = 24.0 + random.uniform(-2.0, 2.0) + 3 * hour_factor # 温度基础值24.0
|
|
|
ammonia_nitrogen = 0.3 + random.uniform(-0.1, 0.1) + 0.1 * day_factor # 氨氮基础值0.3
|
|
|
turbidity = 20.0 + random.uniform(-5.0, 5.0) + 5 * day_factor # 浊度基础值20.0
|
|
|
|
|
|
# 确保数值在合理范围内,创建MonitorData实例
|
|
|
data = MonitorData(
|
|
|
point_id=point.id, # 关联监控点ID
|
|
|
# 溶解氧限制在4.0-8.0之间,保留2位小数
|
|
|
dissolved_oxygen=round(max(4.0, min(8.0, dissolved_oxygen)), 2),
|
|
|
# pH值限制在6.5-8.5之间,保留2位小数
|
|
|
ph_value=round(max(6.5, min(8.5, ph_value)), 2),
|
|
|
# 温度限制在20.0-28.0之间,保留1位小数
|
|
|
temperature=round(max(20.0, min(28.0, temperature)), 1),
|
|
|
# 氨氮限制在0.1-0.5之间,保留2位小数
|
|
|
ammonia_nitrogen=round(max(0.1, min(0.5, ammonia_nitrogen)), 2),
|
|
|
# 浊度限制在10.0-30.0之间,保留2位小数
|
|
|
turbidity=round(max(10.0, min(30.0, turbidity)), 2),
|
|
|
timestamp=timestamp # 时间戳
|
|
|
)
|
|
|
# 将新创建的监控数据添加到数据库会话
|
|
|
db.session.add(data)
|
|
|
|
|
|
# 提交数据库事务,保存所有监控数据
|
|
|
db.session.commit()
|
|
|
# 打印成功消息
|
|
|
print('历史数据创建成功!')
|
|
|
|
|
|
# 打印统计信息
|
|
|
# 统计监控点数量
|
|
|
points_count = MonitorPoint.query.count()
|
|
|
# 统计监控数据数量
|
|
|
data_count = MonitorData.query.count()
|
|
|
# 输出监控点数量
|
|
|
print(f'已创建 {points_count} 个监控点')
|
|
|
# 输出监控数据数量
|
|
|
print(f'已创建 {data_count} 条监控数据')
|
|
|
# 输出数据范围说明
|
|
|
print('数据范围:')
|
|
|
# 溶解氧范围
|
|
|
print('- 溶解氧: 4.0-8.0 mg/L')
|
|
|
# pH值范围
|
|
|
print('- pH值: 6.5-8.5')
|
|
|
# 温度范围
|
|
|
print('- 温度: 20.0-28.0 ℃')
|
|
|
# 氨氮范围
|
|
|
print('- 氨氮: 0.1-0.5 mg/L')
|
|
|
# 浊度范围
|
|
|
print('- 浊度: 10.0-30.0 NTU')
|
|
|
|
|
|
# 主程序入口:当脚本直接运行时执行create_history_data函数
|
|
|
if __name__ == '__main__':
|
|
|
create_history_data()
|