This commit is contained in:
bootunloader
2024-09-22 13:17:14 +03:00
parent 1c56dc079b
commit c1b5f28a60
5 changed files with 37 additions and 21 deletions

View File

@@ -5,7 +5,6 @@ import { rng } from "$lib/utils/rng";
export const testIfSessionIsValid = async (jwt: string) => { export const testIfSessionIsValid = async (jwt: string) => {
try { try {
console.log("Testing session validity...", jwt);
const res = await fetch( const res = await fetch(
`${constants.SCRAP_API_URL}/v1/user/get-balance?userId=6339`, `${constants.SCRAP_API_URL}/v1/user/get-balance?userId=6339`,
{ {

View File

@@ -33,6 +33,18 @@ async function removePostSession() {
await redis.del(constants.POST_SESSION_KEY); 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({ export const postDataApiRouter = createTRPCRouter({
fetchPostDataHistory: protectedProcedure fetchPostDataHistory: protectedProcedure
.input(zPostDataHistoryFilters) .input(zPostDataHistoryFilters)
@@ -55,12 +67,14 @@ export const postDataApiRouter = createTRPCRouter({
.input(zPostDataFilters) .input(zPostDataFilters)
.query(async ({ input }) => { .query(async ({ input }) => {
const date = input.date; const date = input.date;
const cacheKey = getULID();
if (!input.draw) { if (!input.draw) {
return { return {
ok: false, ok: false,
detail: "Draw is required", detail: "Draw is required",
data: [], data: [],
errors: undefined, errors: undefined,
key: cacheKey,
}; };
} }
@@ -69,24 +83,26 @@ export const postDataApiRouter = createTRPCRouter({
await dbApiUser.getAllPostUsersWithParentUsers(), await dbApiUser.getAllPostUsersWithParentUsers(),
); );
if (!balOut.ok || !balOut.data) { 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; const users = balOut.data;
console.log(`[=] ${users.length} users found`); console.log(`[=] ${users.length} users found`);
console.log(users); console.log(users);
const result = await fetchDataForPosting(date, input, users); const result = await fetchDataForPosting(date, input, users);
postDataCacheStore.set(cacheKey, result.data).catch(console.error);
console.log("result.data.length = ", result.data.length); console.log("result.data.length = ", result.data.length);
return result; return { ...result, key: cacheKey };
}), }),
post: protectedProcedure post: protectedProcedure
.input( .input(z.object({ yes: zPostDataFilters, cachedDataKey: z.string() }))
z.object({
yes: zPostDataFilters,
data: z.array(zPostDataEntry),
}),
)
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
if (await hasPostSession()) { if (await hasPostSession()) {
return { return {
@@ -146,8 +162,9 @@ export const postDataApiRouter = createTRPCRouter({
}; };
} }
let data = input.data; let data: any[] = await postDataCacheStore.get(input.cachedDataKey);
if (input.data.length < 1) { console.log("cached.data.length = ", data.length);
if (data.length < 1) {
console.log("No data found from preview, generating a list"); console.log("No data found from preview, generating a list");
const _out = await fetchDataForPosting(date, input.yes, balOut.data); const _out = await fetchDataForPosting(date, input.yes, balOut.data);
if (!_out.ok) { if (!_out.ok) {
@@ -155,6 +172,7 @@ export const postDataApiRouter = createTRPCRouter({
return _out; return _out;
} }
data = _out.data; data = _out.data;
console.log("data.length = ", data.length);
} }
if (data.length < 1) { 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({ const res = await postDataToApi({
sessions: userSessions, sessions: userSessions,
data, data,
@@ -200,6 +217,7 @@ export const postDataApiRouter = createTRPCRouter({
console.log("[+] Data saved to the database"); console.log("[+] Data saved to the database");
await postDataCacheStore.del(input.cachedDataKey);
await removePostSession(); await removePostSession();
return { return {
ok: true, ok: true,

View File

@@ -13,9 +13,8 @@
import toast from "svelte-french-toast"; import toast from "svelte-french-toast";
import type { CopyCounts } from "./copy-counts.ls"; import type { CopyCounts } from "./copy-counts.ls";
import { filters } from "./fs.stores"; import { filters } from "./fs.stores";
import { nowDT, postdata } from "./stores"; import { postDataKey } from "./stores";
import type { TRPC } from "$lib/trpc/trpc"; import type { TRPC } from "$lib/trpc/trpc";
import { hasDrawBeenClosed } from "$lib/utils";
import PostDataSummarySection from "./post-data-summary-section.svelte"; import PostDataSummarySection from "./post-data-summary-section.svelte";
import * as AlertDialog from "$lib/components/ui/alert-dialog/index"; import * as AlertDialog from "$lib/components/ui/alert-dialog/index";
@@ -35,7 +34,6 @@
? $hasAlreadyPostedQ.data.hasPosted ? $hasAlreadyPostedQ.data.hasPosted
: false; : false;
// $: hasTimePassed = hasDrawBeenClosed($filters.date, $filters.draw!, $nowDT);
$: hasTimePassed = false; $: hasTimePassed = false;
let postUsersQ = api.apiUser.getAllPostUsers.createQuery(undefined, { let postUsersQ = api.apiUser.getAllPostUsers.createQuery(undefined, {
@@ -101,7 +99,7 @@
second: $filters.draw?.abcRateS ?? 0, second: $filters.draw?.abcRateS ?? 0,
}, },
}, },
data: $postdata, cachedDataKey: $postDataKey,
}); });
} }
@@ -551,7 +549,8 @@
<div class="w-full max-w-xs"> <div class="w-full max-w-xs">
<Input <Input
value={copyCounts value={copyCounts
? copyCounts[lc ?? "message"] ?? "" ? (copyCounts[lc ?? "message"] ??
"")
: ""} : ""}
label={"Copy count"} label={"Copy count"}
placeholder={`For ${lc ?? ""} message`} placeholder={`For ${lc ?? ""} message`}

View File

@@ -4,7 +4,7 @@
import type { Draw } from "$lib/utils/data.types"; import type { Draw } from "$lib/utils/data.types";
import toast from "svelte-french-toast"; import toast from "svelte-french-toast";
import PostDataPreviewTable from "./post-data-preview-table.svelte"; import PostDataPreviewTable from "./post-data-preview-table.svelte";
import { setPostDataTableData } from "./stores"; import { postDataKey, setPostDataTableData } from "./stores";
import Pill from "$lib/components/atoms/pill.svelte"; import Pill from "$lib/components/atoms/pill.svelte";
import { postdata } from "./stores"; import { postdata } from "./stores";
@@ -16,8 +16,6 @@
maxPrize: number; maxPrize: number;
}; };
console.log(payload);
let affectingRowVisibility = false; let affectingRowVisibility = false;
let fetchPostDataQ = api.postData.getPostDataForPreview.createQuery( let fetchPostDataQ = api.postData.getPostDataForPreview.createQuery(
@@ -44,6 +42,7 @@
postdata.set([]); postdata.set([]);
return; return;
} }
postDataKey.set(d.key);
postdata.set(d.data); postdata.set(d.data);
affectingRowVisibility = false; affectingRowVisibility = false;
toast("Data fetched successfully."); toast("Data fetched successfully.");

View File

@@ -16,6 +16,7 @@ export const filters = writable<{ date: string; draw: Draw | undefined }>({
}); });
export const postdata = writable<PostDataEntry[]>([]); export const postdata = writable<PostDataEntry[]>([]);
export const postDataKey = writable<string>("");
export const postDataTableOptions = writable<TableOptions<PostDataEntry>>({ export const postDataTableOptions = writable<TableOptions<PostDataEntry>>({
data: [], data: [],