-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save questions to test to avoid re-query database in each test (#52)
* docs(readme): remove `db:generate` * fix(db): drizzle push * feat: save questions to test to avoid re-query database in each test
- Loading branch information
Showing
8 changed files
with
189 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { desc, eq, inArray } from 'drizzle-orm' | ||
|
||
import { db } from '~/db' | ||
import { fetchQuestionCount } from '~/db/queries' | ||
import { questions, questionsToTests, tests } from '~/db/schema' | ||
import { env } from '~/env.mjs' | ||
import dayjs from '~/lib/dayjs' | ||
import { Random } from '~/lib/random' | ||
|
||
const generateRandomSeed = ({ now }: { now: Date }) => { | ||
const nowDayjs = dayjs(now) | ||
const dateString = nowDayjs.format('YYYYMMDD') | ||
const idFlag = String( | ||
Math.floor(nowDayjs.hour() / env.NEXT_PUBLIC_REFRESH_INTERVAL_HOURS) | ||
).padStart(2, '0') | ||
return dateString + idFlag | ||
} | ||
|
||
const generateRandomArray = ({ | ||
length, | ||
max, | ||
seed, | ||
}: { | ||
length: number | ||
max: number | ||
seed: string | ||
}) => { | ||
const random = new Random(Number(seed)) | ||
const randomArray: number[] = [] | ||
const getRandomNumber = (): number => { | ||
const randomNumber = random.range(0, max) | ||
// Random array should be unrepeated when `length` is not bigger than `max` | ||
if (length <= max) { | ||
if (randomArray.includes(randomNumber)) { | ||
return getRandomNumber() | ||
} | ||
} | ||
return randomNumber | ||
} | ||
for (let index = 0; index < length; index++) { | ||
randomArray.push(getRandomNumber()) | ||
} | ||
return randomArray | ||
} | ||
|
||
const generateRandomQuestions = async ({ | ||
seed, | ||
testId, | ||
}: { | ||
seed: string | ||
testId: number | ||
}) => { | ||
// Generate random indexes | ||
const questionTotal = await fetchQuestionCount() | ||
const randomIndexes = generateRandomArray({ | ||
length: env.NEXT_PUBLIC_QUESTIONS_PER_CHALLENGE, | ||
max: Math.min(env.NEXT_PUBLIC_ACTIVE_QUESTIONS_LIMIT, questionTotal), | ||
seed, | ||
}) | ||
// Indexes -> Question IDs | ||
const activeQuestionsIds = ( | ||
await db | ||
.select({ id: questions.id }) | ||
.from(questions) | ||
.orderBy(desc(questions.id)) | ||
.limit(env.NEXT_PUBLIC_ACTIVE_QUESTIONS_LIMIT - 1) | ||
).map(({ id }) => id) | ||
const randomIds = randomIndexes.map( | ||
(index) => activeQuestionsIds[index] || -1 | ||
) | ||
// Save IDs to test | ||
await db | ||
.insert(tests) | ||
.values({ id: testId }) | ||
.onDuplicateKeyUpdate({ set: { id: testId } }) | ||
await db | ||
.delete(questionsToTests) | ||
.where(inArray(questionsToTests.questionId, randomIds)) | ||
await db | ||
.insert(questionsToTests) | ||
.values(randomIds.map((questionId) => ({ questionId, testId }))) | ||
// Get image from question IDs | ||
const images = ( | ||
await db | ||
.select({ image: questions.image }) | ||
.from(questions) | ||
.where(inArray(questions.id, randomIds)) | ||
).map(({ image }) => image) | ||
return images | ||
} | ||
|
||
const getQuestionFromTest = async ({ testId }: { testId: number }) => { | ||
const questionRows = await db | ||
.select() | ||
.from(questions) | ||
.innerJoin(questionsToTests, eq(questionsToTests.questionId, questions.id)) | ||
.where(eq(questionsToTests.testId, testId)) | ||
return questionRows.map(({ questions: { image } }) => image) | ||
} | ||
|
||
type GetQuestionsParams = { | ||
length: number | ||
now: Date | ||
testId: number | ||
} | ||
export const getQuestions = async ({ | ||
length, | ||
now, | ||
testId, | ||
}: GetQuestionsParams) => { | ||
const existedTestQuestions = await getQuestionFromTest({ testId }) | ||
if (existedTestQuestions.length >= length) { | ||
return existedTestQuestions.slice(0, length) | ||
} | ||
const generatedQuestions = await generateRandomQuestions({ | ||
seed: generateRandomSeed({ now }), | ||
testId, | ||
}) | ||
return generatedQuestions.slice(0, length) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import 'dotenv/config' | ||
|
||
import type { Config } from 'drizzle-kit' | ||
|
||
export default { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.