63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import { lakesConfig } from './lakesConfig';
|
|
|
|
interface DataRecord {
|
|
timestamp: string;
|
|
level: number;
|
|
flow: number;
|
|
}
|
|
|
|
const lakes = lakesConfig.map(lake => {
|
|
const [internalId, oid] = lake.id.split('|');
|
|
const DATA_FILE = path.resolve(`public/data/${internalId}.json`);
|
|
|
|
let currentLevel = 0;
|
|
let currentFlow = 0;
|
|
let sparkline: number[] = Array(12).fill(0);
|
|
|
|
if (fs.existsSync(DATA_FILE)) {
|
|
try {
|
|
const data: DataRecord[] = JSON.parse(fs.readFileSync(DATA_FILE, 'utf-8'));
|
|
if (data.length > 0) {
|
|
// Find latest valid record or just the last record
|
|
const lastValidLevelData = [...data].reverse().find(d => d.level !== null && !isNaN(d.level));
|
|
const lastValidFlowData = [...data].reverse().find(d => d.flow !== null && !isNaN(d.flow) && d.flow > 0);
|
|
|
|
currentLevel = lastValidLevelData ? lastValidLevelData.level : 0;
|
|
currentFlow = lastValidFlowData ? lastValidFlowData.flow : 0;
|
|
|
|
// Take up to 12 last records for sparkline
|
|
const recentData = data.slice(-12);
|
|
sparkline = recentData.map(d => (d.flow === null || isNaN(d.flow) ? 0 : d.flow));
|
|
|
|
// Pad with zeros if less than 12
|
|
while (sparkline.length < 12) {
|
|
sparkline.unshift(0);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error(`Error reading data for ${internalId}`, e);
|
|
}
|
|
}
|
|
|
|
return {
|
|
id: lake.id,
|
|
name: lake.text.replace('VD ', '').split('-')[0].trim(),
|
|
river: lake.text.includes('-') ? lake.text.split('-')[1].trim() : '',
|
|
priority: lake.priority || false,
|
|
level: currentLevel.toFixed(2),
|
|
capacity: 0, // Removed fake capacity
|
|
inflow: currentFlow.toFixed(1),
|
|
outflow: currentFlow.toFixed(1),
|
|
volume: lake.maxVolume || 0, // Using real maxVolume if known
|
|
lat: lake.coords[0],
|
|
lng: lake.coords[1],
|
|
sparkline
|
|
};
|
|
});
|
|
|
|
const outputPath = path.resolve(process.cwd(), 'public/data/lakes_index.json');
|
|
fs.writeFileSync(outputPath, JSON.stringify(lakes, null, 2));
|
|
console.log('Real lakes index generated:', lakes.length);
|