diff --git a/src/lib/server/external/api.scraping.helpers.ts b/src/lib/server/external/api.scraping.helpers.ts index 9cb5436..488f69c 100755 --- a/src/lib/server/external/api.scraping.helpers.ts +++ b/src/lib/server/external/api.scraping.helpers.ts @@ -5,7 +5,6 @@ import { rng } from "$lib/utils/rng"; export const testIfSessionIsValid = async (jwt: string) => { try { - console.log("Testing session validity...", jwt); const res = await fetch( `${constants.SCRAP_API_URL}/v1/user/get-balance?userId=6339`, { diff --git a/src/lib/trpc/routers/postdata.router.ts b/src/lib/trpc/routers/postdata.router.ts index 4b021c1..2c79e3b 100644 --- a/src/lib/trpc/routers/postdata.router.ts +++ b/src/lib/trpc/routers/postdata.router.ts @@ -33,6 +33,18 @@ async function removePostSession() { await redis.del(constants.POST_SESSION_KEY); } +const postDataCacheStore = { + get: async (key: string) => { + return JSON.parse((await redis.get(`pdcc:${key}`)) ?? "[]"); + }, + set: async (key: string, data: any) => { + await redis.setex(`pdcc:${key}`, 60 * 60 * 1, JSON.stringify(data)); + }, + del: async (key: string) => { + await redis.del(`pdcc:${key}`); + }, +}; + export const postDataApiRouter = createTRPCRouter({ fetchPostDataHistory: protectedProcedure .input(zPostDataHistoryFilters) @@ -55,12 +67,14 @@ export const postDataApiRouter = createTRPCRouter({ .input(zPostDataFilters) .query(async ({ input }) => { const date = input.date; + const cacheKey = getULID(); if (!input.draw) { return { ok: false, detail: "Draw is required", data: [], errors: undefined, + key: cacheKey, }; } @@ -69,24 +83,26 @@ export const postDataApiRouter = createTRPCRouter({ await dbApiUser.getAllPostUsersWithParentUsers(), ); if (!balOut.ok || !balOut.data) { - return { ok: false, detail: balOut.detail, data: [], users: [] }; + return { + ok: false, + key: cacheKey, + detail: balOut.detail, + data: [], + users: [], + }; } const users = balOut.data; console.log(`[=] ${users.length} users found`); console.log(users); const result = await fetchDataForPosting(date, input, users); + postDataCacheStore.set(cacheKey, result.data).catch(console.error); console.log("result.data.length = ", result.data.length); - return result; + return { ...result, key: cacheKey }; }), post: protectedProcedure - .input( - z.object({ - yes: zPostDataFilters, - data: z.array(zPostDataEntry), - }), - ) + .input(z.object({ yes: zPostDataFilters, cachedDataKey: z.string() })) .mutation(async ({ input }) => { if (await hasPostSession()) { return { @@ -146,8 +162,9 @@ export const postDataApiRouter = createTRPCRouter({ }; } - let data = input.data; - if (input.data.length < 1) { + let data: any[] = await postDataCacheStore.get(input.cachedDataKey); + console.log("cached.data.length = ", data.length); + if (data.length < 1) { console.log("No data found from preview, generating a list"); const _out = await fetchDataForPosting(date, input.yes, balOut.data); if (!_out.ok) { @@ -155,6 +172,7 @@ export const postDataApiRouter = createTRPCRouter({ return _out; } data = _out.data; + console.log("data.length = ", data.length); } if (data.length < 1) { @@ -168,9 +186,8 @@ export const postDataApiRouter = createTRPCRouter({ }; } - console.log(`[+] Posting ${input.data.length} entries to the API`); + console.log(`[+] Posting ${data.length} entries to the API`); - console.time("Time taken to post data to the API"); const res = await postDataToApi({ sessions: userSessions, data, @@ -200,6 +217,7 @@ export const postDataApiRouter = createTRPCRouter({ console.log("[+] Data saved to the database"); + await postDataCacheStore.del(input.cachedDataKey); await removePostSession(); return { ok: true, diff --git a/src/routes/admin/fs-modal.svelte b/src/routes/admin/fs-modal.svelte index 110b8be..da0bdfb 100755 --- a/src/routes/admin/fs-modal.svelte +++ b/src/routes/admin/fs-modal.svelte @@ -13,9 +13,8 @@ import toast from "svelte-french-toast"; import type { CopyCounts } from "./copy-counts.ls"; import { filters } from "./fs.stores"; - import { nowDT, postdata } from "./stores"; + import { postDataKey } from "./stores"; import type { TRPC } from "$lib/trpc/trpc"; - import { hasDrawBeenClosed } from "$lib/utils"; import PostDataSummarySection from "./post-data-summary-section.svelte"; import * as AlertDialog from "$lib/components/ui/alert-dialog/index"; @@ -35,7 +34,6 @@ ? $hasAlreadyPostedQ.data.hasPosted : false; - // $: hasTimePassed = hasDrawBeenClosed($filters.date, $filters.draw!, $nowDT); $: hasTimePassed = false; let postUsersQ = api.apiUser.getAllPostUsers.createQuery(undefined, { @@ -101,7 +99,7 @@ second: $filters.draw?.abcRateS ?? 0, }, }, - data: $postdata, + cachedDataKey: $postDataKey, }); } @@ -551,7 +549,8 @@