Skip to content

Commit 23eb14d

Browse files
authored
Feature/274 stats query (#275)
* Add query param to stats * Docs * Fix test
1 parent dff8614 commit 23eb14d

11 files changed

Lines changed: 125 additions & 42 deletions

File tree

docs/api/api_data.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,25 @@ define({
13831383
"name": "getHackerStats",
13841384
"group": "Hacker",
13851385
"version": "0.0.9",
1386+
"parameter": {
1387+
"fields": {
1388+
"query": [{
1389+
"group": "query",
1390+
"type": "String",
1391+
"optional": false,
1392+
"field": "model",
1393+
"description": "<p>the model to be searched (Only hacker supported)</p>"
1394+
},
1395+
{
1396+
"group": "query",
1397+
"type": "Array",
1398+
"optional": false,
1399+
"field": "q",
1400+
"description": "<p>the query to be executed. For more information on how to format this, please see https://docs.mchacks.ca/architecture/</p>"
1401+
}
1402+
]
1403+
}
1404+
},
13861405
"success": {
13871406
"fields": {
13881407
"Success 200": [{

docs/api/api_data.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,25 @@
13821382
"name": "getHackerStats",
13831383
"group": "Hacker",
13841384
"version": "0.0.9",
1385+
"parameter": {
1386+
"fields": {
1387+
"query": [{
1388+
"group": "query",
1389+
"type": "String",
1390+
"optional": false,
1391+
"field": "model",
1392+
"description": "<p>the model to be searched (Only hacker supported)</p>"
1393+
},
1394+
{
1395+
"group": "query",
1396+
"type": "Array",
1397+
"optional": false,
1398+
"field": "q",
1399+
"description": "<p>the query to be executed. For more information on how to format this, please see https://docs.mchacks.ca/architecture/</p>"
1400+
}
1401+
]
1402+
}
1403+
},
13851404
"success": {
13861405
"fields": {
13871406
"Success 200": [{

docs/api/api_project.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
define({
2-
"name": "hackerAPI",
3-
"version": "0.0.8",
4-
"description": "Documentation for the API used for mchacks",
5-
"defaultVersion": "0.0.8",
6-
"title": "hackerAPI documentation",
7-
"url": "https://api.mchacks.ca/api",
8-
"sampleUrl": "https://api.mchacks.ca/api",
9-
"apidoc": "0.3.0",
10-
"generator": {
11-
"name": "apidoc",
12-
"time": "2019-01-05T01:35:07.317Z",
13-
"url": "http://apidocjs.com",
14-
"version": "0.17.7"
15-
}
1+
define({
2+
"name": "hackerAPI",
3+
"version": "0.0.8",
4+
"description": "Documentation for the API used for mchacks",
5+
"defaultVersion": "0.0.8",
6+
"title": "hackerAPI documentation",
7+
"url": "https://api.mchacks.ca/api",
8+
"sampleUrl": "https://api.mchacks.ca/api",
9+
"apidoc": "0.3.0",
10+
"generator": {
11+
"name": "apidoc",
12+
"time": "2019-01-07T03:26:47.443Z",
13+
"url": "http://apidocjs.com",
14+
"version": "0.17.7"
15+
}
1616
});

docs/api/api_project.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{
2-
"name": "hackerAPI",
3-
"version": "0.0.8",
4-
"description": "Documentation for the API used for mchacks",
5-
"defaultVersion": "0.0.8",
6-
"title": "hackerAPI documentation",
7-
"url": "https://api.mchacks.ca/api",
8-
"sampleUrl": "https://api.mchacks.ca/api",
9-
"apidoc": "0.3.0",
10-
"generator": {
11-
"name": "apidoc",
12-
"time": "2019-01-05T01:35:07.317Z",
13-
"url": "http://apidocjs.com",
14-
"version": "0.17.7"
15-
}
1+
{
2+
"name": "hackerAPI",
3+
"version": "0.0.8",
4+
"description": "Documentation for the API used for mchacks",
5+
"defaultVersion": "0.0.8",
6+
"title": "hackerAPI documentation",
7+
"url": "https://api.mchacks.ca/api",
8+
"sampleUrl": "https://api.mchacks.ca/api",
9+
"apidoc": "0.3.0",
10+
"generator": {
11+
"name": "apidoc",
12+
"time": "2019-01-07T03:26:47.443Z",
13+
"url": "http://apidocjs.com",
14+
"version": "0.17.7"
15+
}
1616
}

middlewares/hacker.middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ async function findSelf(req, res, next) {
487487
}
488488

489489
async function getStats(req, res, next) {
490-
const stats = await Services.Hacker.getStats();
490+
const stats = await Services.Hacker.getStats(req.body.results);
491491
req.body.stats = stats;
492492
next();
493493
}

middlewares/search.middleware.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function parseQuery(req, res, next) {
2626
}
2727
//Default limit
2828
if (!req.body.hasOwnProperty("limit")) {
29-
req.body.limit = 25;
29+
req.body.limit = 10000;
3030
} else {
3131
req.body.limit = parseInt(req.body.limit);
3232
}
@@ -62,8 +62,14 @@ async function executeQuery(req, res, next) {
6262
return next();
6363
}
6464

65+
function setExpandTrue(req, res, next) {
66+
req.body.expand = true;
67+
next();
68+
}
69+
6570

6671
module.exports = {
6772
parseQuery: parseQuery,
68-
executeQuery: Middleware.Util.asyncMiddleware(executeQuery)
73+
executeQuery: Middleware.Util.asyncMiddleware(executeQuery),
74+
setExpandTrue: setExpandTrue,
6975
};

middlewares/validators/hacker.validator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ module.exports = {
4444
downloadResumeValidator: [
4545
VALIDATOR.mongoIdValidator("param", "id", false)
4646
],
47+
statsValidator: [
48+
VALIDATOR.searchModelValidator("query", "model", false),
49+
VALIDATOR.searchValidator("query", "q")
50+
]
4751
};

routes/api/hacker.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const Middleware = {
1313
parseBody: require("../../middlewares/parse-body.middleware"),
1414
Util: require("../../middlewares/util.middleware"),
1515
Hacker: require("../../middlewares/hacker.middleware"),
16-
Auth: require("../../middlewares/auth.middleware")
16+
Auth: require("../../middlewares/auth.middleware"),
17+
Search: require("../../middlewares/search.middleware")
1718
};
1819
const Services = {
1920
Hacker: require("../../services/hacker.service"),
@@ -173,6 +174,10 @@ module.exports = {
173174
* @apiName getHackerStats
174175
* @apiGroup Hacker
175176
* @apiVersion 0.0.9
177+
*
178+
* @apiParam (query) {String} model the model to be searched (Only hacker supported)
179+
* @apiParam (query) {Array} q the query to be executed. For more information on how to format this, please see https://docs.mchacks.ca/architecture/
180+
*
176181
* @apiSuccess {string} message Success message
177182
* @apiSuccess {object} data Hacker object
178183
* @apiSuccessExample {object} Success-Response:
@@ -201,6 +206,11 @@ module.exports = {
201206
hackerRouter.route("/stats").get(
202207
Middleware.Auth.ensureAuthenticated(),
203208
Middleware.Auth.ensureAuthorized(),
209+
Middleware.Validator.Hacker.statsValidator,
210+
Middleware.parseBody.middleware,
211+
Middleware.Search.setExpandTrue,
212+
Middleware.Search.parseQuery,
213+
Middleware.Search.executeQuery,
204214
Middleware.Hacker.getStats,
205215
Controllers.Hacker.gotStats
206216
);

services/hacker.service.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,21 @@ function findByAccountId(accountId) {
8181
return Hacker.findOne(query, logger.updateCallbackFactory(TAG, "hacker"));
8282
}
8383

84-
async function getStats() {
85-
const TAG = `[ hacker Service # getHackerStats ]`;
84+
async function getStatsAllHackersCached() {
85+
const TAG = `[ hacker Service # getStatsAll ]`;
8686
if (cache.get(Constants.CACHE_KEY_STATS) !== null) {
8787
logger.info(`${TAG} Getting cached stats`);
8888
return cache.get(Constants.CACHE_KEY_STATS);
8989
}
9090
const allHackers = await Hacker.find({}, logger.updateCallbackFactory(TAG, "hacker")).populate({
9191
path: "accountId",
9292
});
93+
cache.put(Constants.CACHE_KEY_STATS, stats, Constants.CACHE_TIMEOUT_STATS); //set a time-out of 5 minutes
94+
return getStats(allHackers);
95+
}
96+
97+
function getStats(hackers) {
98+
const TAG = `[ hacker Service # getStats ]`;
9399
const stats = {
94100
total: 0,
95101
status: {},
@@ -106,7 +112,7 @@ async function getStats() {
106112
age: {}
107113
};
108114

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

@@ -144,5 +149,6 @@ module.exports = {
144149
updateOne: updateOne,
145150
findIds: findIds,
146151
findByAccountId: findByAccountId,
147-
getStats: getStats
152+
getStats: getStats,
153+
getStatsAllHackersCached: getStatsAllHackersCached,
148154
};

services/search.service.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ function executeQuery(model, queryArray, page, limit, sort, sort_by, shouldExpan
6565
} else if (sort == "asc") {
6666
query.sort(sort_by);
6767
}
68-
return query.lean()
69-
.limit(limit)
68+
return query.limit(limit)
7069
.skip(limit * page)
71-
.exec()
70+
.exec();
7271
}
7372

7473
module.exports = {

0 commit comments

Comments
 (0)