From cba647ca2771ffa06f0f2ed29474c9300d46f43f Mon Sep 17 00:00:00 2001 From: Thanu Poptiphueng Date: Mon, 24 Jun 2024 15:16:47 +0700 Subject: [PATCH] randomize result --- .gitignore | 1 + addMetadata.ts | 33 ++++++--------------------------- src/userRoute.ts | 11 +++++++++++ 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index f6d2723..cb08ec8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ sqlite.db-shm sqlite.db-wal user.json user-p.json +user-c.json diff --git a/addMetadata.ts b/addMetadata.ts index ea4c8b0..f23c0fe 100644 --- a/addMetadata.ts +++ b/addMetadata.ts @@ -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); diff --git a/src/userRoute.ts b/src/userRoute.ts index fa62103..e0b7e12 100644 --- a/src/userRoute.ts +++ b/src/userRoute.ts @@ -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(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; +}