-
Notifications
You must be signed in to change notification settings - Fork 6
/
frontpage.ts
41 lines (37 loc) · 1.08 KB
/
frontpage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Question } from "@prisma/client";
import { prisma } from "./database/prisma";
import { measureTime } from "./utils/measureTime";
export async function getFrontpage(): Promise<Question[]> {
const questions = (
await prisma.frontpageId.findMany({
include: {
question: true,
},
})
)
.map((f) => f.question)
.filter((q) => q);
return questions;
}
export async function rebuildFrontpage() {
await measureTime(async () => {
const rows = await prisma.$queryRaw<{ id: string }[]>`
SELECT questions.id FROM questions, history
WHERE
questions.id = history.id
AND (questions.qualityindicators->>'stars')::int >= 3
AND questions.description != ''
AND questions.url NOT LIKE '%kalshi%'
AND JSONB_ARRAY_LENGTH(questions.options) > 0
GROUP BY questions.id
HAVING COUNT(DISTINCT history.fetched) >= 7
ORDER BY RANDOM() LIMIT 50
`;
await prisma.$transaction([
prisma.frontpageId.deleteMany({}),
prisma.frontpageId.createMany({
data: rows,
}),
]);
});
}