-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstrumentation.ts
40 lines (36 loc) · 1.13 KB
/
instrumentation.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
import { cache, ratingCache } from "./lib/utils.server";
import ora from "ora";
interface CFUser {
handle: string;
maxRating: number;
}
async function ratingsTask(){
const resp = await fetch(`https://codeforces.com/api/user.info?handles=${Object.keys(ratingCache).join(";")}`);
let data;
try {
data = await resp.json();
} catch {
console.error("[cf] ERROR: Codeforces API down");
return;
}
if (data.status !== "OK") {
console.error(`[cf] ERROR: ${data.comment}`);
return;
}
Object.assign(ratingCache, Object.fromEntries(data.result.map((user: CFUser) => [user.handle, user.maxRating])));
}
export async function register(){
const instances = Object.values(cache);
const message = `Initializing cache instances [$/${instances.length+1}]`;
const spinner = ora({ text: message.replace("$", "0"), indent: 1 }).start();
const update = () => {
cnt++;
spinner.text = message.replace("$", cnt.toString());
}
let cnt = 0;
await Promise.all([
ratingsTask().then(update),
...instances.map(instance => instance.init().then(update))
]);
spinner.succeed("All cache instances initialized");
}