diff --git a/data/scores.db b/data/scores.db new file mode 100644 index 0000000..6b014d6 --- /dev/null +++ b/data/scores.db @@ -0,0 +1 @@ +{"$$indexCreated":{"fieldName":"key","unique":true,"sparse":false}} diff --git a/data/transitions.db b/data/transitions.db new file mode 100644 index 0000000..e69de29 diff --git a/main.ts b/main.ts index 730e1c7..b99d0af 100644 --- a/main.ts +++ b/main.ts @@ -8,19 +8,6 @@ const prisma = new PrismaClient(); const redirectUri = "http://localhost:3000/auth/callback"; -const db = { - scores: new Datastore({ filename: "data/scores.db", autoload: true }), - transitions: new Datastore({ - filename: "data/transitions.db", - autoload: true, - }), -}; - -db.scores.ensureIndex({ fieldName: "key", unique: true }); -db.scores.persistence.setAutocompactionInterval(5000); - -// await new Promise((resolve) => db.loadDatabase(resolve)); - async function getUserToken() { const rlInterface = createInterface({ input: process.stdin, @@ -200,17 +187,40 @@ async function scrapeSingle(channelId) { async function scrapeChannels() { const channels: Channel[] = await fetchApi("/chat/channels"); - // // biome-ignore lint/style/noNonNullAssertion: - // const osuChannel = channels.find((channel) => channel.name === "#osu")!; - // const { channel_id: osuChannelId } = osuChannel; + + if (!Array.isArray(channels)) return; + await Promise.all( channels.map((channel) => scrapeSingle(channel.channel_id)), ); } +async function getStats(elapsed) { + const result = await prisma.score.count(); + console.log( + `${result} total scores (prev query: ${Math.round(elapsed / 10) / 100}s)`, + ); + + const result2 = await prisma.$queryRaw` + SELECT + beatmapset_id, artist, title, COUNT(*) as count + FROM Score + JOIN BeatmapSet ON Score.beatmapset_id = BeatmapSet.id + GROUP BY beatmapset_id + ORDER BY count DESC + LIMIT 5; + `; + for (const row of result2) { + console.log(` ${row.count}\t${row.artist} - ${row.title}`); + } +} + async function mainLoop() { while (true) { + const start = performance.now(); await scrapeChannels(); + const end = performance.now(); + await getStats(end - start); await sleep(10000); } }