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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
import { count, eq } from "drizzle-orm";
import { z } from "zod";
import { bookmarkLinks, bookmarks, users } from "@hoarder/db/schema";
import {
LinkCrawlerQueue,
OpenAIQueue,
SearchIndexingQueue,
} from "@hoarder/shared/queues";
import { adminProcedure, router } from "../index";
export const adminAppRouter = router({
stats: adminProcedure
.output(
z.object({
numUsers: z.number(),
numBookmarks: z.number(),
crawlStats: z.object({
queuedInRedis: z.number(),
pending: z.number(),
failed: z.number(),
}),
inferenceStats: z.object({
queuedInRedis: z.number(),
pending: z.number(),
failed: z.number(),
}),
indexingStats: z.object({
queuedInRedis: z.number(),
}),
}),
)
.query(async ({ ctx }) => {
const [
[{ value: numUsers }],
[{ value: numBookmarks }],
// Crawls
pendingCrawlsInRedis,
[{ value: pendingCrawls }],
[{ value: failedCrawls }],
// Indexing
pendingIndexingInRedis,
// Inference
pendingInferenceInRedis,
[{ value: pendingInference }],
[{ value: failedInference }],
] = await Promise.all([
ctx.db.select({ value: count() }).from(users),
ctx.db.select({ value: count() }).from(bookmarks),
// Crawls
LinkCrawlerQueue.getWaitingCount(),
ctx.db
.select({ value: count() })
.from(bookmarkLinks)
.where(eq(bookmarkLinks.crawlStatus, "pending")),
ctx.db
.select({ value: count() })
.from(bookmarkLinks)
.where(eq(bookmarkLinks.crawlStatus, "failure")),
// Indexing
SearchIndexingQueue.getWaitingCount(),
// Inference
OpenAIQueue.getWaitingCount(),
ctx.db
.select({ value: count() })
.from(bookmarks)
.where(eq(bookmarks.taggingStatus, "pending")),
ctx.db
.select({ value: count() })
.from(bookmarks)
.where(eq(bookmarks.taggingStatus, "failure")),
]);
return {
numUsers,
numBookmarks,
crawlStats: {
queuedInRedis: pendingCrawlsInRedis,
pending: pendingCrawls,
failed: failedCrawls,
},
inferenceStats: {
queuedInRedis: pendingInferenceInRedis,
pending: pendingInference,
failed: failedInference,
},
indexingStats: {
queuedInRedis: pendingIndexingInRedis,
},
};
}),
recrawlLinks: adminProcedure
.input(
z.object({
crawlStatus: z.enum(["success", "failure", "all"]),
runInference: z.boolean(),
}),
)
.mutation(async ({ ctx, input }) => {
const bookmarkIds = await ctx.db.query.bookmarkLinks.findMany({
columns: {
id: true,
},
...(input.crawlStatus === "all"
? {}
: { where: eq(bookmarkLinks.crawlStatus, input.crawlStatus) }),
});
await Promise.all(
bookmarkIds.map((b) =>
LinkCrawlerQueue.add("crawl", {
bookmarkId: b.id,
runInference: input.runInference,
}),
),
);
}),
reindexAllBookmarks: adminProcedure.mutation(async ({ ctx }) => {
const bookmarkIds = await ctx.db.query.bookmarks.findMany({
columns: {
id: true,
},
});
await Promise.all(
bookmarkIds.map((b) =>
SearchIndexingQueue.add("search_indexing", {
bookmarkId: b.id,
type: "index",
}),
),
);
}),
});
|