From 9aa2546e1bd938b27861ae46135a18fa3db91928 Mon Sep 17 00:00:00 2001 From: dvelo <52332868+DeveloLongScript@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:20:17 -0500 Subject: [PATCH] fix: i cant ditch inngest :( --- README.md | 1 + src/pages/api/inngest.ts | 124 +++++++++++++++++++++++++++ src/pages/api/v1/cron/periodic-st.ts | 74 ---------------- vercel.json | 8 +- 4 files changed, 126 insertions(+), 81 deletions(-) create mode 100644 src/pages/api/inngest.ts delete mode 100644 src/pages/api/v1/cron/periodic-st.ts diff --git a/README.md b/README.md index a1c2835..3b484ce 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ First, you must supply the following services with API keys: - [Clerk](https://clerk.com): Create an app and put the respective keys in `.env.local` - MongoDB: Create a database, can be anywhere, and put the location to connect in `.env.local` for the key `MONGO_DB` (this isn't required by any means, but if you want to store any short term or historical data, use this.) +- Inngest: Inngest is a smaller library, but runs the `cron` jobs which will make servers automaticly get added to the database. _This project uses `yarn` as the main package manager. If `package-lock.json` is present, your pull request will get denied._ Second, run `yarn` and `yarn build`. To start the app, run `yarn start`. diff --git a/src/pages/api/inngest.ts b/src/pages/api/inngest.ts new file mode 100644 index 0000000..5b92d11 --- /dev/null +++ b/src/pages/api/inngest.ts @@ -0,0 +1,124 @@ +// inngest in mh-stats provides information periodicly. like every 30 minutes for historical data. +// its fully automatic + +import Favorites from "@/app/account/favorites/page"; +import { OnlineServer } from "@/lib/types/server"; +import { Inngest } from "inngest"; +import { serve } from "inngest/next"; +import { MongoClient } from "mongodb"; +import { Noto_Sans_Mahajani } from "next/font/google"; + +// Create a client to send and receive events +export const inngest = new Inngest({ id: "my-app" }); + +// Create an API that serves zero functions +export default serve({ + client: inngest, + functions: [ + inngest.createFunction( + { id: "every-30-min" }, + [{ cron: "*/30 * * * *" }, { event: "test/30-min" }], + async ({ event, step }) => { + const mongo = new MongoClient(process.env.MONGO_DB as string); + try { + const mh = await ( + await fetch("https://api.minehut.com/servers", { + headers: { + accept: "*/*", + "accept-language": Math.random().toString(), + priority: "u=1, i", + "sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": '"macOS"', + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "cross-site", + Referer: "http://localhost:3000/", + "Referrer-Policy": "strict-origin-when-cross-origin", + }, + body: null, + method: "GET", + }) + ).json(); + + const mha = mongo.db("mhsf").collection("mh"); + const meta = mongo.db("mhsf").collection("meta"); + const dbl = mongo.db("mhsf").collection("history"); + mha.insertOne({ + total_players: mh.total_players, + total_servers: mh.total_servers, + unix: Date.now(), + }); + mh.servers.forEach(async (server: OnlineServer, i: number) => { + const favorites = (async () => { + const result = await meta.find({ server: server.name }).toArray(); + if (result.length == 0) { + return 0; + } + return result[0].favorites; + })(); + const result = await favorites; + + dbl.insertOne({ + player_count: server.playerData.playerCount, + favorites: result, + server: server.name, + time: Date.now(), + }); + + if (i == mh.servers.length) { + mongo.close(); + + return { + event, + body: "Finished adding " + mh.servers.length + " servers.", + }; + } + }); + } catch (e) { + mongo.close(); + return { event, body: "Cloudflare.. aborting " + e }; + } + } + ), + inngest.createFunction( + { id: "every-two-months" }, + [{ cron: "0 0 1 */2 *" }], + async ({ event, step }) => { + const mongo = new MongoClient(process.env.MONGO_DB as string); + const meta = mongo.db("mhsf").collection("history"); + const historical = mongo.db("mhsf").collection("historical"); + + const array = await meta.find().toArray(); + const result: any = {}; + + array.forEach((c) => { + if (result[c.server] == undefined) { + result[c.server] = { + server: c.server, + player_count: [c.player_count], + favorites: [c.favorites], + time: Date.now(), + }; + } else { + result[c.server] = { + server: c.server, + player_count: [...result[c.server]?.player_count, c.player_count], + favorites: [...result[c.server]?.favorites, c.favorites], + time: c.time, + }; + } + }); + + historical.insertMany(Object.values(result)); + meta.drop(); + mongo.close(); + + return { + event, + body: "Dropped database. ", + }; + } + ), + ], +}); diff --git a/src/pages/api/v1/cron/periodic-st.ts b/src/pages/api/v1/cron/periodic-st.ts deleted file mode 100644 index f863a9d..0000000 --- a/src/pages/api/v1/cron/periodic-st.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { OnlineServer } from "@/lib/types/server"; -import { MongoClient } from "mongodb"; -import { NextApiRequest, NextApiResponse } from "next"; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse -) { - if (req.headers.Authorization !== `Bearer ${process.env.CRON_SECRET}`) { - return res.status(401).end("Unauthorized"); - } - - const mongo = new MongoClient(process.env.MONGO_DB as string); - try { - const mh = await ( - await fetch("https://api.minehut.com/servers", { - headers: { - accept: "*/*", - "accept-language": Math.random().toString(), - priority: "u=1, i", - "sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"', - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-platform": '"macOS"', - "sec-fetch-dest": "empty", - "sec-fetch-mode": "cors", - "sec-fetch-site": "cross-site", - Referer: "http://localhost:3000/", - "Referrer-Policy": "strict-origin-when-cross-origin", - }, - body: null, - method: "GET", - }) - ).json(); - - const mha = mongo.db("mhsf").collection("mh"); - const meta = mongo.db("mhsf").collection("meta"); - const dbl = mongo.db("mhsf").collection("history"); - mha.insertOne({ - total_players: mh.total_players, - total_servers: mh.total_servers, - unix: Date.now(), - }); - mh.servers.forEach(async (server: OnlineServer, i: number) => { - const favorites = (async () => { - const result = await meta.find({ server: server.name }).toArray(); - if (result.length == 0) { - return 0; - } - return result[0].favorites; - })(); - const result = await favorites; - - dbl.insertOne({ - player_count: server.playerData.playerCount, - favorites: result, - server: server.name, - time: Date.now(), - }); - - if (i == mh.servers.length) { - mongo.close(); - - return res.send({ - body: "Finished adding " + mh.servers.length + " servers.", - }); - } - }); - } catch (e) { - mongo.close(); - return res.send({ - body: "Cloudflare interferred", - }); - } -} diff --git a/vercel.json b/vercel.json index 9dde3a8..27ddd6f 100644 --- a/vercel.json +++ b/vercel.json @@ -1,9 +1,3 @@ { - "crons": [ - { - "path": "/api/v1/cron/periodic-st", - "schedule": "*/30 * * * *" - }, - { "path": "/api/v1/cron/data-cleanup", "schedule": "0 0 1 */2 *" } - ] + "crons": [{ "path": "/api/v1/cron/data-cleanup", "schedule": "0 0 1 */2 *" }] }