-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanualInsert.ts
84 lines (69 loc) · 2.39 KB
/
manualInsert.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
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
import { createClient } from "@supabase/supabase-js";
import { getBeatmapSet } from "./osuRequests";
import { Database } from "./database.types";
import {
adjustRankDates,
calcEarlyProbability,
getFormattedMapsFromDatabase,
getUpdatedMaps,
storeMapProperties,
} from "./osuHelpers";
import { Redis } from "@upstash/redis";
require("dotenv").config();
const supabase = createClient<Database>(
process.env.SUPABASE_URL!,
process.env.SERVICE_ROLE!,
);
const insertMap = async (beatmapSetId: number) => {
var { data: res, error } = await supabase.from("app_data").select("*");
if (!res || error) throw new Error(`failed to get app_data. Error: ${error}`);
const appData = res[0];
const { qualifiedMaps, rankedMaps, qualifiedData } =
await getFormattedMapsFromDatabase(supabase);
const previousData = storeMapProperties(qualifiedData);
const newBeatmapSet = await getBeatmapSet(appData.access_token, beatmapSetId);
if (newBeatmapSet.queueDate) {
qualifiedMaps[newBeatmapSet.mode] = qualifiedMaps[newBeatmapSet.mode]
.filter((beatmapSet) => beatmapSet.id !== newBeatmapSet.id);
qualifiedMaps[newBeatmapSet.mode].push(newBeatmapSet);
qualifiedMaps[newBeatmapSet.mode].sort((a, b) =>
a.queueDate!.getTime() - b.queueDate!.getTime()
);
} else {
rankedMaps[newBeatmapSet.mode] = rankedMaps[newBeatmapSet.mode].filter(
(beatmapSet) => beatmapSet.id !== newBeatmapSet.id,
);
rankedMaps[newBeatmapSet.mode].push(newBeatmapSet);
rankedMaps[newBeatmapSet.mode].sort((a, b) =>
a.rankDate!.getTime() - b.rankDate!.getTime()
);
}
adjustRankDates(
qualifiedMaps[newBeatmapSet.mode],
rankedMaps[newBeatmapSet.mode],
);
calcEarlyProbability(qualifiedMaps);
const { mapsToUpdate, updatedMapIds } = getUpdatedMaps(
qualifiedMaps,
previousData,
);
const { error: errorBeatmapSets } = await supabase.from("beatmapsets").upsert(
mapsToUpdate,
);
if (errorBeatmapSets) console.log(errorBeatmapSets);
if (updatedMapIds.length > 0) {
const redis = Redis.fromEnv();
const timestamp = Date.now();
redis.set(`updates-${timestamp}`, JSON.stringify(mapsToUpdate), { ex: 60 });
const { error } = await supabase
.from("updates")
.upsert({
id: 1,
timestamp,
updated_maps: updatedMapIds,
deleted_maps: [],
});
if (error) console.log(error);
}
};
insertMap(0);