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();
|
|
|
|
const res = await client.query(
|
|
|
|
"SELECT frontpage_sliced FROM latest.frontpage ORDER BY id DESC LIMIT 1"
|
|
|
|
);
|
|
|
|
if (!res.rows.length) return [];
|
|
|
|
return res.rows[0].frontpage_sliced;
|
2022-03-16 21:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getFrontpageFullRaw() {
|
2022-03-21 21:53:16 +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 [];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
let shuffle = (array) => {
|
|
|
|
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
|
|
|
|
let currentIndex = array.length,
|
|
|
|
randomIndex;
|
|
|
|
|
|
|
|
// While there remain elements to shuffle...
|
|
|
|
while (currentIndex != 0) {
|
|
|
|
// Pick a remaining element...
|
|
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
|
|
currentIndex--;
|
|
|
|
|
|
|
|
// And swap it with the current element.
|
|
|
|
[array[currentIndex], array[randomIndex]] = [
|
|
|
|
array[randomIndex],
|
|
|
|
array[currentIndex],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Main
|
|
|
|
export async function downloadFrontpage() {
|
|
|
|
let init = Date.now();
|
|
|
|
|
2022-03-21 21:53:16 +00:00
|
|
|
const frontpageFull = await pgRead({
|
|
|
|
schema: "latest",
|
|
|
|
tableName: "combined",
|
|
|
|
});
|
2022-03-16 21:02:34 +00:00
|
|
|
|
2022-03-21 21:53:16 +00:00
|
|
|
let frontpageFiltered = frontpageFull.filter(
|
2022-03-16 21:02:34 +00:00
|
|
|
(forecast) =>
|
|
|
|
forecast.qualityindicators &&
|
|
|
|
forecast.qualityindicators.stars >= 3 &&
|
|
|
|
forecast.options &&
|
|
|
|
forecast.options.length > 0 &&
|
|
|
|
forecast.description != ""
|
|
|
|
);
|
2022-03-21 21:53:16 +00:00
|
|
|
let frontpageSliced = shuffle(frontpageFiltered).slice(0, 50);
|
|
|
|
|
|
|
|
const client = await readWritePool.connect();
|
|
|
|
await client.query(
|
|
|
|
"INSERT INTO latest.frontpage(frontpage_full, frontpage_sliced) VALUES($1, $2)",
|
|
|
|
[JSON.stringify(frontpageFull), JSON.stringify(frontpageSliced)]
|
2022-03-16 21:02:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let end = Date.now();
|
|
|
|
let difference = end - init;
|
|
|
|
console.log(
|
|
|
|
`Took ${difference / 1000} seconds, or ${difference / (1000 * 60)} minutes.`
|
|
|
|
);
|
|
|
|
}
|