index.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>多平台价格与价差监控</title>
  7. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  8. <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@2.0.1/dist/chartjs-plugin-zoom.min.js"></script>
  9. <style>
  10. body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f4; color: #333; }
  11. .container { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 20px; }
  12. h1, h2 { text-align: center; color: #333; }
  13. table { width: 100%; border-collapse: collapse; margin-top: 20px; margin-bottom: 20px; }
  14. th, td { border: 1px solid #ddd; padding: 10px; text-align: left; font-size:0.9em; } /* Smaller padding/font for more data */
  15. th { background-color: #e9e9e9; }
  16. .price-up { color: green; }
  17. .price-down { color: red; }
  18. .error-message { color: #c00; font-style: italic; font-size: 0.9em; }
  19. .timestamp { font-size: 0.9em; color: #666; text-align: right; margin-top: 15px; }
  20. .chart-container { position: relative; height: 40vh; width: 90vw; margin: auto; margin-bottom: 10px; }
  21. .controls-container { text-align: center; margin-bottom: 20px; margin-top: 5px; }
  22. .control-button { background-color: #007bff; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer; margin:0 5px; }
  23. .control-button:hover { background-color: #0056b3; }
  24. .pause-button-active { background-color: #ffc107; color: #333; }
  25. .pause-button-active:hover { background-color: #e0a800; }
  26. .platform-name {font-weight: bold;}
  27. </style>
  28. </head>
  29. <body>
  30. <div class="container">
  31. <h1 id="main-title">价格监控</h1>
  32. <div class="controls-container">
  33. <button id="pause-resume-button" class="control-button">暂停刷新</button>
  34. </div>
  35. <table>
  36. <thead>
  37. <tr>
  38. <th>平台</th>
  39. <th>价格 (USDT)</th>
  40. <th>状态/错误</th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. <tr>
  45. <td class="platform-name">OpenOcean (BSC)</td>
  46. <td id="oo-price">加载中...</td>
  47. <td id="oo-status" class="status-cell"></td>
  48. </tr>
  49. <tr>
  50. <td class="platform-name" id="gate-spot-label">Gate.io 现货</td>
  51. <td id="gate-spot-price">加载中...</td>
  52. <td id="gate-spot-status" class="status-cell"></td>
  53. </tr>
  54. <tr>
  55. <td class="platform-name" id="gate-futures-label">Gate.io 期货</td>
  56. <td id="gate-futures-price">加载中...</td>
  57. <td id="gate-futures-status" class="status-cell"></td>
  58. </tr>
  59. </tbody>
  60. </table>
  61. <h2>价差百分比</h2>
  62. <table>
  63. <thead>
  64. <tr>
  65. <th>对比</th>
  66. <th>价差 (%)</th>
  67. </tr>
  68. </thead>
  69. <tbody>
  70. <tr>
  71. <td id="diff-label-oo-spot">OO vs Gate.io 现货</td>
  72. <td id="diff-oo-vs-spot">计算中...</td>
  73. </tr>
  74. <tr>
  75. <td id="diff-label-oo-futures">OO vs Gate.io 期货</td>
  76. <td id="diff-oo-vs-futures">计算中...</td>
  77. </tr>
  78. <tr>
  79. <td id="diff-label-spot-futures">Gate.io 现货 vs 期货</td>
  80. <td id="diff-spot-vs-futures">计算中...</td>
  81. </tr>
  82. </tbody>
  83. </table>
  84. <div class="timestamp">最后更新: <span id="last-updated">N/A</span></div>
  85. </div>
  86. <div class="container">
  87. <h2 id="price-chart-title">价格历史曲线</h2>
  88. <div class="chart-container">
  89. <canvas id="priceHistoryChart"></canvas>
  90. </div>
  91. <div class="controls-container">
  92. <button id="reset-price-zoom-button" class="control-button">重置缩放</button>
  93. </div>
  94. </div>
  95. <div class="container">
  96. <h2 id="diff-chart-title">价差百分比历史曲线</h2>
  97. <div class="chart-container">
  98. <canvas id="diffPercentageChart"></canvas> <!-- Changed ID -->
  99. </div>
  100. <div class="controls-container">
  101. <button id="reset-diff-zoom-button" class="control-button">重置缩放</button>
  102. </div>
  103. </div>
  104. <script>
  105. let priceChartInstance = null;
  106. let diffChartInstance = null; // For percentage differences
  107. let dataUpdateIntervalID = null;
  108. let isPaused = false;
  109. const REFRESH_INTERVAL_MS = 1000;
  110. const pauseResumeButton = document.getElementById('pause-resume-button');
  111. let isSyncingZoomPan = false;
  112. // Get initial config from Flask if available
  113. const initialConfig = {
  114. GATEIO_SPOT_PAIR: "{{ config.GATEIO_SPOT_PAIR if config else 'SPOT_USDT' }}",
  115. GATEIO_FUTURES_CONTRACT: "{{ config.GATEIO_FUTURES_CONTRACT if config else 'FUTURES_USDT' }}",
  116. TARGET_ASSET_SYMBOL: "{{ config.TARGET_ASSET_SYMBOL if config else 'ASSET' }}"
  117. };
  118. let currentGateSpotPair = initialConfig.GATEIO_SPOT_PAIR;
  119. let currentGateFuturesContract = initialConfig.GATEIO_FUTURES_CONTRACT;
  120. let currentAssetSymbol = initialConfig.TARGET_ASSET_SYMBOL;
  121. function formatPrice(priceStr) {
  122. // (formatPrice function from previous example, can be reused)
  123. if (priceStr === null || priceStr === undefined || priceStr === "N/A") return "N/A";
  124. const price = parseFloat(priceStr);
  125. if (isNaN(price)) return "N/A";
  126. if (price === 0) return "0.000000";
  127. if (price < 0.000001 && price > 0) return price.toExponential(4);
  128. if (price < 0.01) return price.toFixed(8);
  129. if (price < 1) return price.toFixed(6);
  130. return price.toFixed(4);
  131. }
  132. function formatPercentage(percStr) {
  133. if (percStr === null || percStr === undefined || percStr === "N/A") return "N/A";
  134. // Assumes percStr is like "+0.1234%" or "-5.6789%"
  135. if (String(percStr).includes('%')) return percStr;
  136. const perc = parseFloat(percStr);
  137. if (isNaN(perc)) return "N/A";
  138. return `${perc > 0 ? '+' : ''}${perc.toFixed(4)}%`;
  139. }
  140. function syncXAxes(sourceChart, targetChart) { /* (Same as before) */
  141. if (isSyncingZoomPan || !sourceChart || !targetChart) return;
  142. isSyncingZoomPan = true;
  143. const sourceXScale = sourceChart.scales.x;
  144. const targetXScale = targetChart.scales.x;
  145. if (sourceXScale && targetXScale) {
  146. targetXScale.options.min = sourceXScale.min;
  147. targetXScale.options.max = sourceXScale.max;
  148. targetChart.update('none');
  149. }
  150. requestAnimationFrame(() => { isSyncingZoomPan = false; });
  151. }
  152. function initializeChart(ctx, chartIdentifier, datasetsConfig, initialLabels, yAxisTitle, chartTitleText, isPriceChart = true) {
  153. return new Chart(ctx, {
  154. type: 'line',
  155. data: { labels: initialLabels, datasets: datasetsConfig },
  156. options: {
  157. responsive: true, maintainAspectRatio: false, animation: { duration: 150 },
  158. scales: {
  159. x: { title: { display: true, text: '时间' } },
  160. y: {
  161. title: { display: true, text: yAxisTitle },
  162. beginAtZero: !isPriceChart ? false : undefined, // For diff chart, don't begin at zero
  163. ticks: {
  164. callback: function(value) {
  165. return isPriceChart ? formatPrice(String(value)) : parseFloat(value).toFixed(2) + '%';
  166. }
  167. }
  168. }
  169. },
  170. plugins: {
  171. legend: { position: 'top' }, title: { display: true, text: chartTitleText },
  172. tooltip: {
  173. callbacks: {
  174. label: function(context) {
  175. let label = context.dataset.label || '';
  176. if (label) label += ': ';
  177. if (context.parsed.y !== null) {
  178. label += isPriceChart ? formatPrice(String(context.parsed.y)) : parseFloat(context.parsed.y).toFixed(4) + '%';
  179. }
  180. return label;
  181. }
  182. }
  183. },
  184. zoom: { /* (Zoom config same as before) */
  185. pan: {
  186. enabled: true, mode: 'x', threshold: 5,
  187. onPanComplete({chart}) {
  188. if (chartIdentifier === 'price' && diffChartInstance) syncXAxes(chart, diffChartInstance);
  189. else if (chartIdentifier === 'diff' && priceChartInstance) syncXAxes(chart, priceChartInstance);
  190. }
  191. },
  192. zoom: {
  193. wheel: { enabled: true, speed: 0.1 }, pinch: { enabled: true },
  194. drag: { enabled: true, backgroundColor: 'rgba(0,123,255,0.25)'},
  195. mode: 'x',
  196. onZoomComplete({chart}) {
  197. if (chartIdentifier === 'price' && diffChartInstance) syncXAxes(chart, diffChartInstance);
  198. else if (chartIdentifier === 'diff' && priceChartInstance) syncXAxes(chart, priceChartInstance);
  199. }
  200. }
  201. }
  202. },
  203. elements: { point:{ radius: 1.5 } } // Smaller points for more lines
  204. }
  205. });
  206. }
  207. function updateChartData(chartInstance, newLabels, newDatasetsDataArray) {
  208. // (updateChartData function largely same, ensures it updates all datasets in newDatasetsDataArray)
  209. if (!chartInstance) return;
  210. const currentXMin = chartInstance.scales.x.min;
  211. const currentXMax = chartInstance.scales.x.max;
  212. let isZoomed = (currentXMin !== undefined && currentXMax !== undefined);
  213. if (isZoomed && chartInstance.data.labels.length > 0) {
  214. isZoomed = (currentXMin !== chartInstance.data.labels[0] || currentXMax !== chartInstance.data.labels[chartInstance.data.labels.length - 1]);
  215. }
  216. chartInstance.data.labels = newLabels;
  217. newDatasetsDataArray.forEach((datasetData, index) => {
  218. if(chartInstance.data.datasets[index]) { // Check if dataset exists
  219. chartInstance.data.datasets[index].data = datasetData;
  220. }
  221. });
  222. if (!isPaused) {
  223. chartInstance.options.scales.x.min = undefined;
  224. chartInstance.options.scales.x.max = undefined;
  225. } else {
  226. if(isZoomed) {
  227. chartInstance.options.scales.x.min = currentXMin;
  228. chartInstance.options.scales.x.max = currentXMax;
  229. } else {
  230. chartInstance.options.scales.x.min = undefined;
  231. chartInstance.options.scales.x.max = undefined;
  232. }
  233. }
  234. chartInstance.update('quiet');
  235. }
  236. function updateDisplayAndCharts() {
  237. fetch('/data')
  238. .then(response => response.json())
  239. .then(data => {
  240. const current = data.current;
  241. // Update global config vars if they changed (though unlikely for these)
  242. currentGateSpotPair = current.config_gate_spot_pair || currentGateSpotPair;
  243. currentGateFuturesContract = current.config_gate_futures_contract || currentGateFuturesContract;
  244. currentAssetSymbol = current.config_target_asset_symbol || currentAssetSymbol;
  245. // --- Update Titles and Labels ---
  246. document.getElementById('main-title').textContent = `${currentAssetSymbol}/USDT 多平台价格监控`;
  247. document.getElementById('gate-spot-label').textContent = `Gate.io 现货 (${currentGateSpotPair})`;
  248. document.getElementById('gate-futures-label').textContent = `Gate.io 期货 (${currentGateFuturesContract})`;
  249. document.getElementById('diff-label-oo-spot').textContent = `OO vs Gate.io 现货 (${currentGateSpotPair})`;
  250. document.getElementById('diff-label-oo-futures').textContent = `OO vs Gate.io 期货 (${currentGateFuturesContract})`;
  251. document.getElementById('diff-label-spot-futures').textContent = `Gate.io 现货 (${currentGateSpotPair}) vs 期货 (${currentGateFuturesContract})`;
  252. const priceChartTitle = `${currentAssetSymbol}/USDT 价格历史`;
  253. const priceChartYLabel = `价格 (${currentAssetSymbol}/USDT)`;
  254. const diffChartTitleText = "价差百分比历史";
  255. if (priceChartInstance) {
  256. priceChartInstance.options.plugins.title.text = priceChartTitle;
  257. priceChartInstance.options.scales.y.title.text = priceChartYLabel;
  258. }
  259. if (diffChartInstance) {
  260. diffChartInstance.options.plugins.title.text = diffChartTitleText;
  261. }
  262. // --- Update Table Data ---
  263. document.getElementById('oo-price').textContent = formatPrice(current.oo_price);
  264. document.getElementById('gate-spot-price').textContent = formatPrice(current.gate_spot_price);
  265. document.getElementById('gate-futures-price').textContent = formatPrice(current.gate_futures_price);
  266. document.getElementById('oo-status').textContent = current.oo_error ? `错误: ${current.oo_error}` : '正常';
  267. document.getElementById('gate-spot-status').textContent = current.gate_spot_error ? `错误: ${current.gate_spot_error}` : '正常';
  268. document.getElementById('gate-futures-status').textContent = current.gate_futures_error ? `错误: ${current.gate_futures_error}` : '正常';
  269. // Update error message classes
  270. document.getElementById('oo-status').className = current.oo_error ? 'status-cell error-message' : 'status-cell';
  271. document.getElementById('gate-spot-status').className = current.gate_spot_error ? 'status-cell error-message' : 'status-cell';
  272. document.getElementById('gate-futures-status').className = current.gate_futures_error ? 'status-cell error-message' : 'status-cell';
  273. // Update diff percentages
  274. const diffOOSpotEl = document.getElementById('diff-oo-vs-spot');
  275. const diffOOFuturesEl = document.getElementById('diff-oo-vs-futures');
  276. const diffSpotFuturesEl = document.getElementById('diff-spot-vs-futures');
  277. diffOOSpotEl.textContent = formatPercentage(current.diff_oo_vs_spot_percentage);
  278. diffOOFuturesEl.textContent = formatPercentage(current.diff_oo_vs_futures_percentage);
  279. diffSpotFuturesEl.textContent = formatPercentage(current.diff_spot_vs_futures_percentage);
  280. [diffOOSpotEl, diffOOFuturesEl, diffSpotFuturesEl].forEach(el => {
  281. const valStr = el.textContent.replace('%','').replace('+','');
  282. const val = parseFloat(valStr);
  283. if (!isNaN(val)) {
  284. el.className = val > 0 ? 'price-up' : (val < 0 ? 'price-down' : '');
  285. } else { el.className = ''; }
  286. });
  287. document.getElementById('last-updated').textContent = current.last_updated || "N/A";
  288. // --- Update Charts ---
  289. const history = data.history;
  290. // Price History Chart
  291. const priceCtx = document.getElementById('priceHistoryChart').getContext('2d');
  292. const priceDatasets = [
  293. { label: 'OpenOcean', data: history.prices.oo, borderColor: 'rgb(75, 192, 192)', tension: 0.1, fill: false },
  294. { label: `Gate Spot (${currentGateSpotPair})`, data: history.prices.spot, borderColor: 'rgb(255, 99, 132)', tension: 0.1, fill: false },
  295. { label: `Gate Futures (${currentGateFuturesContract})`, data: history.prices.futures, borderColor: 'rgb(54, 162, 235)', tension: 0.1, fill: false }
  296. ];
  297. if (!priceChartInstance) {
  298. priceChartInstance = initializeChart(priceCtx, 'price', priceDatasets, history.prices.labels, priceChartYLabel, priceChartTitle, true);
  299. } else {
  300. priceChartInstance.data.datasets[1].label = `Gate Spot (${currentGateSpotPair})`;
  301. priceChartInstance.data.datasets[2].label = `Gate Futures (${currentGateFuturesContract})`;
  302. updateChartData(priceChartInstance, history.prices.labels, [history.prices.oo, history.prices.spot, history.prices.futures]);
  303. }
  304. // Diff Percentage Chart
  305. const diffCtx = document.getElementById('diffPercentageChart').getContext('2d');
  306. const diffDatasets = [
  307. { label: `OO vs Spot (${currentGateSpotPair})`, data: history.diffs.oo_vs_spot, borderColor: 'rgb(255, 159, 64)', tension: 0.1, fill: false },
  308. { label: `OO vs Futures (${currentGateFuturesContract})`, data: history.diffs.oo_vs_futures, borderColor: 'rgb(153, 102, 255)', tension: 0.1, fill: false },
  309. { label: `Spot (${currentGateSpotPair}) vs Futures (${currentGateFuturesContract})`, data: history.diffs.spot_vs_futures, borderColor: 'rgb(75, 192, 75)', tension: 0.1, fill: false }
  310. ];
  311. if (!diffChartInstance) {
  312. diffChartInstance = initializeChart(diffCtx, 'diff', diffDatasets, history.diffs.labels, '价差 (%)', diffChartTitleText, false);
  313. } else {
  314. diffChartInstance.data.datasets[0].label = `OO vs Spot (${currentGateSpotPair})`;
  315. diffChartInstance.data.datasets[1].label = `OO vs Futures (${currentGateFuturesContract})`;
  316. diffChartInstance.data.datasets[2].label = `Spot (${currentGateSpotPair}) vs Futures (${currentGateFuturesContract})`;
  317. updateChartData(diffChartInstance, history.diffs.labels, [history.diffs.oo_vs_spot, history.diffs.oo_vs_futures, history.diffs.spot_vs_futures]);
  318. }
  319. })
  320. .catch(error => {
  321. console.error('Error fetching data for all platforms:', error);
  322. // Display a general error, or individual errors if available.
  323. });
  324. }
  325. function togglePauseResume() { /* (Same as before) */
  326. isPaused = !isPaused;
  327. if (isPaused) {
  328. clearInterval(dataUpdateIntervalID); dataUpdateIntervalID = null;
  329. pauseResumeButton.textContent = '继续刷新'; pauseResumeButton.classList.add('pause-button-active');
  330. } else {
  331. pauseResumeButton.textContent = '暂停刷新'; pauseResumeButton.classList.remove('pause-button-active');
  332. updateDisplayAndCharts();
  333. dataUpdateIntervalID = setInterval(updateDisplayAndCharts, REFRESH_INTERVAL_MS);
  334. }
  335. }
  336. function resetAllZooms() { /* (Same as before, ensure it updates both charts) */
  337. if (priceChartInstance) {
  338. priceChartInstance.resetZoom(); priceChartInstance.options.scales.x.min = undefined; priceChartInstance.options.scales.x.max = undefined;
  339. }
  340. if (diffChartInstance) {
  341. diffChartInstance.resetZoom(); diffChartInstance.options.scales.x.min = undefined; diffChartInstance.options.scales.x.max = undefined;
  342. }
  343. if(isPaused){ updateDisplayAndCharts(); }
  344. else {
  345. if (priceChartInstance) priceChartInstance.update('none');
  346. if (diffChartInstance) diffChartInstance.update('none');
  347. }
  348. }
  349. pauseResumeButton.addEventListener('click', togglePauseResume);
  350. document.getElementById('reset-price-zoom-button').addEventListener('click', resetAllZooms);
  351. document.getElementById('reset-diff-zoom-button').addEventListener('click', resetAllZooms);
  352. updateDisplayAndCharts(); // Initial load
  353. if (!isPaused) {
  354. dataUpdateIntervalID = setInterval(updateDisplayAndCharts, REFRESH_INTERVAL_MS);
  355. }
  356. </script>
  357. </body>
  358. </html>