metaforecast/src/backend/frontpage.ts

69 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-21 21:53:16 +00:00
import { pgRead, readWritePool } from './database/pg-wrapper';
2022-03-16 21:02:34 +00:00
export async function getFrontpageRaw() {
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(
"SELECT frontpage_sliced FROM latest.frontpage ORDER BY id DESC LIMIT 1"
);
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 getFrontpageFullRaw() {
2022-03-25 23:51:11 +00:00
const client = await readWritePool.connect();
const res = await client.query(
"SELECT frontpage_full FROM latest.frontpage ORDER BY id DESC LIMIT 1"
);
if (!res.rows.length) return [];
console.log(res.rows[0]);
return res.rows[0].frontpage_full;
2022-03-16 21:02:34 +00:00
}
export async function getFrontpage() {
let frontPageForecastsCompatibleWithFuse = [];
try {
let data = await getFrontpageRaw();
frontPageForecastsCompatibleWithFuse = data.map((result) => ({
item: result,
score: 0,
}));
return frontPageForecastsCompatibleWithFuse;
} catch (error) {
console.log(error);
} finally {
return frontPageForecastsCompatibleWithFuse;
}
}
2022-03-25 23:51:11 +00:00
export async function rebuildFrontpage() {
const frontpageFull = await pgRead({
schema: "latest",
tableName: "combined",
});
const client = await readWritePool.connect();
const frontpageSliced = (
await client.query(`
SELECT * FROM latest.combined
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(
"INSERT INTO latest.frontpage(frontpage_full, frontpage_sliced) VALUES($1, $2)",
[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.`
);
}