simplifications
This commit is contained in:
@@ -59,14 +59,7 @@ const upsertData = async (
|
|||||||
|
|
||||||
// Insert new entries in chunks
|
// Insert new entries in chunks
|
||||||
console.time("insertion time");
|
console.time("insertion time");
|
||||||
const chunks = chunkArray(entries, chunkSize);
|
for (const chunk of chunkArray(entries, chunkSize)) {
|
||||||
// .map(async (chunk) => {
|
|
||||||
// await surreal.insert<BookingEntry>(tableName, chunk);
|
|
||||||
// });
|
|
||||||
// for (let i = 0; i < chunks.length; i += 2) {
|
|
||||||
// await Promise.all(chunks.slice(i, i + 2));
|
|
||||||
// }
|
|
||||||
for (const chunk of chunks) {
|
|
||||||
await surreal.insert<BookingEntry>(tableName, chunk);
|
await surreal.insert<BookingEntry>(tableName, chunk);
|
||||||
}
|
}
|
||||||
console.timeEnd("insertion time");
|
console.timeEnd("insertion time");
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import {
|
|||||||
type ApiPostUserWithParent,
|
type ApiPostUserWithParent,
|
||||||
ApiUserTypes,
|
ApiUserTypes,
|
||||||
DEFAULT_RANDOM_DISTRIBUTOR,
|
DEFAULT_RANDOM_DISTRIBUTOR,
|
||||||
zApiPostUser,
|
|
||||||
} from "$lib/utils/data.types";
|
} from "$lib/utils/data.types";
|
||||||
|
import { chunkArray } from "../array.chunk";
|
||||||
import { parseToRID, surreal } from "../connectors/surreal.db";
|
import { parseToRID, surreal } from "../connectors/surreal.db";
|
||||||
|
|
||||||
const getUserById = async (userId: string) => {
|
const getUserById = async (userId: string) => {
|
||||||
@@ -159,89 +159,67 @@ const doesExist = async (userId?: string) => {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
const insertMany = async (data: LooseApiUser[], postUsers: ApiPostUser[]) => {
|
async function insertMany(
|
||||||
console.log("insertMany :: ", data.length);
|
|
||||||
await surreal.insert<LooseApiUser>(
|
|
||||||
"apiuser",
|
|
||||||
data.map((e) => {
|
|
||||||
return {
|
|
||||||
...e,
|
|
||||||
postData: !!postUsers.find((u) => u.userId === e.userId),
|
|
||||||
createdAt: new Date().toISOString(),
|
|
||||||
updatedAt: new Date().toISOString(),
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
async function upsertMany(
|
|
||||||
data: LooseApiUser[],
|
data: LooseApiUser[],
|
||||||
wipeTable: boolean,
|
|
||||||
deleteUserType:
|
deleteUserType:
|
||||||
| typeof ApiUserTypes.DISTRIBUTOR
|
| typeof ApiUserTypes.DISTRIBUTOR
|
||||||
| typeof ApiUserTypes.DEALER,
|
| typeof ApiUserTypes.DEALER,
|
||||||
|
tries = 0,
|
||||||
) {
|
) {
|
||||||
const postUsers = await getAllPostUsers();
|
if (tries >= 3) {
|
||||||
console.log(postUsers);
|
console.log("Max retries reached for upserting users");
|
||||||
if (wipeTable) {
|
return;
|
||||||
console.log("[wipeTable] :: deleting all previous users");
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const postUsers = await getAllPostUsers();
|
||||||
|
console.log(postUsers);
|
||||||
|
console.log("Wiping all previous users");
|
||||||
await surreal.query("delete from apiuser where userType = $userType", {
|
await surreal.query("delete from apiuser where userType = $userType", {
|
||||||
userType: deleteUserType,
|
userType: deleteUserType,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
console.log("upsertMany :: ", data.length);
|
const entries = [];
|
||||||
const toCreate = [] as LooseApiUser[];
|
for (const apiUser of data) {
|
||||||
const out = data.map(async (apiUser) => {
|
if (apiUser.disable === 1) {
|
||||||
// INFO: if you do want to keep disabled users, remove this check
|
continue;
|
||||||
if (apiUser.disable === 1) {
|
}
|
||||||
return;
|
const uid = parseToRID(`apiuser:${apiUser.id}`);
|
||||||
|
const existingUser = await surreal.select<ApiUser>(uid);
|
||||||
|
const postData =
|
||||||
|
existingUser?.postData ??
|
||||||
|
!!postUsers.find((pu) => pu.userId === apiUser.userId) ??
|
||||||
|
false;
|
||||||
|
entries.push({
|
||||||
|
...apiUser,
|
||||||
|
id: uid,
|
||||||
|
postData,
|
||||||
|
createdAt: existingUser?.createdAt ?? new Date().toISOString(),
|
||||||
|
updatedAt: new Date().toISOString(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const uid = parseToRID(`apiuser:${apiUser.id}`);
|
|
||||||
const u = await surreal.select<ApiUser>(uid);
|
console.log(`[+] Creating ${entries.length} users into apiuser table`);
|
||||||
if (!u || !u.id) {
|
|
||||||
toCreate.push(apiUser);
|
let chunkSize = Math.floor(
|
||||||
return;
|
Math.random() * (entries.length * 0.2 - entries.length * 0.05) +
|
||||||
|
entries.length * 0.05,
|
||||||
|
);
|
||||||
|
if (chunkSize > 1000) chunkSize = 1000;
|
||||||
|
|
||||||
|
console.log(`Chunk Size: ${chunkSize}`);
|
||||||
|
|
||||||
|
console.time("Insertion Time");
|
||||||
|
for (const chunk of chunkArray(entries, chunkSize)) {
|
||||||
|
await surreal.insert("apiuser", chunk);
|
||||||
}
|
}
|
||||||
let postData =
|
console.timeEnd("Insertion Time");
|
||||||
u.postData ??
|
|
||||||
!!postUsers.find((pu) => pu.userId === u.userId) ??
|
console.log(`[+] Successfully processed ${entries.length} users`);
|
||||||
false;
|
} catch (e) {
|
||||||
const qId = parseToRID(u.id);
|
console.log("Failed to process users, attempting retry");
|
||||||
await surreal.update<LooseApiUser>(qId, {
|
console.error(e);
|
||||||
id: u.id,
|
return await insertMany(data, deleteUserType, tries + 1);
|
||||||
userId: apiUser.userId,
|
|
||||||
userType: apiUser.userType,
|
|
||||||
disableBooking: apiUser.disableBooking,
|
|
||||||
sendVoucher: apiUser.sendVoucher,
|
|
||||||
voucherGenerated: apiUser.voucherGenerated,
|
|
||||||
parentAdmin: apiUser.parentAdmin,
|
|
||||||
parentDistributor: apiUser.parentDistributor,
|
|
||||||
userName: apiUser.userName,
|
|
||||||
userCity: apiUser.userCity,
|
|
||||||
password: apiUser.password,
|
|
||||||
accessDenied: apiUser.accessDenied,
|
|
||||||
phoneNumber: apiUser.phoneNumber,
|
|
||||||
emailAddress: apiUser.emailAddress,
|
|
||||||
disable: apiUser.disable,
|
|
||||||
commission: apiUser.commission,
|
|
||||||
commissionPangora: apiUser.commissionPangora,
|
|
||||||
allowTitles: apiUser.allowTitles,
|
|
||||||
specialDealer: apiUser.specialDealer,
|
|
||||||
allowBalance: apiUser.allowBalance,
|
|
||||||
balance: apiUser.balance,
|
|
||||||
profitlossShare: apiUser.profitlossShare,
|
|
||||||
shareProfitonly: apiUser.shareProfitonly,
|
|
||||||
allowRemoveold: apiUser.allowRemoveold,
|
|
||||||
removeDays: apiUser.removeDays,
|
|
||||||
language: apiUser.language,
|
|
||||||
postData,
|
|
||||||
createdAt: u.createdAt,
|
|
||||||
updatedAt: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await Promise.allSettled(out);
|
|
||||||
if (toCreate.length > 0) {
|
|
||||||
await insertMany(toCreate, postUsers);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,7 +234,7 @@ export const dbApiUser = {
|
|||||||
getRandomDistributor,
|
getRandomDistributor,
|
||||||
getRandomDealer,
|
getRandomDealer,
|
||||||
doesExist,
|
doesExist,
|
||||||
upsertMany,
|
insertMany,
|
||||||
setPostDataFlagForUsers,
|
setPostDataFlagForUsers,
|
||||||
updatePostUsersBalances,
|
updatePostUsersBalances,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ export const actions = {
|
|||||||
errors: [{ message: done.message }],
|
errors: [{ message: done.message }],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await dbApiUser.upsertMany(done.data, true, ApiUserTypes.DISTRIBUTOR);
|
await dbApiUser.insertMany(done.data, ApiUserTypes.DISTRIBUTOR);
|
||||||
return { success: true, errors: [] as ServerError };
|
return { success: true, errors: [] as ServerError };
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ export const actions = {
|
|||||||
if (done.errors.length > 0) {
|
if (done.errors.length > 0) {
|
||||||
return fail(400, { success: false, errors: done.errors });
|
return fail(400, { success: false, errors: done.errors });
|
||||||
}
|
}
|
||||||
await dbApiUser.upsertMany(done.dealers, true, ApiUserTypes.DEALER);
|
await dbApiUser.insertMany(done.dealers, ApiUserTypes.DEALER);
|
||||||
return { success: true, errors: [] as ServerError };
|
return { success: true, errors: [] as ServerError };
|
||||||
},
|
},
|
||||||
} satisfies Actions;
|
} satisfies Actions;
|
||||||
|
|||||||
Reference in New Issue
Block a user