Skip to content

Commit

Permalink
Merge pull request #24 from Horizontal-org/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
juandans01 authored Jul 17, 2023
2 parents 293b4cc + 14c20b8 commit 492390e
Showing 1 changed file with 104 additions and 2 deletions.
106 changes: 104 additions & 2 deletions src/modules/question/services/quiz.question.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class GenerateQuizQuestionService {
private apps;
private fieldsOfWorkIds;

private MIN_PHISHING_PERCENTAGE = 50
private MAX_PHISHING_PERCENTAGE = 60

constructor(
@InjectRepository(Question)
private readonly questionRepository: Repository<Question>,
Expand Down Expand Up @@ -68,6 +71,8 @@ export class GenerateQuizQuestionService {
text: explanation.explanationTranslations[0]?.content,
})),
}));


let final = [];
// if 1 app return everything from that app
if (apps.length === 1) {
Expand All @@ -89,6 +94,7 @@ export class GenerateQuizQuestionService {
});
}


// order repetition
const repetitionKeys = Object.keys(appCounter).sort((a, b) => {
return appCounter[a].length - appCounter[b].length;
Expand All @@ -109,7 +115,8 @@ export class GenerateQuizQuestionService {
for (let j = 0; j < appCounter[appId].length; j++) {
const questionId = appCounter[appId][j];
const question = all.find((q) => q.id == questionId);
if (!usedQuestions.includes(questionId) && usedApps[appId] <= perc) {
// if (!usedQuestions.includes(questionId) && usedApps[appId] <= perc) { REMOVED PERCENTAGE CHECK, I DONT KNOW IF WE NEED THIS ANYMORE
if (!usedQuestions.includes(questionId)) {
questionsWithApps.push({
...question,
apps: null,
Expand All @@ -121,11 +128,97 @@ export class GenerateQuizQuestionService {
}
}

final = this.fillFieldsOfWork(questionsWithApps);
final = this.balancePhishing(questionsWithApps)
// CREO QUE YA NO NECESITAMOS ESTO final = this.fillFieldsOfWork(questionsWithApps);

if (final.length > 0 ) {
this.shuffleArray(final)
}
return final;
}
}


private balancePhishing = (questions) => {

console.log("🚀 ~ file: quiz.question.service.ts:140 ~ GenerateQuizQuestionService ~ this.fieldsOfWorkIds:", this.fieldsOfWorkIds)
// console.log("🚀 ~ file: quiz.question.service.ts:140 ~ GenerateQuizQuestionService ~ console.log ~ questions:", questions)

// aunque agarre 10 preguntas que cumplen mis fields of work si no tienen un buen balance de phising/nophising saco las que haga falta para lograrlo

this.shuffleArray(questions)
const sorted = questions.sort((a, b) => this.fieldsOfWorkIds.includes(b.fieldOfWorkId) - this.fieldsOfWorkIds.includes(a.fieldOfWorkId))
const firstTen = sorted.slice(0, 10)
console.log("FIRST TEN -> ", firstTen.map((q) => {return {
id: q.id, phising: q.isPhising, fieldsOfWorkId: q.fieldOfWorkId, app: q.app.name
}}))

const extra = sorted.slice(10)
console.log("REMAINING -> ", extra.map((q) => {return {
id: q.id, phising: q.isPhising, fieldsOfWorkId: q.fieldOfWorkId, app: q.app.name
}}))

if (extra.length === 0) {
return firstTen
}

// check if the balance of the first ten questions is valid
let phishingPercentage = this.getPhishingPercentage(firstTen)


if (phishingPercentage > this.MAX_PHISHING_PERCENTAGE) {
console.log("REMOVE PHISHING ", phishingPercentage)
this.changeBalance(firstTen, extra, 0)
} else if (phishingPercentage < this.MIN_PHISHING_PERCENTAGE) {
console.log("ADD PHISHING ", phishingPercentage)
this.changeBalance(firstTen, extra, 1)
}

console.log("FIRST TEN -> ", firstTen.map((q) => {return {
id: q.id, phising: q.isPhising, fieldsOfWorkId: q.fieldOfWorkId, app: q.app.name
}}))
phishingPercentage = this.getPhishingPercentage(firstTen)

return firstTen
}

private changeBalance = (firstTen, extra, addPhising) => {
let usefulExtra = extra.filter(e => addPhising ? e.isPhising : !e.isPhising)
console.log("usefulExtra -> ", usefulExtra.map((q) => {return {
id: q.id, phising: q.isPhising, fieldsOfWorkId: q.fieldOfWorkId, app: q.app.name
}}))

addPhising ? firstTen.sort((a, b) => b.isPhising - a.isPhising ) : firstTen.sort((a, b) => a.isPhising - b.isPhising )
console.log("sortedByPhishingState -> ", firstTen.map((q) => {return {
id: q.id, phising: q.isPhising, fieldsOfWorkId: q.fieldOfWorkId, app: q.app.name
}}))

if (usefulExtra.length > 0) {
// replace
firstTen.splice(firstTen.length - 1, 1, usefulExtra[0])
usefulExtra.shift()

const phishingPercentage = this.getPhishingPercentage(firstTen)

if (phishingPercentage > this.MAX_PHISHING_PERCENTAGE) {
console.log("REMOVE PHISHING ", phishingPercentage)
this.changeBalance(firstTen, usefulExtra, 0)
} else if (phishingPercentage < this.MIN_PHISHING_PERCENTAGE) {
console.log("ADD PHISHING ", phishingPercentage)
this.changeBalance(firstTen, usefulExtra, 1)
}
}
}

private getPhishingPercentage = (array) => {
const selectedQuestions = array.slice(0, 10);
const phishingCount = selectedQuestions.filter((q) => q.isPhising).length;
const percentage = (phishingCount * 100) / selectedQuestions.length;

console.log("IS PHISHING -> ", percentage);
return percentage;
};

private fillFieldsOfWork = (questions) => {
let final = questions.filter((af) =>
this.fieldsOfWorkIds.includes(af.fieldOfWorkId),
Expand Down Expand Up @@ -166,4 +259,13 @@ export class GenerateQuizQuestionService {
const final = this.fillFieldsOfWork(appQuestions);
return final;
};

private shuffleArray = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

0 comments on commit 492390e

Please sign in to comment.