From a805914fe6de1725ba739db67edc28c6269f811d Mon Sep 17 00:00:00 2001 From: Anton Volnuhin Date: Thu, 29 Jan 2026 13:44:41 +0300 Subject: [PATCH] automatic months --- app.js | 15 +++++++++++---- server.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 48736dc..391f2a8 100644 --- a/app.js +++ b/app.js @@ -875,9 +875,16 @@ function loadTinyColor() { async function loadAvailableMonths() { // Load TinyColor first await loadTinyColor(); - - // Include all available months - const months = ['2025-01', '2025-02']; + + // Fetch available months from server + const response = await fetch('/api/months'); + const months = await response.json(); + + if (months.length === 0) { + console.error('No month data files found'); + return; + } + const select = document.getElementById('month-select'); // Clear existing options @@ -1270,4 +1277,4 @@ function setupHoverEvents(sunburstData) { // Reset to default view when a section is no longer emphasized showDefaultView(); }); -} \ No newline at end of file +} diff --git a/server.js b/server.js index e5ee288..539cfc9 100644 --- a/server.js +++ b/server.js @@ -13,15 +13,40 @@ const MIME_TYPES = { }; const server = http.createServer((req, res) => { + // API endpoint to list available months + if (req.url === '/api/months') { + fs.readdir('.', (err, files) => { + if (err) { + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Failed to read directory' })); + return; + } + + // Find CSV files matching altcats-YYYY-MM.csv pattern + const monthPattern = /^altcats-(\d{4}-\d{2})\.csv$/; + const months = files + .map(file => { + const match = file.match(monthPattern); + return match ? match[1] : null; + }) + .filter(Boolean) + .sort(); + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(months)); + }); + return; + } + // Handle URL let filePath = '.' + req.url; if (filePath === './') { filePath = './index.html'; } - + const extname = path.extname(filePath); const contentType = MIME_TYPES[extname] || 'application/octet-stream'; - + fs.readFile(filePath, (err, data) => { if (err) { if (err.code === 'ENOENT') { @@ -33,7 +58,7 @@ const server = http.createServer((req, res) => { } return; } - + res.writeHead(200, { 'Content-Type': contentType }); res.end(data); });