Compare commits
3 Commits
f2824d88f7
...
1c07b417ce
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c07b417ce | |||
| becf0364ad | |||
| 342042577d |
@ -32,10 +32,6 @@ COPY --from=builder /app/public* ./public/
|
||||
# 设置淘宝源并安装生产依赖
|
||||
RUN npm config set registry https://registry.npmmirror.com && npm install --only=production
|
||||
|
||||
|
||||
ENV DATABASE_URL="mysql://root:fullstack@baishe.vps.honor3.com:6033/iboard"
|
||||
|
||||
|
||||
# 暴露 3000 端口
|
||||
EXPOSE 3000
|
||||
|
||||
|
||||
78
pages/api/sh300-index.js
Normal file
78
pages/api/sh300-index.js
Normal file
@ -0,0 +1,78 @@
|
||||
import { createPool } from 'mariadb';
|
||||
|
||||
async function handler(req, res) {
|
||||
if (req.method !== 'GET') {
|
||||
return res.status(405).json({ message: '只支持GET请求' });
|
||||
}
|
||||
|
||||
const { startDate, endDate, limit } = req.query;
|
||||
|
||||
let pool;
|
||||
try {
|
||||
pool = createPool({
|
||||
host: process.env.DB_HOST || '192.168.111.111',
|
||||
port: parseInt(process.env.DB_PORT || '3306'),
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || 'fullstack',
|
||||
database: process.env.DB_NAME || 'iboard',
|
||||
connectionLimit: 5
|
||||
});
|
||||
|
||||
let query = 'SELECT date, close FROM econ_SH300Index';
|
||||
const params = [];
|
||||
const conditions = [];
|
||||
|
||||
if (startDate && endDate) {
|
||||
conditions.push('date BETWEEN ? AND ?');
|
||||
params.push(startDate, endDate);
|
||||
}
|
||||
|
||||
if (conditions.length > 0) {
|
||||
query += ' WHERE ' + conditions.join(' AND ');
|
||||
}
|
||||
|
||||
query += ' ORDER BY date DESC';
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
// 限制返回数量,默认为30
|
||||
const limitValue = limit ? parseInt(limit) : 30;
|
||||
const limitedResult = result.slice(0, limitValue);
|
||||
|
||||
// 反转顺序,使日期从早到晚
|
||||
const sortedResult = limitedResult.reverse();
|
||||
|
||||
const data = sortedResult.map(row => {
|
||||
let dateStr;
|
||||
if (row.date instanceof Date) {
|
||||
dateStr = row.date.toISOString().split('T')[0];
|
||||
} else if (typeof row.date === 'string') {
|
||||
dateStr = row.date;
|
||||
} else {
|
||||
dateStr = String(row.date);
|
||||
}
|
||||
return {
|
||||
date: dateStr,
|
||||
close: parseFloat(row.close)
|
||||
};
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
data: data
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取沪深300数据失败:', error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: '获取沪深300数据失败: ' + error.message
|
||||
});
|
||||
} finally {
|
||||
if (pool) {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default handler;
|
||||
@ -1,7 +1,8 @@
|
||||
import { useState } from 'react'
|
||||
import { useState, useEffect } from 'react'
|
||||
import Head from 'next/head'
|
||||
import Navbar from '../components/Navbar'
|
||||
import Footer from '../components/Footer'
|
||||
import TimeRangePicker from '../components/TimeRangePicker'
|
||||
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'
|
||||
|
||||
const usDebtData = [
|
||||
@ -28,6 +29,11 @@ const chinaDebtData = [
|
||||
|
||||
export default function Strategy() {
|
||||
const [selectedStrategy, setSelectedStrategy] = useState('us-debt-gdp')
|
||||
const [sh300ExchangeData, setSh300ExchangeData] = useState([])
|
||||
const [sh300Loading, setSh300Loading] = useState(false)
|
||||
const [timeRange, setTimeRange] = useState('1m')
|
||||
const [startDate, setStartDate] = useState('')
|
||||
const [endDate, setEndDate] = useState('')
|
||||
|
||||
const strategies = [
|
||||
{
|
||||
@ -37,9 +43,84 @@ export default function Strategy() {
|
||||
{
|
||||
id: 'china-debt-gdp',
|
||||
name: '中国政府债务/GDP'
|
||||
},
|
||||
{
|
||||
id: 'sh300-exchange-rate',
|
||||
name: '沪深300与汇率对比'
|
||||
}
|
||||
]
|
||||
|
||||
const handleTimeRangeChange = (range, start, end) => {
|
||||
setTimeRange(range)
|
||||
setStartDate(start)
|
||||
setEndDate(end)
|
||||
}
|
||||
|
||||
const fetchSh300ExchangeData = async () => {
|
||||
console.log('fetchSh300ExchangeData called')
|
||||
setSh300Loading(true)
|
||||
try {
|
||||
let sh300Url = '/api/sh300-index'
|
||||
let exchangeUrl = '/api/exchange-rate'
|
||||
|
||||
const params = []
|
||||
if (timeRange === 'custom' && startDate && endDate) {
|
||||
params.push(`startDate=${startDate}`)
|
||||
params.push(`endDate=${endDate}`)
|
||||
} else if (timeRange === '1m') {
|
||||
params.push('limit=30')
|
||||
} else if (timeRange === '3m') {
|
||||
params.push('limit=90')
|
||||
} else if (timeRange === '1y') {
|
||||
params.push('limit=365')
|
||||
}
|
||||
|
||||
if (params.length > 0) {
|
||||
sh300Url += '?' + params.join('&')
|
||||
exchangeUrl += '?' + params.join('&')
|
||||
}
|
||||
|
||||
console.log('Fetching from:', sh300Url, exchangeUrl)
|
||||
|
||||
const [sh300Res, exchangeRes] = await Promise.all([
|
||||
fetch(sh300Url),
|
||||
fetch(exchangeUrl)
|
||||
])
|
||||
console.log('Response status:', sh300Res.status, exchangeRes.status)
|
||||
|
||||
const sh300Json = await sh300Res.json()
|
||||
const exchangeJson = await exchangeRes.json()
|
||||
|
||||
console.log('Response data:', sh300Json, exchangeJson)
|
||||
|
||||
if (sh300Json.success && exchangeJson.success) {
|
||||
const sh300Map = new Map(sh300Json.data.map(item => [item.date, item.close]))
|
||||
const exchangeMap = new Map(exchangeJson.data.map(item => [item.date, item.rate]))
|
||||
|
||||
const allDates = [...new Set([...sh300Map.keys(), ...exchangeMap.keys()])].sort()
|
||||
|
||||
const mergedData = allDates.map(date => ({
|
||||
date,
|
||||
sh300: sh300Map.get(date) || null,
|
||||
exchangeRate: exchangeMap.get(date) || null
|
||||
})).filter(item => item.sh300 !== null && item.exchangeRate !== null)
|
||||
|
||||
console.log('Merged data:', mergedData)
|
||||
setSh300ExchangeData(mergedData)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取数据失败:', error)
|
||||
} finally {
|
||||
setSh300Loading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedStrategy === 'sh300-exchange-rate') {
|
||||
fetchSh300ExchangeData()
|
||||
}
|
||||
}, [selectedStrategy, timeRange, startDate, endDate])
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
<Head>
|
||||
@ -272,6 +353,46 @@ export default function Strategy() {
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{selectedStrategy === 'sh300-exchange-rate' && (
|
||||
<div className="strategy-detail">
|
||||
<h2 className="strategy-title">沪深300与汇率对比分析</h2>
|
||||
<p className="strategy-description">
|
||||
对比沪深300指数收盘价与人民币兑美元汇率的走势关系
|
||||
</p>
|
||||
|
||||
{sh300Loading ? (
|
||||
<div className="loading">加载中...</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="chart-header">
|
||||
<TimeRangePicker
|
||||
value={timeRange}
|
||||
onChange={handleTimeRangeChange}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="chart-section">
|
||||
<h3 className="chart-title">沪深300与汇率对比(双Y轴)</h3>
|
||||
<div className="chart-container">
|
||||
<ResponsiveContainer width="100%" height={400}>
|
||||
<LineChart data={sh300ExchangeData} margin={{ top: 20, right: 60, left: 20, bottom: 5 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" />
|
||||
<XAxis dataKey="date" />
|
||||
<YAxis yAxisId="left" domain={['auto', 'auto']} allowDataOverflow={true} />
|
||||
<YAxis yAxisId="right" orientation="right" domain={['auto', 'auto']} allowDataOverflow={true} tickFormatter={(value) => value.toFixed(4)} />
|
||||
<Tooltip />
|
||||
<Legend />
|
||||
<Line yAxisId="left" type="monotone" dataKey="sh300" name="沪深300收盘价" stroke="#8884d8" strokeWidth={2} activeDot={{ r: 8 }} />
|
||||
<Line yAxisId="right" type="monotone" dataKey="exchangeRate" name="汇率" stroke="#82ca9d" strokeWidth={2} activeDot={{ r: 8 }} />
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
79
scripts/fetch-one-year.js
Normal file
79
scripts/fetch-one-year.js
Normal file
@ -0,0 +1,79 @@
|
||||
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
async function runScraper(scraperPath, startDate, endDate) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const args = [scraperPath];
|
||||
if (startDate) {
|
||||
args.push('--start', startDate);
|
||||
}
|
||||
if (endDate) {
|
||||
args.push('--end', endDate);
|
||||
}
|
||||
|
||||
const proc = spawn('bun', args, { stdio: 'inherit' });
|
||||
proc.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error(`Process exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
proc.on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchOneYearData() {
|
||||
console.log('='.repeat(70));
|
||||
console.log('📊 获取近一年数据');
|
||||
console.log('='.repeat(70));
|
||||
|
||||
const currentDate = new Date();
|
||||
const startDate = new Date(currentDate);
|
||||
startDate.setFullYear(startDate.getFullYear() - 1);
|
||||
|
||||
const startDateStr = startDate.toISOString().split('T')[0];
|
||||
const endDateStr = currentDate.toISOString().split('T')[0];
|
||||
|
||||
console.log(`时间范围: ${startDateStr} 至 ${endDateStr}`);
|
||||
console.log('');
|
||||
|
||||
console.log('='.repeat(70));
|
||||
console.log('💱 开始获取 USD-CNY 汇率数据...');
|
||||
console.log('='.repeat(70));
|
||||
|
||||
let usdSuccess = false;
|
||||
try {
|
||||
await runScraper('./scraper/usd-cny-rate.scraper.js', startDateStr, endDateStr);
|
||||
console.log(`✅ USD-CNY 汇率数据更新完成`);
|
||||
usdSuccess = true;
|
||||
} catch (error) {
|
||||
console.error(`❌ USD-CNY 汇率数据更新失败:`, error.message);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log('='.repeat(70));
|
||||
console.log('📈 开始获取沪深300指数数据...');
|
||||
console.log('='.repeat(70));
|
||||
|
||||
let sh300Success = false;
|
||||
try {
|
||||
await runScraper('./scraper/sh300.scraper.js', startDateStr, endDateStr);
|
||||
console.log(`✅ 沪深300指数数据更新完成`);
|
||||
sh300Success = true;
|
||||
} catch (error) {
|
||||
console.error(`❌ 沪深300指数数据更新失败:`, error.message);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log('='.repeat(70));
|
||||
console.log('📊 数据获取完成');
|
||||
console.log('='.repeat(70));
|
||||
console.log(`USD-CNY 汇率: ${usdSuccess ? '✅ 成功' : '❌ 失败'}`);
|
||||
console.log(`沪深300指数: ${sh300Success ? '✅ 成功' : '❌ 失败'}`);
|
||||
console.log('='.repeat(70));
|
||||
}
|
||||
|
||||
fetchOneYearData();
|
||||
@ -1,12 +1,13 @@
|
||||
import cron from 'node-cron';
|
||||
import { trigger as triggerUSDCNYRate } from '../scraper/usd-cny-rate.scraper.js';
|
||||
import { trigger as triggerSH300Data } from '../scraper/sh300.scraper.js';
|
||||
|
||||
console.log('='.repeat(70));
|
||||
console.log('🚀 启动定时任务调度器');
|
||||
console.log('='.repeat(70));
|
||||
|
||||
// 注册定时任务
|
||||
const job = cron.schedule('0 0 * * *', async () => {
|
||||
// 注册定时任务 - USD-CNY 汇率数据
|
||||
const jobUSDCNY = cron.schedule('0 0 * * *', async () => {
|
||||
console.log('\n' + '='.repeat(70));
|
||||
console.log('⏰ 执行定时任务: USD-CNY 汇率数据更新');
|
||||
console.log('📅 执行时间:', new Date().toISOString());
|
||||
@ -25,10 +26,34 @@ const job = cron.schedule('0 0 * * *', async () => {
|
||||
timezone: 'Asia/Shanghai'
|
||||
});
|
||||
|
||||
// 注册定时任务 - 沪深300指数数据
|
||||
const jobSH300 = cron.schedule('0 0 * * *', async () => {
|
||||
console.log('\n' + '='.repeat(70));
|
||||
console.log('⏰ 执行定时任务: 沪深300指数数据更新');
|
||||
console.log('📅 执行时间:', new Date().toISOString());
|
||||
console.log('='.repeat(70));
|
||||
|
||||
try {
|
||||
await triggerSH300Data();
|
||||
console.log('\n✅ 任务执行成功!');
|
||||
} catch (error) {
|
||||
console.error('\n❌ 任务执行失败:', error.message);
|
||||
} finally {
|
||||
console.log('='.repeat(70));
|
||||
}
|
||||
}, {
|
||||
scheduled: true,
|
||||
timezone: 'Asia/Shanghai'
|
||||
});
|
||||
|
||||
console.log('✅ 定时任务已启动: USD-CNY 汇率数据更新');
|
||||
console.log('📅 执行表达式: 0 0 * * * (每天 0 点)');
|
||||
console.log('📅 下次执行时间: 明天 00:00');
|
||||
console.log('');
|
||||
console.log('✅ 定时任务已启动: 沪深300指数数据更新');
|
||||
console.log('📅 执行表达式: 0 0 * * * (每天 0 点)');
|
||||
console.log('📅 下次执行时间: 明天 00:00');
|
||||
console.log('');
|
||||
console.log('按 Ctrl+C 停止...');
|
||||
console.log('='.repeat(70));
|
||||
|
||||
@ -38,7 +63,8 @@ process.on('SIGINT', () => {
|
||||
console.log('🛑 停止定时任务调度器');
|
||||
console.log('='.repeat(70));
|
||||
|
||||
job.stop();
|
||||
jobUSDCNY.stop();
|
||||
jobSH300.stop();
|
||||
console.log('✅ 定时任务已停止');
|
||||
|
||||
console.log('\n' + '='.repeat(70));
|
||||
|
||||
@ -265,13 +265,50 @@ export async function trigger(startDate = null, endDate = null) {
|
||||
return await checkAndFillSH300Data(startDate, endDate);
|
||||
}
|
||||
|
||||
// 直接运行测试
|
||||
function parseCommandLineArgs() {
|
||||
const args = process.argv.slice(2);
|
||||
let startDate = null;
|
||||
let endDate = null;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--start' && i + 1 < args.length) {
|
||||
startDate = args[i + 1];
|
||||
i++;
|
||||
} else if (args[i] === '--end' && i + 1 < args.length) {
|
||||
endDate = args[i + 1];
|
||||
i++;
|
||||
} else if (args[i] === '--help') {
|
||||
console.log('使用方法:');
|
||||
console.log(' bun sh300.scraper.js [选项]');
|
||||
console.log('');
|
||||
console.log('选项:');
|
||||
console.log(' --start YYYY-MM-DD 指定开始日期');
|
||||
console.log(' --end YYYY-MM-DD 指定结束日期');
|
||||
console.log(' --help 显示帮助信息');
|
||||
console.log('');
|
||||
console.log('示例:');
|
||||
console.log(' bun sh300.scraper.js');
|
||||
console.log(' bun sh300.scraper.js --start 2024-01-01 --end 2024-01-31');
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
return { startDate, endDate };
|
||||
}
|
||||
|
||||
async function runTest() {
|
||||
console.log('='.repeat(60));
|
||||
console.log('测试 sh300.scraper.js');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
// 测试配置读取
|
||||
const { startDate, endDate } = parseCommandLineArgs();
|
||||
|
||||
if (startDate) {
|
||||
console.log(`\n使用命令行参数:`);
|
||||
console.log(` 开始日期: ${startDate}`);
|
||||
console.log(` 结束日期: ${endDate || '默认'}`);
|
||||
}
|
||||
|
||||
console.log('\n测试数据库配置读取...');
|
||||
const config = loadDatabaseConfig();
|
||||
console.log('配置读取结果:');
|
||||
@ -282,7 +319,7 @@ async function runTest() {
|
||||
console.log('配置读取测试完成!');
|
||||
|
||||
try {
|
||||
const result = await trigger();
|
||||
const result = await trigger(startDate, endDate);
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log('测试结果:');
|
||||
console.log(` 开始日期: ${result.startDate}`);
|
||||
@ -298,5 +335,4 @@ async function runTest() {
|
||||
}
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
runTest();
|
||||
|
||||
@ -262,13 +262,50 @@ export async function trigger(startDate = null, endDate = null) {
|
||||
return await checkAndFillUSDCNYRate(startDate, endDate);
|
||||
}
|
||||
|
||||
// 直接运行测试
|
||||
function parseCommandLineArgs() {
|
||||
const args = process.argv.slice(2);
|
||||
let startDate = null;
|
||||
let endDate = null;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--start' && i + 1 < args.length) {
|
||||
startDate = args[i + 1];
|
||||
i++;
|
||||
} else if (args[i] === '--end' && i + 1 < args.length) {
|
||||
endDate = args[i + 1];
|
||||
i++;
|
||||
} else if (args[i] === '--help') {
|
||||
console.log('使用方法:');
|
||||
console.log(' bun usd-cny-rate.scraper.js [选项]');
|
||||
console.log('');
|
||||
console.log('选项:');
|
||||
console.log(' --start YYYY-MM-DD 指定开始日期');
|
||||
console.log(' --end YYYY-MM-DD 指定结束日期');
|
||||
console.log(' --help 显示帮助信息');
|
||||
console.log('');
|
||||
console.log('示例:');
|
||||
console.log(' bun usd-cny-rate.scraper.js');
|
||||
console.log(' bun usd-cny-rate.scraper.js --start 2024-01-01 --end 2024-01-31');
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
return { startDate, endDate };
|
||||
}
|
||||
|
||||
async function runTest() {
|
||||
console.log('='.repeat(60));
|
||||
console.log('测试 usd-cny-rate.scraper.js');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
// 测试配置读取
|
||||
const { startDate, endDate } = parseCommandLineArgs();
|
||||
|
||||
if (startDate) {
|
||||
console.log(`\n使用命令行参数:`);
|
||||
console.log(` 开始日期: ${startDate}`);
|
||||
console.log(` 结束日期: ${endDate || '默认'}`);
|
||||
}
|
||||
|
||||
console.log('\n测试数据库配置读取...');
|
||||
const config = loadDatabaseConfig();
|
||||
console.log('配置读取结果:');
|
||||
@ -279,7 +316,7 @@ async function runTest() {
|
||||
console.log('配置读取测试完成!');
|
||||
|
||||
try {
|
||||
const result = await trigger();
|
||||
const result = await trigger(startDate, endDate);
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log('测试结果:');
|
||||
console.log(` 开始日期: ${result.startDate}`);
|
||||
@ -295,5 +332,4 @@ async function runTest() {
|
||||
}
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
runTest();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user