Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions server/scripts/setup/generators/testGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { faker } from '@faker-js/faker';
import { Test } from '../../../src/models/Test';
import { Trainee, TestResult, TestType } from '../../../src/models/Trainee';

export const generateTest = (trainee: Trainee): Test => {
const score = getRandomScore();
return {
id: '',
date: faker.date.future({ refDate: trainee.educationInfo.startDate }),
type: faker.helpers.arrayElement(Object.values(TestType)),
result: getRandomTestResult(score),
score: score,
comments: '',
};
};

const getRandomTestResult = (score: number | null): TestResult => {
if (faker.datatype.boolean(0.01)) {
return TestResult.Disqualified;
}
if (score === null) {
return faker.helpers.arrayElement(Object.values(TestResult));
}
if (score < 6) {
return TestResult.Failed;
}
if (score < 7) {
return TestResult.PassedWithWarning;
}
return TestResult.Passed;
};

const getRandomScore = (): number | null => {
const weightedRandomScore: number | null = faker.helpers.weightedArrayElement([
{ value: null, weight: 10 },
{ value: 1, weight: 1 },
{ value: 2, weight: 2 },
{ value: 3, weight: 2 },
{ value: 4, weight: 5 },
{ value: 5, weight: 10 },
{ value: 6, weight: 15 },
{ value: 7, weight: 20 },
{ value: 8, weight: 30 },
{ value: 9, weight: 20 },
{ value: 10, weight: 10 },
]);

if (weightedRandomScore === null) {
return null;
}

return faker.number.float({ min: weightedRandomScore - 1, max: weightedRandomScore, fractionDigits: 1 });
};
18 changes: 16 additions & 2 deletions server/scripts/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* - .env file configured in the server root directory.
* - Dojo server running locally (npm run dev)
*
* The script will generate 800 trainees by default (configurable via GENERATE_COUNT).
* The script will generate 500 trainees by default (configurable via GENERATE_COUNT).
*/

import fs from 'fs';
Expand All @@ -24,6 +24,7 @@ import * as dotenv from 'dotenv';
import * as path from 'path';
import { TokenService } from '../../src/services/TokenService';
import { createInterface } from 'readline/promises';
import { generateTest } from './generators/testGenerator';

// Config
const MONGO_URI = 'mongodb://localhost:27017/dojo';
Expand Down Expand Up @@ -129,6 +130,18 @@ const addStrikes = async (token: string, trainee: Trainee, count: number) => {
await Promise.all(promises);
};

const addTests = async (token: string, trainee: Trainee, count: number) => {
const url = `${BASE_URL}/trainees/${trainee.id}/tests`;
const promises = new Array(count).fill(0).map(() => {
return fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
body: JSON.stringify(generateTest(trainee)),
});
});
await Promise.all(promises);
};

const addInteractions = async (token: string, trainee: Trainee, count: number) => {
const url = `${BASE_URL}/trainees/${trainee.id}/interactions`;
const promises = new Array(count).fill(0).map(() => {
Expand Down Expand Up @@ -182,9 +195,10 @@ const main = async () => {
const imageResponse = await fetch(imageURL);
const imageData = await imageResponse.blob();

// Set profile picture and add strikes and interactions
// Set profile picture and add strikes, tests and interactions
await setProfilePicture(token, newTrainee.id, imageData);
await addStrikes(token, newTrainee, randomStrikeNumber());
await addTests(token, newTrainee, faker.number.int({ min: 0, max: 7 }));
await addInteractions(token, newTrainee, faker.number.int({ min: 0, max: 12 }));

const percent = ((i + 1) / GENERATE_COUNT) * 100;
Expand Down
Loading