···
160
160
* Store forecasts for a single station by calling the internal store API
161
161
*/
162
162
export async function storeForecastForStation(stationId: string) {
163
163
+
console.log(`[forecast-store] ========================================`);
164
164
+
console.log(`[forecast-store] START: Storing forecasts for station ${stationId}`);
165
165
+
console.log(`[forecast-store] ========================================`);
166
166
+
163
167
try {
164
168
const { getDuckConn } = await import("@/lib/db/duckdb");
165
169
const conn = await getDuckConn();
166
170
const storageDate = new Date().toISOString().split('T')[0];
171
171
+
console.log(`[forecast-store] ✓ Database connection established`);
167
172
168
173
// Create forecast table if not exists
169
174
await conn.run(`
···
181
186
PRIMARY KEY(storage_date, station_id, forecast_date, source)
182
187
)
183
188
`);
189
189
+
console.log(`[forecast-store] ✓ Forecast table created/verified`);
184
190
185
191
// Get station coordinates first
192
192
+
console.log(`[forecast-store] Fetching station metadata...`);
186
193
const stationsResponse = await fetch('https://dataset.api.hub.geosphere.at/v1/station/current/tawes-v1-10min/metadata');
194
194
+
195
195
+
if (!stationsResponse.ok) {
196
196
+
throw new Error(`Failed to fetch station metadata: ${stationsResponse.status} ${stationsResponse.statusText}`);
197
197
+
}
198
198
+
187
199
const stationsData = await stationsResponse.json();
188
200
const station = stationsData.stations.find((s: any) => s.id === stationId);
189
201
190
202
if (!station) {
191
191
-
console.error(`[forecast] Station ${stationId} not found`);
192
192
-
return;
203
203
+
throw new Error(`Station ${stationId} not found in metadata`);
193
204
}
205
205
+
206
206
+
console.log(`[forecast-store] ✓ Station found: ${station.name} (${station.lat}, ${station.lon})`);
207
207
+
194
208
195
209
const lat = station.lat;
196
210
const lon = station.lon;
···
393
407
`, [storageDate, stationId, day.date, 'openmeteo', day.tempMin, day.tempMax, day.precipitation, day.windSpeed, day.windGust]);
394
408
}
395
409
}
396
396
-
} catch (e) {
397
397
-
console.error(`[forecast] Failed to store ${sourceName}:`, e);
410
410
+
} catch (e: any) {
411
411
+
console.error(`[forecast-store] ✗ Failed to store ${sourceName}:`, e?.message || e);
398
412
}
399
413
}
400
400
-
} catch (e) {
401
401
-
console.error("[forecast] Storage failed:", e);
414
414
+
415
415
+
console.log(`[forecast-store] ========================================`);
416
416
+
console.log(`[forecast-store] DONE: Forecasts stored for station ${stationId}`);
417
417
+
console.log(`[forecast-store] ========================================`);
418
418
+
} catch (e: any) {
419
419
+
console.error(`[forecast-store] ========================================`);
420
420
+
console.error(`[forecast-store] ✗✗✗ STORAGE FAILED ✗✗✗`);
421
421
+
console.error(`[forecast-store] ========================================`);
422
422
+
console.error(`[forecast-store] Error:`, e?.message || e);
423
423
+
console.error(`[forecast-store] Stack:`, e?.stack);
402
424
throw e;
403
425
}
404
426
}