105 lines
3.7 KiB
JavaScript
105 lines
3.7 KiB
JavaScript
|
|
const mysql = require('mysql2/promise');
|
||
|
|
require('dotenv').config();
|
||
|
|
|
||
|
|
async function verifyEnvironmentData() {
|
||
|
|
let connection;
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 创建数据库连接
|
||
|
|
connection = await mysql.createConnection({
|
||
|
|
host: process.env.DB_HOST,
|
||
|
|
port: process.env.DB_PORT,
|
||
|
|
user: process.env.DB_USER,
|
||
|
|
password: process.env.DB_PASSWORD,
|
||
|
|
database: process.env.DB_NAME
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('数据库连接成功');
|
||
|
|
|
||
|
|
// 检查表是否存在
|
||
|
|
const [tables] = await connection.execute(
|
||
|
|
"SHOW TABLES LIKE 'environment_schedules'"
|
||
|
|
);
|
||
|
|
|
||
|
|
if (tables.length === 0) {
|
||
|
|
console.log('环境监测时刻表不存在');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('环境监测时刻表存在');
|
||
|
|
|
||
|
|
// 获取总记录数
|
||
|
|
const [countResult] = await connection.execute(
|
||
|
|
'SELECT COUNT(*) as total_records FROM environment_schedules'
|
||
|
|
);
|
||
|
|
console.log(`\n总记录数: ${countResult[0].total_records}`);
|
||
|
|
|
||
|
|
// 按农场分组统计
|
||
|
|
const [farmStats] = await connection.execute(
|
||
|
|
`SELECT farm_id, COUNT(*) as record_count,
|
||
|
|
MIN(monitoring_date) as earliest_date,
|
||
|
|
MAX(monitoring_date) as latest_date
|
||
|
|
FROM environment_schedules
|
||
|
|
GROUP BY farm_id
|
||
|
|
ORDER BY farm_id`
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('\n按农场统计:');
|
||
|
|
farmStats.forEach(stat => {
|
||
|
|
console.log(`农场${stat.farm_id}: ${stat.record_count}条记录, 日期范围: ${stat.earliest_date} 到 ${stat.latest_date}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 获取最新的10条记录
|
||
|
|
const [recentRecords] = await connection.execute(
|
||
|
|
`SELECT farm_id, device_id, schedule_time, temperature, humidity,
|
||
|
|
DATE(monitoring_date) as date, status
|
||
|
|
FROM environment_schedules
|
||
|
|
ORDER BY monitoring_date DESC, schedule_time DESC
|
||
|
|
LIMIT 10`
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('\n最新的10条环境监测记录:');
|
||
|
|
console.log('农场ID | 设备ID | 日期 | 时间 | 温度(°C) | 湿度(%) | 状态');
|
||
|
|
console.log('-------|----------|------------|----------|----------|---------|--------');
|
||
|
|
recentRecords.forEach(record => {
|
||
|
|
console.log(`${record.farm_id.toString().padEnd(6)} | ${record.device_id.padEnd(8)} | ${record.date} | ${record.schedule_time} | ${record.temperature.toString().padEnd(8)} | ${record.humidity.toString().padEnd(7)} | ${record.status}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 检查数据分布
|
||
|
|
const [timeStats] = await connection.execute(
|
||
|
|
`SELECT schedule_time, COUNT(*) as count
|
||
|
|
FROM environment_schedules
|
||
|
|
GROUP BY schedule_time
|
||
|
|
ORDER BY schedule_time`
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('\n按监测时间统计:');
|
||
|
|
timeStats.forEach(stat => {
|
||
|
|
console.log(`${stat.schedule_time}: ${stat.count}条记录`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 温度湿度范围统计
|
||
|
|
const [rangeStats] = await connection.execute(
|
||
|
|
`SELECT
|
||
|
|
MIN(temperature) as min_temp, MAX(temperature) as max_temp, AVG(temperature) as avg_temp,
|
||
|
|
MIN(humidity) as min_humidity, MAX(humidity) as max_humidity, AVG(humidity) as avg_humidity
|
||
|
|
FROM environment_schedules`
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('\n温度湿度统计:');
|
||
|
|
const stats = rangeStats[0];
|
||
|
|
console.log(`温度范围: ${parseFloat(stats.min_temp).toFixed(2)}°C - ${parseFloat(stats.max_temp).toFixed(2)}°C (平均: ${parseFloat(stats.avg_temp).toFixed(2)}°C)`);
|
||
|
|
console.log(`湿度范围: ${parseFloat(stats.min_humidity).toFixed(2)}% - ${parseFloat(stats.max_humidity).toFixed(2)}% (平均: ${parseFloat(stats.avg_humidity).toFixed(2)}%)`);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('验证失败:', error.message);
|
||
|
|
} finally {
|
||
|
|
if (connection) {
|
||
|
|
await connection.end();
|
||
|
|
console.log('\n数据库连接已关闭');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行验证脚本
|
||
|
|
verifyEnvironmentData();
|