meteor_detection_system/meteor-web-backend/scripts/seed-meteor-analysis-data.js

241 lines
7.2 KiB
JavaScript

import { Pool } from 'pg';
import dotenv from 'dotenv';
dotenv.config();
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
// 流星雨名称
const meteorShowers = [
'象限仪座流星雨', '英仙座流星雨', '双子座流星雨', '天琴座流星雨',
'宝瓶座流星雨', '猎户座流星雨', '金牛座流星雨', '狮子座流星雨'
];
// 分类
const classifications = ['流星雨', '亮流星', '火球', '流星群', '零散流星'];
// 站点名称
const stationNames = ['北京站', '东京站', '柏林站', '伦敦站', '纽约站', '洛杉矶站', '莫斯科站', '悉尼站'];
// 天气条件
const weatherConditions = ['晴朗', '少云', '多云', '阴天', '小雨', '雷雨', '雾'];
// 方向
const directions = ['东北', '东南', '西南', '西北', '北', '南', '东', '西'];
// 生成随机流星事件数据
function generateMeteorEventData() {
return {
weather_condition: weatherConditions[Math.floor(Math.random() * weatherConditions.length)],
station_name: stationNames[Math.floor(Math.random() * stationNames.length)],
classification: classifications[Math.floor(Math.random() * classifications.length)],
duration: Number((Math.random() * 4 + 0.5).toFixed(2)), // 0.5 to 4.5 seconds
direction: directions[Math.floor(Math.random() * directions.length)],
azimuth: Math.floor(Math.random() * 360), // 0° to 360°
altitude: Math.floor(Math.random() * 90), // 0° to 90°
velocity: Number((Math.random() * 50 + 10).toFixed(2)), // 10 to 60 km/s
shower_name: meteorShowers[Math.floor(Math.random() * meteorShowers.length)],
};
}
// 生成分析结果数据
function generateAnalysisResults() {
const results = [];
// 时间分布分析
const timeDistribution = {
yearly: [
{ period: '2023', count: 15247 },
{ period: '2024', count: 18453 }
],
monthly: [
{ month: '1月', count: 1250 },
{ month: '2月', count: 1100 },
{ month: '3月', count: 1350 },
{ month: '4月', count: 1200 },
{ month: '5月', count: 1400 },
{ month: '6月', count: 1180 },
{ month: '7月', count: 1320 },
{ month: '8月', count: 2247 }, // 英仙座流星雨高峰
{ month: '9月', count: 1150 },
{ month: '10月', count: 1300 },
{ month: '11月', count: 1100 },
{ month: '12月', count: 1650 }
],
hourly: Array.from({ length: 24 }, (_, i) => ({
hour: i,
count: Math.floor(Math.random() * 200) + 50
}))
};
results.push({
analysis_type: 'time_distribution',
time_frame: 'all',
result_data: timeDistribution
});
// 亮度分析
const brightnessAnalysis = {
distribution: [
{ magnitude: '-6以下', count: 150 },
{ magnitude: '-6到-4', count: 450 },
{ magnitude: '-4到-2', count: 2800 },
{ magnitude: '-2到0', count: 5200 },
{ magnitude: '0到2', count: 4500 },
{ magnitude: '2到4', count: 1800 },
{ magnitude: '4以上', count: 347 }
],
statistics: {
total: 15247,
average: -2.5,
median: -2.0,
brightest: -5.6,
dimmest: 4.2
}
};
results.push({
analysis_type: 'brightness_analysis',
time_frame: 'all',
result_data: brightnessAnalysis
});
// 区域分布分析
const regionalAnalysis = [
{ region: '北京站', count: 2150 },
{ region: '东京站', count: 1950 },
{ region: '柏林站', count: 1800 },
{ region: '伦敦站', count: 1700 },
{ region: '纽约站', count: 2200 },
{ region: '洛杉矶站', count: 1900 },
{ region: '莫斯科站', count: 1750 },
{ region: '悉尼站', count: 1797 }
];
results.push({
analysis_type: 'regional_distribution',
time_frame: 'all',
result_data: regionalAnalysis
});
// 统计摘要
const summary = {
totalDetections: 15247,
averageBrightness: -2.5,
mostActiveMonth: { month: '8月', count: 2247, shower: '英仙座流星雨' },
topStations: regionalAnalysis.slice(0, 3)
};
results.push({
analysis_type: 'statistics_summary',
time_frame: 'all',
result_data: summary
});
return results;
}
async function seedMeteorAnalysisData() {
const client = await pool.connect();
try {
await client.query('BEGIN');
console.log('检查现有流星事件数据...');
const eventCountResult = await client.query('SELECT COUNT(*) FROM validated_events');
const eventCount = parseInt(eventCountResult.rows[0].count);
if (eventCount > 0) {
console.log(`找到 ${eventCount} 条现有流星事件,更新分析字段...`);
// 随机更新现有事件的分析字段
const events = await client.query('SELECT id FROM validated_events ORDER BY created_at LIMIT 100');
for (const event of events.rows) {
const analysisData = generateMeteorEventData();
await client.query(`
UPDATE validated_events
SET
weather_condition = $1,
station_name = $2,
classification = $3,
duration = $4,
direction = $5,
azimuth = $6,
altitude = $7,
velocity = $8,
shower_name = $9
WHERE id = $10
`, [
analysisData.weather_condition,
analysisData.station_name,
analysisData.classification,
analysisData.duration,
analysisData.direction,
analysisData.azimuth,
analysisData.altitude,
analysisData.velocity,
analysisData.shower_name,
event.id
]);
}
console.log(`更新了 ${events.rows.length} 条流星事件的分析字段`);
} else {
console.log('没有找到现有流星事件数据,跳过更新步骤');
}
console.log('清理现有分析结果...');
await client.query('DELETE FROM analysis_results');
console.log('插入分析结果数据...');
const analysisResults = generateAnalysisResults();
for (const result of analysisResults) {
await client.query(`
INSERT INTO analysis_results (analysis_type, time_frame, result_data)
VALUES ($1, $2, $3)
`, [
result.analysis_type,
result.time_frame,
JSON.stringify(result.result_data)
]);
console.log(`插入分析结果: ${result.analysis_type}`);
}
await client.query('COMMIT');
console.log('流星分析数据种子插入完成!');
// 显示统计信息
const analysisCount = await client.query('SELECT COUNT(*) FROM analysis_results');
const updatedEventsCount = await client.query(
'SELECT COUNT(*) FROM validated_events WHERE station_name IS NOT NULL'
);
console.log(`插入了 ${analysisCount.rows[0].count} 条分析结果`);
console.log(`更新了 ${updatedEventsCount.rows[0].count} 条流星事件的分析字段`);
} catch (error) {
await client.query('ROLLBACK');
console.error('种子数据插入失败:', error);
} finally {
client.release();
await pool.end();
}
}
// 运行种子脚本
if (import.meta.url === `file://${process.argv[1]}`) {
seedMeteorAnalysisData()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
}
export { seedMeteorAnalysisData };