Skip to content

Commit

Permalink
Merge pull request #122 from Arquisoft/Hotfix
Browse files Browse the repository at this point in the history
Removed code smells from QuestionService
  • Loading branch information
didierrc authored Apr 10, 2024
2 parents 59227ce + 37b332d commit 378579b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
2 changes: 1 addition & 1 deletion questionservice/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import app from './app';
const port = 8004;

// mongodb://127.0.0.1:27018/questiondb
const mongoUri = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017';
const mongoUri = process.env.MONGODB_URI??'mongodb://127.0.0.1:27017';
mongoose.connect(mongoUri);

const server = app.listen(port, () => {
Expand Down
73 changes: 35 additions & 38 deletions questionservice/src/services/question-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,43 @@ async function generateQuestions(
size: number,
lang: any
): Promise<object[] | void> {
try {

let numberQuestionsDB = Math.min(Math.floor(size / 2), await QuestionModel.countDocuments());
let questionsToGenerate = size - numberQuestionsDB;
console.log('------------------');
console.log('Questions requested: ' + size);
console.log('------------------');
console.log('Questions from DB: ' + numberQuestionsDB);
console.log('Expected Questions to Generate: ' + questionsToGenerate);

let numberQuestionsDB = Math.min(Math.floor(size / 2), await QuestionModel.countDocuments());
let questionsToGenerate = size - numberQuestionsDB;
console.log('------------------');
console.log('Questions requested: ' + size);
console.log('------------------');
console.log('Questions from DB: ' + numberQuestionsDB);
console.log('Expected Questions to Generate: ' + questionsToGenerate);

// Trying to obtain n random documents
let randomQuestionsTemplates = await getRandomQuestions(questionsToGenerate);
// Trying to obtain n random documents
let randomQuestionsTemplates = await getRandomQuestions(questionsToGenerate);

// Generate and return questions generated from those documents
let questionsArray = await generateQuestionsArray(randomQuestionsTemplates);
// Generate and return questions generated from those documents
let questionsArray = await generateQuestionsArray(randomQuestionsTemplates);

// Skipping questions not generated a.k.a. void
questionsArray = questionsArray.filter(q => typeof q === 'object');
// Skipping questions not generated a.k.a. void
questionsArray = questionsArray.filter(q => typeof q === 'object');

// We take the remaining questions from DB
numberQuestionsDB = size - questionsArray.length;
let questionsArrayDB = await getQuestionsFromDB(numberQuestionsDB);
// Save questions to DB
saveQuestions(questionsArray);
// We take the remaining questions from DB
numberQuestionsDB = size - questionsArray.length;
let questionsArrayDB = await getQuestionsFromDB(numberQuestionsDB);
// Save questions to DB
saveQuestions(questionsArray);

console.log('Retrieved ' + questionsArray.length + ' Questions from Wikidata');
console.log('------------------');
// Concatenating both arrays
questionsArray = questionsArray.concat(questionsArrayDB);
console.log('Retrieved ' + questionsArray.length + ' Questions from Wikidata');
console.log('------------------');
// Concatenating both arrays
questionsArray = questionsArray.concat(questionsArrayDB);

console.log('Retrieved ' + questionsArray.length + ' Questions from DB and Wikidata');
console.log('Retrieved ' + questionsArray.length + ' Questions from DB and Wikidata');

// Translation
if (lang && lang.toLowerCase() !== 'en') {
return await translateQuestionsArray(questionsArray, lang);
}
return questionsArray;
} catch (error) {
throw error;
// Translation
if (lang && lang.toLowerCase() !== 'en') {
return await translateQuestionsArray(questionsArray, lang);
}

return questionsArray;
}

/**
Expand Down Expand Up @@ -284,7 +281,7 @@ function getSparqlQueryFromDocument(document: any): string {
let sparqlQuery: string = document.question_type.query;
let optionEntities = document.question_type.entities as string[];
if (optionEntities.length > 0) {
var randomEntity = getRandomItem(optionEntities);
let randomEntity = getRandomItem(optionEntities);
sparqlQuery = sparqlQuery.replace(/\$\$\$/g, randomEntity);
}
return sparqlQuery;
Expand All @@ -300,9 +297,9 @@ function generateRandomIndexes(
length: number,
numberOfIndexes: number = optionsNumber
) {
var randomIndexes: number[] = [];
for (var i = 0; i < numberOfIndexes; i++) {
var possibleRandom = Math.floor(Math.random() * length);
let randomIndexes: number[] = [];
for (let i = 0; i < numberOfIndexes; i++) {
let possibleRandom = Math.floor(Math.random() * length);
while (randomIndexes.includes(possibleRandom))
possibleRandom = Math.floor(Math.random() * length);

Expand All @@ -322,8 +319,8 @@ function getRandomResponses(
randomIndexes: number[]
): any {
let answersArray: object[] = [];
for (var i = 0; i < optionsNumber; i++) {
var answer = wikidataResponse[randomIndexes[i]].answerLabel;
for (let i = 0; i < optionsNumber; i++) {
let answer = wikidataResponse[randomIndexes[i]].answerLabel;
answersArray[i] = {
id: i + 1,
text: answer,
Expand Down
2 changes: 1 addition & 1 deletion questionservice/src/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function validateSizePresent(req: Request) {
* @returns The field as a number
*/
function validateNumber(field: string) {
const size = parseInt(field as string, 10);
const size = parseInt(field, 10);
// Checking parameter is a number
if (isNaN(size)) {
throw new Error('The size parameter must be a number');
Expand Down
2 changes: 1 addition & 1 deletion questionservice/test/question-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async function mockWikidataSparql(numberReturnValues: number) {

}

function mockResponseTranslationRequest() {
async function mockResponseTranslationRequest() {

const mockResponseTranslation = {
status: 200,
Expand Down

0 comments on commit 378579b

Please sign in to comment.