From d3cd34be2fd03ea79145dfa8eb16e82d2e23191b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 19 Sep 2016 21:33:46 +0200 Subject: [PATCH] Server: add requests stats endpoint --- server/controllers/api/v1/index.js | 2 + server/controllers/api/v1/requests.js | 38 ++++++++ server/initializers/constants.js | 6 +- server/models/request.js | 11 ++- server/tests/api/check-params.js | 21 ++++- server/tests/api/requests.js | 128 ++++++++++++++++++++++++++ 6 files changed, 199 insertions(+), 7 deletions(-) create mode 100644 server/controllers/api/v1/requests.js create mode 100644 server/tests/api/requests.js diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js index af41bc280..2e4fb2dab 100644 --- a/server/controllers/api/v1/index.js +++ b/server/controllers/api/v1/index.js @@ -7,12 +7,14 @@ const router = express.Router() const clientsController = require('./clients') const podsController = require('./pods') const remoteController = require('./remote') +const requestsController = require('./requests') const usersController = require('./users') const videosController = require('./videos') router.use('/clients', clientsController) router.use('/pods', podsController) router.use('/remote', remoteController) +router.use('/requests', requestsController) router.use('/users', usersController) router.use('/videos', videosController) router.use('/*', badRequest) diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js new file mode 100644 index 000000000..6018bb40c --- /dev/null +++ b/server/controllers/api/v1/requests.js @@ -0,0 +1,38 @@ +'use strict' + +const express = require('express') +const mongoose = require('mongoose') + +const constants = require('../../../initializers/constants') +const middlewares = require('../../../middlewares') +const admin = middlewares.admin +const oAuth = middlewares.oauth + +const Request = mongoose.model('Request') + +const router = express.Router() + +router.get('/stats', + oAuth.authenticate, + admin.ensureIsAdmin, + getStatsRequests +) + +// --------------------------------------------------------------------------- + +module.exports = router + +// --------------------------------------------------------------------------- + +function getStatsRequests (req, res, next) { + Request.list(function (err, requests) { + if (err) return next(err) + + const remainingMilliSeconds = constants.REQUESTS_INTERVAL - (Date.now() % constants.REQUESTS_INTERVAL) + + return res.json({ + requests: requests, + remainingMilliSeconds: remainingMilliSeconds + }) + }) +} diff --git a/server/initializers/constants.js b/server/initializers/constants.js index dd4eff493..76ebb8681 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -55,7 +55,7 @@ const FRIEND_SCORE = { } // Time to wait between requests to the friends (10 min) -let INTERVAL = 600000 +let REQUESTS_INTERVAL = 600000 const OAUTH_LIFETIME = { ACCESS_TOKEN: 3600 * 4, // 4 hours @@ -109,7 +109,7 @@ const USER_ROLES = { // Special constants for a test instance if (isTestInstance() === true) { FRIEND_SCORE.BASE = 20 - INTERVAL = 10000 + REQUESTS_INTERVAL = 10000 CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 } @@ -121,7 +121,7 @@ module.exports = { CONFIG: CONFIG, CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, FRIEND_SCORE: FRIEND_SCORE, - INTERVAL: INTERVAL, + REQUESTS_INTERVAL: REQUESTS_INTERVAL, OAUTH_LIFETIME: OAUTH_LIFETIME, PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, PODS_SCORE: PODS_SCORE, diff --git a/server/models/request.js b/server/models/request.js index 4d521919a..4e510e18a 100644 --- a/server/models/request.js +++ b/server/models/request.js @@ -19,14 +19,15 @@ let timer = null const RequestSchema = mongoose.Schema({ request: mongoose.Schema.Types.Mixed, - to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'users' } ] + to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ] }) RequestSchema.statics = { activate, deactivate, flush, - forceSend + forceSend, + list } RequestSchema.pre('save', function (next) { @@ -53,7 +54,7 @@ mongoose.model('Request', RequestSchema) function activate () { logger.info('Requests scheduler activated.') - timer = setInterval(makeRequests.bind(this), constants.INTERVAL) + timer = setInterval(makeRequests.bind(this), constants.REQUESTS_INTERVAL) } function deactivate () { @@ -72,6 +73,10 @@ function forceSend () { makeRequests.call(this) } +function list (callback) { + this.find({ }, callback) +} + // --------------------------------------------------------------------------- // Make a requests to friends of a certain type diff --git a/server/tests/api/check-params.js b/server/tests/api/check-params.js index f060b2ad4..a696bc171 100644 --- a/server/tests/api/check-params.js +++ b/server/tests/api/check-params.js @@ -13,6 +13,7 @@ const usersUtils = require('../utils/users') describe('Test parameters validator', function () { let server = null + let userAccessToken = null // --------------------------------------------------------------- @@ -496,7 +497,6 @@ describe('Test parameters validator', function () { describe('Of the users API', function () { const path = '/api/v1/users/' let userId = null - let userAccessToken = null describe('When listing users', function () { it('Should fail with a bad start pagination', function (done) { @@ -721,6 +721,25 @@ describe('Test parameters validator', function () { }) }) + describe('Of the requests API', function () { + const path = '/api/v1/requests/stats' + + it('Should fail with an non authenticated user', function (done) { + request(server.url) + .get(path) + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('Should fail with a non admin user', function (done) { + request(server.url) + .get(path) + .set('Authorization', 'Bearer ' + userAccessToken) + .set('Accept', 'application/json') + .expect(403, done) + }) + }) + after(function (done) { process.kill(-server.app.pid) diff --git a/server/tests/api/requests.js b/server/tests/api/requests.js new file mode 100644 index 000000000..af36f6e34 --- /dev/null +++ b/server/tests/api/requests.js @@ -0,0 +1,128 @@ +'use strict' + +const chai = require('chai') +const each = require('async/each') +const expect = chai.expect +const request = require('supertest') + +const loginUtils = require('../utils/login') +const podsUtils = require('../utils/pods') +const serversUtils = require('../utils/servers') +const videosUtils = require('../utils/videos') + +describe('Test requests stats', function () { + const path = '/api/v1/requests/stats' + let servers = [] + + function uploadVideo (server, callback) { + const name = 'my super video' + const description = 'my super description' + const tags = [ 'tag1', 'tag2' ] + const fixture = 'video_short.webm' + + videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback) + } + + function getRequestsStats (server, callback) { + request(server.url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + server.accessToken) + .expect(200) + .end(callback) + } + + // --------------------------------------------------------------- + + before(function (done) { + this.timeout(20000) + serversUtils.flushAndRunMultipleServers(2, function (serversRun, urlsRun) { + servers = serversRun + + each(servers, function (server, callbackEach) { + loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { + if (err) return callbackEach(err) + + server.accessToken = accessToken + callbackEach() + }) + }, function (err) { + if (err) throw err + + const server1 = servers[0] + podsUtils.makeFriends(server1.url, server1.accessToken, done) + }) + }) + }) + + it('Should have a correct timer', function (done) { + const server = servers[0] + + getRequestsStats(server, function (err, res) { + if (err) throw err + + const body = res.body + expect(body.remainingMilliSeconds).to.be.at.least(0) + expect(body.remainingMilliSeconds).to.be.at.most(10000) + + done() + }) + }) + + it('Should have the correct request', function (done) { + this.timeout(15000) + + const server = servers[0] + // Ensure the requests of pod 1 won't be made + servers[1].app.kill() + + uploadVideo(server, function (err) { + if (err) throw err + + getRequestsStats(server, function (err, res) { + if (err) throw err + + const body = res.body + expect(body.requests).to.have.lengthOf(1) + + const request = body.requests[0] + expect(request.to).to.have.lengthOf(1) + expect(request.request.type).to.equal('add') + + // Wait one cycle + setTimeout(done, 10000) + }) + }) + }) + + it('Should have the correct requests', function (done) { + const server = servers[0] + + uploadVideo(server, function (err) { + if (err) throw err + + getRequestsStats(server, function (err, res) { + if (err) throw err + + const body = res.body + expect(body.requests).to.have.lengthOf(2) + + const request = body.requests[1] + expect(request.to).to.have.lengthOf(1) + expect(request.request.type).to.equal('add') + + done() + }) + }) + }) + + after(function (done) { + process.kill(-servers[0].app.pid) + + if (this.ok) { + serversUtils.flushTests(done) + } else { + done() + } + }) +})