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
19 changes: 19 additions & 0 deletions docs/api/api_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,25 @@ define({
"name": "getHackerStats",
"group": "Hacker",
"version": "0.0.9",
"parameter": {
"fields": {
"query": [{
"group": "query",
"type": "String",
"optional": false,
"field": "model",
"description": "<p>the model to be searched (Only hacker supported)</p>"
},
{
"group": "query",
"type": "Array",
"optional": false,
"field": "q",
"description": "<p>the query to be executed. For more information on how to format this, please see https://docs.mchacks.ca/architecture/</p>"
}
]
}
},
"success": {
"fields": {
"Success 200": [{
Expand Down
19 changes: 19 additions & 0 deletions docs/api/api_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,25 @@
"name": "getHackerStats",
"group": "Hacker",
"version": "0.0.9",
"parameter": {
"fields": {
"query": [{
"group": "query",
"type": "String",
"optional": false,
"field": "model",
"description": "<p>the model to be searched (Only hacker supported)</p>"
},
{
"group": "query",
"type": "Array",
"optional": false,
"field": "q",
"description": "<p>the query to be executed. For more information on how to format this, please see https://docs.mchacks.ca/architecture/</p>"
}
]
}
},
"success": {
"fields": {
"Success 200": [{
Expand Down
30 changes: 15 additions & 15 deletions docs/api/api_project.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
define({
"name": "hackerAPI",
"version": "0.0.8",
"description": "Documentation for the API used for mchacks",
"defaultVersion": "0.0.8",
"title": "hackerAPI documentation",
"url": "https://api.mchacks.ca/api",
"sampleUrl": "https://api.mchacks.ca/api",
"apidoc": "0.3.0",
"generator": {
"name": "apidoc",
"time": "2019-01-05T01:35:07.317Z",
"url": "http://apidocjs.com",
"version": "0.17.7"
}
define({
"name": "hackerAPI",
"version": "0.0.8",
"description": "Documentation for the API used for mchacks",
"defaultVersion": "0.0.8",
"title": "hackerAPI documentation",
"url": "https://api.mchacks.ca/api",
"sampleUrl": "https://api.mchacks.ca/api",
"apidoc": "0.3.0",
"generator": {
"name": "apidoc",
"time": "2019-01-07T03:26:47.443Z",
"url": "http://apidocjs.com",
"version": "0.17.7"
}
});
30 changes: 15 additions & 15 deletions docs/api/api_project.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "hackerAPI",
"version": "0.0.8",
"description": "Documentation for the API used for mchacks",
"defaultVersion": "0.0.8",
"title": "hackerAPI documentation",
"url": "https://api.mchacks.ca/api",
"sampleUrl": "https://api.mchacks.ca/api",
"apidoc": "0.3.0",
"generator": {
"name": "apidoc",
"time": "2019-01-05T01:35:07.317Z",
"url": "http://apidocjs.com",
"version": "0.17.7"
}
{
"name": "hackerAPI",
"version": "0.0.8",
"description": "Documentation for the API used for mchacks",
"defaultVersion": "0.0.8",
"title": "hackerAPI documentation",
"url": "https://api.mchacks.ca/api",
"sampleUrl": "https://api.mchacks.ca/api",
"apidoc": "0.3.0",
"generator": {
"name": "apidoc",
"time": "2019-01-07T03:26:47.443Z",
"url": "http://apidocjs.com",
"version": "0.17.7"
}
}
2 changes: 1 addition & 1 deletion middlewares/hacker.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ async function findSelf(req, res, next) {
}

async function getStats(req, res, next) {
const stats = await Services.Hacker.getStats();
const stats = await Services.Hacker.getStats(req.body.results);
req.body.stats = stats;
next();
}
Expand Down
10 changes: 8 additions & 2 deletions middlewares/search.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function parseQuery(req, res, next) {
}
//Default limit
if (!req.body.hasOwnProperty("limit")) {
req.body.limit = 25;
req.body.limit = 10000;
} else {
req.body.limit = parseInt(req.body.limit);
}
Expand Down Expand Up @@ -62,8 +62,14 @@ async function executeQuery(req, res, next) {
return next();
}

function setExpandTrue(req, res, next) {
req.body.expand = true;
next();
}


module.exports = {
parseQuery: parseQuery,
executeQuery: Middleware.Util.asyncMiddleware(executeQuery)
executeQuery: Middleware.Util.asyncMiddleware(executeQuery),
setExpandTrue: setExpandTrue,
};
4 changes: 4 additions & 0 deletions middlewares/validators/hacker.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ module.exports = {
downloadResumeValidator: [
VALIDATOR.mongoIdValidator("param", "id", false)
],
statsValidator: [
VALIDATOR.searchModelValidator("query", "model", false),
VALIDATOR.searchValidator("query", "q")
]
};
12 changes: 11 additions & 1 deletion routes/api/hacker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const Middleware = {
parseBody: require("../../middlewares/parse-body.middleware"),
Util: require("../../middlewares/util.middleware"),
Hacker: require("../../middlewares/hacker.middleware"),
Auth: require("../../middlewares/auth.middleware")
Auth: require("../../middlewares/auth.middleware"),
Search: require("../../middlewares/search.middleware")
};
const Services = {
Hacker: require("../../services/hacker.service"),
Expand Down Expand Up @@ -173,6 +174,10 @@ module.exports = {
* @apiName getHackerStats
* @apiGroup Hacker
* @apiVersion 0.0.9
*
* @apiParam (query) {String} model the model to be searched (Only hacker supported)
* @apiParam (query) {Array} q the query to be executed. For more information on how to format this, please see https://docs.mchacks.ca/architecture/
*
* @apiSuccess {string} message Success message
* @apiSuccess {object} data Hacker object
* @apiSuccessExample {object} Success-Response:
Expand Down Expand Up @@ -201,6 +206,11 @@ module.exports = {
hackerRouter.route("/stats").get(
Middleware.Auth.ensureAuthenticated(),
Middleware.Auth.ensureAuthorized(),
Middleware.Validator.Hacker.statsValidator,
Middleware.parseBody.middleware,
Middleware.Search.setExpandTrue,
Middleware.Search.parseQuery,
Middleware.Search.executeQuery,
Middleware.Hacker.getStats,
Controllers.Hacker.gotStats
);
Expand Down
16 changes: 11 additions & 5 deletions services/hacker.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,21 @@ function findByAccountId(accountId) {
return Hacker.findOne(query, logger.updateCallbackFactory(TAG, "hacker"));
}

async function getStats() {
const TAG = `[ hacker Service # getHackerStats ]`;
async function getStatsAllHackersCached() {
const TAG = `[ hacker Service # getStatsAll ]`;
if (cache.get(Constants.CACHE_KEY_STATS) !== null) {
logger.info(`${TAG} Getting cached stats`);
return cache.get(Constants.CACHE_KEY_STATS);
}
const allHackers = await Hacker.find({}, logger.updateCallbackFactory(TAG, "hacker")).populate({
path: "accountId",
});
cache.put(Constants.CACHE_KEY_STATS, stats, Constants.CACHE_TIMEOUT_STATS); //set a time-out of 5 minutes
return getStats(allHackers);
}

function getStats(hackers) {
const TAG = `[ hacker Service # getStats ]`;
const stats = {
total: 0,
status: {},
Expand All @@ -106,7 +112,7 @@ async function getStats() {
age: {}
};

allHackers.forEach((hacker) => {
hackers.forEach((hacker) => {
if (!hacker.accountId) {
// user is no longer with us for some reason :(
return;
Expand All @@ -133,7 +139,6 @@ async function getStats() {
const age = hacker.accountId.getAge();
stats.age[age] = (stats.age[age]) ? stats.age[age] + 1 : 1;
});
cache.put(Constants.CACHE_KEY_STATS, stats, Constants.CACHE_TIMEOUT_STATS); //set a time-out of 5 minutes
return stats;
}

Expand All @@ -144,5 +149,6 @@ module.exports = {
updateOne: updateOne,
findIds: findIds,
findByAccountId: findByAccountId,
getStats: getStats
getStats: getStats,
getStatsAllHackersCached: getStatsAllHackersCached,
};
5 changes: 2 additions & 3 deletions services/search.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ function executeQuery(model, queryArray, page, limit, sort, sort_by, shouldExpan
} else if (sort == "asc") {
query.sort(sort_by);
}
return query.lean()
.limit(limit)
return query.limit(limit)
.skip(limit * page)
.exec()
.exec();
}

module.exports = {
Expand Down
20 changes: 20 additions & 0 deletions tests/hacker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,22 @@ describe("POST add a hacker resume", function () {
});

describe("GET Hacker stats", function () {
it("It should FAIL and get hacker stats (invalid validation)", function (done) {
//this takes a lot of time for some reason
util.auth.login(agent, Admin1, (error) => {
if (error) {
return done(error);
}
return agent
.get(`/api/hacker/stats`)
.end(function (err, res) {
res.should.have.status(422);
res.should.have.property("body");
res.body.should.have.property("message");
done();
});
});
});
it("It should SUCCEED and get hacker stats", function (done) {
//this takes a lot of time for some reason
util.auth.login(agent, Admin1, (error) => {
Expand All @@ -758,6 +774,10 @@ describe("GET Hacker stats", function () {
}
return agent
.get(`/api/hacker/stats`)
.query({
model: "hacker",
q: JSON.stringify([])
})
.end(function (err, res) {
res.should.have.status(200);
res.should.have.property("body");
Expand Down