2022-03-28 17:59:07 +00:00
|
|
|
import { pgRead, readWritePool } from "./database/pg-wrapper";
|
2022-04-01 20:24:35 +00:00
|
|
|
import { Forecast } from "./platforms";
|
2022-03-16 21:02:34 +00:00
|
|
|
|
2022-04-01 20:24:35 +00:00
|
|
|
export async function getFrontpage(): Promise<Forecast[]> {
|
2022-04-05 23:16:55 +00:00
|
|
|
const res = await readWritePool.query(
|
2022-03-30 10:53:22 +00:00
|
|
|
"SELECT frontpage_sliced FROM frontpage ORDER BY id DESC LIMIT 1"
|
2022-03-25 23:51:11 +00:00
|
|
|
);
|
|
|
|
if (!res.rows.length) return [];
|
|
|
|
return res.rows[0].frontpage_sliced;
|
2022-03-16 21:02:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:24:35 +00:00
|
|
|
export async function getFrontpageFull(): Promise<Forecast[]> {
|
2022-04-05 23:16:55 +00:00
|
|
|
const res = await readWritePool.query(
|
2022-03-30 10:53:22 +00:00
|
|
|
"SELECT frontpage_full FROM frontpage ORDER BY id DESC LIMIT 1"
|
2022-03-25 23:51:11 +00:00
|
|
|
);
|
|
|
|
if (!res.rows.length) return [];
|
|
|
|
return res.rows[0].frontpage_full;
|
2022-03-16 21:02:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:51:11 +00:00
|
|
|
export async function rebuildFrontpage() {
|
|
|
|
const frontpageFull = await pgRead({
|
2022-04-01 20:24:35 +00:00
|
|
|
tableName: "questions",
|
2022-03-25 23:51:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const frontpageSliced = (
|
2022-04-05 23:16:55 +00:00
|
|
|
await readWritePool.query(`
|
2022-04-01 20:24:35 +00:00
|
|
|
SELECT * FROM questions
|
2022-03-25 23:51:11 +00:00
|
|
|
WHERE
|
|
|
|
(qualityindicators->>'stars')::int >= 3
|
|
|
|
AND description != ''
|
|
|
|
AND JSON_ARRAY_LENGTH(options) > 0
|
|
|
|
ORDER BY RANDOM() LIMIT 50
|
|
|
|
`)
|
|
|
|
).rows;
|
|
|
|
|
|
|
|
const start = Date.now();
|
2022-04-05 23:16:55 +00:00
|
|
|
await readWritePool.query(
|
2022-03-30 10:53:22 +00:00
|
|
|
"INSERT INTO frontpage(frontpage_full, frontpage_sliced) VALUES($1, $2)",
|
2022-03-25 23:51:11 +00:00
|
|
|
[JSON.stringify(frontpageFull), JSON.stringify(frontpageSliced)]
|
|
|
|
);
|
|
|
|
|
|
|
|
const end = Date.now();
|
|
|
|
const difference = end - start;
|
|
|
|
console.log(
|
|
|
|
`Took ${difference / 1000} seconds, or ${difference / (1000 * 60)} minutes.`
|
|
|
|
);
|
|
|
|
}
|