Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IDATT-2501-Backend
Manage
Activity
Members
Labels
Plan
Issues
32
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TrioTech
IDATT-2501-Backend
Merge requests
!70
Resolve "Add random question controller"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Add random question controller"
145-add-random-question-controller
into
main
Overview
0
Commits
1
Pipelines
2
Changes
2
Merged
Pedro Pablo Cardona Arroyave
requested to merge
145-add-random-question-controller
into
main
1 year ago
Overview
0
Commits
1
Pipelines
2
Changes
2
Expand
Closes
#145 (closed)
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
4fa3fb31
1 commit,
1 year ago
2 files
+
76
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
controllers/question.controller.js
+
67
−
0
Options
@@ -150,8 +150,75 @@ async function getRandomQuestionsByCategory(category_id, count) {
}
}
async
function
getRandomQuestions
(
count
)
{
let
client
;
try
{
client
=
await
connectToDatabase
();
const
db
=
client
.
db
(
"
TrioTech
"
);
const
questionCollection
=
db
.
collection
(
"
questions
"
);
// Calculate the number of questions to fetch for each difficulty level
const
totalQuestions
=
count
;
const
countDifficult0
=
Math
.
floor
(
0.4
*
totalQuestions
);
const
countDifficult1
=
Math
.
floor
(
0.3
*
totalQuestions
);
const
countDifficult2
=
totalQuestions
-
countDifficult0
-
countDifficult1
;
// Define the aggregation pipeline to select questions based on difficulty
const
pipeline
=
[
{
$match
:
{
$and
:
[{
difficult
:
0
}],
},
},
{
$sample
:
{
size
:
countDifficult0
},
},
{
$unionWith
:
{
coll
:
"
questions
"
,
pipeline
:
[
{
$match
:
{
$and
:
[{
difficult
:
1
}],
},
},
{
$sample
:
{
size
:
countDifficult1
},
},
],
},
},
{
$unionWith
:
{
coll
:
"
questions
"
,
pipeline
:
[
{
$match
:
{
$and
:
[{
difficult
:
2
}],
},
},
{
$sample
:
{
size
:
countDifficult2
},
},
],
},
},
];
const
response
=
await
questionCollection
.
aggregate
(
pipeline
).
toArray
();
return
response
;
}
catch
(
error
)
{
throw
error
;
}
finally
{
if
(
client
)
{
await
client
.
close
();
}
}
}
module
.
exports
=
{
createQuestion
,
getQuestion
,
getRandomQuestionsByCategory
,
getRandomQuestions
,
};
Loading