68 lines
2.2 KiB
JavaScript
68 lines
2.2 KiB
JavaScript
function json(data, status = 200) {
|
|
return new Response(JSON.stringify(data), {
|
|
status,
|
|
headers: { "content-type": "application/json; charset=UTF-8" },
|
|
});
|
|
}
|
|
|
|
function modelItem(prediction, probability, isCorrect) {
|
|
if (prediction === null || prediction === undefined) return null;
|
|
return {
|
|
prediction: Number(prediction),
|
|
probability: probability === null || probability === undefined ? null : Number(probability),
|
|
is_correct: isCorrect === null || isCorrect === undefined ? null : Number(isCorrect) === 1,
|
|
};
|
|
}
|
|
|
|
export async function onRequestGet(context) {
|
|
const db = context.env.DB;
|
|
if (!db) {
|
|
return json({ success: false, error: "缺少 D1 绑定 DB" }, 500);
|
|
}
|
|
|
|
try {
|
|
const date = context.params.date;
|
|
const sql = `
|
|
SELECT
|
|
timestamp,
|
|
hour,
|
|
open_price,
|
|
close_price,
|
|
actual_result,
|
|
dlinear_prediction,
|
|
dlinear_prob,
|
|
dlinear_correct,
|
|
nbeats_prediction,
|
|
nbeats_prob,
|
|
nbeats_correct,
|
|
ttm_prediction,
|
|
ttm_prob,
|
|
ttm_correct,
|
|
ensemble_prediction,
|
|
ensemble_correct
|
|
FROM backup_hourly_model_predictions
|
|
WHERE date = ?
|
|
ORDER BY hour ASC
|
|
`;
|
|
|
|
const result = await db.prepare(sql).bind(date).all();
|
|
const rows = (result.results || []).map((r) => ({
|
|
timestamp_cst: r.timestamp,
|
|
hour: r.hour,
|
|
open_price: r.open_price === null || r.open_price === undefined ? null : Number(r.open_price),
|
|
close_price: r.close_price === null || r.close_price === undefined ? null : Number(r.close_price),
|
|
actual_result: r.actual_result === null || r.actual_result === undefined ? null : Number(r.actual_result),
|
|
models: {
|
|
Ensemble: modelItem(r.ensemble_prediction, null, r.ensemble_correct),
|
|
DLinear: modelItem(r.dlinear_prediction, r.dlinear_prob, r.dlinear_correct),
|
|
"N-BEATS": modelItem(r.nbeats_prediction, r.nbeats_prob, r.nbeats_correct),
|
|
"TTM-R2": modelItem(r.ttm_prediction, r.ttm_prob, r.ttm_correct),
|
|
},
|
|
}));
|
|
|
|
return json({ success: true, date, data: rows });
|
|
} catch (error) {
|
|
return json({ success: false, error: String(error) }, 500);
|
|
}
|
|
}
|