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) => {
try {
console.log("Testing session validity...", jwt);
const res = await fetch(
`${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);
}
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,

View File

@@ -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 @@
<div class="w-full max-w-xs">
<Input
value={copyCounts
? copyCounts[lc ?? "message"] ?? ""
? (copyCounts[lc ?? "message"] ??
"")
: ""}
label={"Copy count"}
placeholder={`For ${lc ?? ""} message`}

View File

@@ -4,7 +4,7 @@
import type { Draw } from "$lib/utils/data.types";
import toast from "svelte-french-toast";
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 { postdata } from "./stores";
@@ -16,8 +16,6 @@
maxPrize: number;
};
console.log(payload);
let affectingRowVisibility = false;
let fetchPostDataQ = api.postData.getPostDataForPreview.createQuery(
@@ -44,6 +42,7 @@
postdata.set([]);
return;
}
postDataKey.set(d.key);
postdata.set(d.data);
affectingRowVisibility = false;
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 postDataKey = writable<string>("");
export const postDataTableOptions = writable<TableOptions<PostDataEntry>>({
data: [],