randomize result
All checks were successful
backend-action / build-image (push) Successful in 11m53s

This commit is contained in:
2024-06-24 15:16:47 +07:00
parent f6b18d3f82
commit cba647ca27
3 changed files with 18 additions and 27 deletions

1
.gitignore vendored
View File

@@ -13,3 +13,4 @@ sqlite.db-shm
sqlite.db-wal
user.json
user-p.json
user-c.json

View File

@@ -10,7 +10,7 @@ import {
import { Groups, Opinions, Provinces, Districts } from "./initialData";
import { createBucket, createClient } from "./src/minio";
import { Config } from "./src/config";
import ud from "./user-p.json";
import ud from "./user-c.json";
const user_data: UserData[] = ud;
console.log(ud.length);
@@ -82,9 +82,6 @@ type UserData = {
job_code: number;
selection: string[];
province: string;
province_code: number;
district: string;
district_code: number;
rank: number;
};
@@ -92,31 +89,17 @@ async function create_user() {
const provinces = await db.query.province.findMany({});
const district = await db.query.zone.findMany({});
for (const newUser of user_data) {
const thisProvince = provinces.find((p) => p.id == newUser.province_code);
const thisDistrict = district.find((p) => p.id == newUser.district_code);
let isSelectionFound = true;
for (const selection of newUser.selection) {
const isFound = user_data.findIndex(
(p) =>
`${p.first_name} ${p.last_name}` == selection &&
p.province_code == newUser.province_code,
(p) => `${p.first_name} ${p.last_name}` == selection,
);
if (isFound == -1) {
isSelectionFound = false;
}
}
if (
thisProvince === undefined ||
thisDistrict === undefined ||
!isSelectionFound
) {
console.log(
newUser.province,
newUser.district,
thisDistrict,
thisProvince,
isSelectionFound,
);
if (!isSelectionFound) {
console.log(newUser.province, newUser, isSelectionFound);
} else {
await db.insert(user).values({
group: newUser.job_code,
@@ -128,7 +111,7 @@ async function create_user() {
age: 0,
job: "",
education: "",
zone: thisDistrict.id,
zone: 1001,
rank: newUser.rank,
});
}
@@ -143,11 +126,7 @@ async function create_relation() {
});
for (const u of allUser) {
let thisUsers = user_data.filter(
(raw) =>
raw.first_name == u.firstName &&
raw.last_name == u.lastName &&
raw.district_code == u.zone.id &&
raw.province_code == u.zone.province.id,
(raw) => raw.first_name == u.firstName && raw.last_name == u.lastName,
);
if (thisUsers.length !== 1) {
console.log("duplicated users", thisUsers);

View File

@@ -245,11 +245,14 @@ async function getAllUser(
(u) => validSelection.filter((v) => v.id == u.id).length == 0,
),
];
resultUser = randomArray(1, 10, resultUser);
} else {
resultUser = [
...topTen,
...users.filter((u) => topTen.filter((v) => v.id == u.id).length == 0),
];
resultUser = randomArray(0, 5, resultUser);
resultUser = randomArray(5, 10, resultUser);
}
return resultUser
@@ -505,3 +508,11 @@ async function getZone(province?: number) {
.then((queryResult) => queryResult.map((v) => v.id));
return zoneIds;
}
function randomArray<T>(from: number, to: number, arr: T[]): T[] {
for (let i = Math.min(arr.length - 1, from); i < to; i++) {
const j = Math.floor(Math.random() * (i - from)) + from;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}