metaforecast/src/backend/frontpage.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

import { pgRead, readWritePool } from "./database/pg-wrapper";
import { Forecast } from "./platforms";
2022-03-16 21:02:34 +00:00
export async function getFrontpage(): Promise<Forecast[]> {
2022-03-21 21:53:16 +00:00
const client = await readWritePool.connect();
2022-03-25 23:51:11 +00:00
const res = await client.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 [];
console.log(res.rows[0].frontpage_sliced);
return res.rows[0].frontpage_sliced;
2022-03-16 21:02:34 +00:00
}
export async function getFrontpageFull(): Promise<Forecast[]> {
2022-03-25 23:51:11 +00:00
const client = await readWritePool.connect();
const res = await client.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 [];
console.log(res.rows[0]);
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({
tableName: "questions",
2022-03-25 23:51:11 +00:00
});
const client = await readWritePool.connect();
const frontpageSliced = (
await client.query(`
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();
await client.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.`
);
}