debug: add in-memory logs and logs endpoint
Deploy Bürgerwind / deploy (push) Successful in 16s
Details
Deploy Bürgerwind / deploy (push) Successful in 16s
Details
This commit is contained in:
parent
6787290554
commit
2a3e5a9fdf
30
server.js
30
server.js
|
|
@ -22,10 +22,14 @@ const pool = new Pool({
|
||||||
password: process.env.DB_PASSWORD
|
password: process.env.DB_PASSWORD
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test DB Connection
|
// In-memory logs for remote debugging
|
||||||
pool.connect()
|
const serverLogs = [];
|
||||||
.then(() => console.log('Connected to PostgreSQL successfully'))
|
function log(msg) {
|
||||||
.catch(err => console.error('Connection error', err.stack));
|
const entry = `[${new Date().toISOString()}] ${msg}`;
|
||||||
|
console.log(entry);
|
||||||
|
serverLogs.push(entry);
|
||||||
|
if (serverLogs.length > 50) serverLogs.shift();
|
||||||
|
}
|
||||||
|
|
||||||
// Status-Endpunkt für Diagnose
|
// Status-Endpunkt für Diagnose
|
||||||
app.get('/api/status', async (req, res) => {
|
app.get('/api/status', async (req, res) => {
|
||||||
|
|
@ -42,27 +46,33 @@ app.get('/api/status', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('/api/logs', (req, res) => {
|
||||||
|
res.type('text/plain').send(serverLogs.join('\n'));
|
||||||
|
});
|
||||||
|
|
||||||
// API to save turbines
|
// API to save turbines
|
||||||
app.post('/api/wea', async (req, res) => {
|
app.post('/api/wea', async (req, res) => {
|
||||||
const { projekt_id, turbines } = req.body;
|
const { projekt_id, turbines } = req.body;
|
||||||
const targetProject = projekt_id || 'BWSamern-Ohne';
|
const targetProject = projekt_id || 'BWSamern-Ohne';
|
||||||
const schema = process.env.DB_SCHEMA || 'geodaten';
|
const schema = process.env.DB_SCHEMA || 'geodaten';
|
||||||
|
|
||||||
console.log(`Empfange Save-Request für Projekt: ${targetProject} (${turbines?.length || 0} WEAs)`);
|
log(`Empfange Save-Request für Projekt: ${targetProject} (${turbines?.length || 0} WEAs)`);
|
||||||
|
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
try {
|
try {
|
||||||
await client.query('BEGIN');
|
await client.query('BEGIN');
|
||||||
|
|
||||||
// 1. Bestehende WEAs für dieses Projekt löschen
|
// 1. Bestehende WEAs für dieses Projekt löschen
|
||||||
await client.query(
|
const delRes = await client.query(
|
||||||
`DELETE FROM ${schema}.wea_standorte WHERE projekt_id = $1`,
|
`DELETE FROM ${schema}.wea_standorte WHERE projekt_id = $1`,
|
||||||
[targetProject]
|
[targetProject]
|
||||||
);
|
);
|
||||||
|
log(`Gelöscht: ${delRes.rowCount} bestehende Anlagen.`);
|
||||||
|
|
||||||
// 2. Neue WEAs einfügen
|
// 2. Neue WEAs einfügen
|
||||||
if (turbines && turbines.length > 0) {
|
if (turbines && turbines.length > 0) {
|
||||||
for (const t of turbines) {
|
for (const t of turbines) {
|
||||||
|
log(`Füge WEA ein: Nr=${t.nr}, Typ=${t.type}, Pos=${t.latlng?.lat},${t.latlng?.lng}`);
|
||||||
await client.query(
|
await client.query(
|
||||||
`INSERT INTO ${schema}.wea_standorte
|
`INSERT INTO ${schema}.wea_standorte
|
||||||
(projekt_id, wea_nummer, anlagentyp, nabenhoehe, rotordurchmesser, geom)
|
(projekt_id, wea_nummer, anlagentyp, nabenhoehe, rotordurchmesser, geom)
|
||||||
|
|
@ -73,11 +83,11 @@ app.post('/api/wea', async (req, res) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
await client.query('COMMIT');
|
await client.query('COMMIT');
|
||||||
console.log(`Erfolgreich ${turbines?.length || 0} WEAs gespeichert.`);
|
log(`Erfolgreich gespeichert: ${turbines?.length || 0} WEAs.`);
|
||||||
res.json({ message: `${turbines?.length || 0} WEAs erfolgreich in Datenbank gespeichert.` });
|
res.json({ message: `${turbines?.length || 0} WEAs erfolgreich in Datenbank gespeichert.` });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await client.query('ROLLBACK');
|
await client.query('ROLLBACK');
|
||||||
console.error('Fehler beim Speichern in DB:', err);
|
log(`FEHLER beim Speichern: ${err.message}`);
|
||||||
res.status(500).json({ error: 'Datenbankfehler beim Speichern', details: err.message });
|
res.status(500).json({ error: 'Datenbankfehler beim Speichern', details: err.message });
|
||||||
} finally {
|
} finally {
|
||||||
client.release();
|
client.release();
|
||||||
|
|
@ -87,6 +97,7 @@ app.post('/api/wea', async (req, res) => {
|
||||||
// API to load turbines
|
// API to load turbines
|
||||||
app.get('/api/wea/:projekt_id', async (req, res) => {
|
app.get('/api/wea/:projekt_id', async (req, res) => {
|
||||||
const { projekt_id } = req.params;
|
const { projekt_id } = req.params;
|
||||||
|
log(`Lade WEAs für Projekt: ${projekt_id}`);
|
||||||
try {
|
try {
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
`SELECT wea_nummer as nr, anlagentyp as type, nabenhoehe as hh, rotordurchmesser as rd, ksf_drehung as ksfAngle,
|
`SELECT wea_nummer as nr, anlagentyp as type, nabenhoehe as hh, rotordurchmesser as rd, ksf_drehung as ksfAngle,
|
||||||
|
|
@ -94,9 +105,10 @@ app.get('/api/wea/:projekt_id', async (req, res) => {
|
||||||
FROM geodaten.wea_standorte WHERE projekt_id = $1`,
|
FROM geodaten.wea_standorte WHERE projekt_id = $1`,
|
||||||
[projekt_id]
|
[projekt_id]
|
||||||
);
|
);
|
||||||
|
log(`Geladen: ${result.rowCount} Anlagen.`);
|
||||||
res.status(200).json(result.rows);
|
res.status(200).json(result.rows);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error loading turbines', e);
|
log(`FEHLER beim Laden: ${e.message}`);
|
||||||
res.status(500).json({ error: e.message });
|
res.status(500).json({ error: e.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue