mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2024-11-22 00:38:03 -06:00
Server: add requests stats endpoint
This commit is contained in:
parent
2e92c10b6c
commit
d3cd34be2f
@ -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)
|
||||
|
38
server/controllers/api/v1/requests.js
Normal file
38
server/controllers/api/v1/requests.js
Normal file
@ -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
|
||||
})
|
||||
})
|
||||
}
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
128
server/tests/api/requests.js
Normal file
128
server/tests/api/requests.js
Normal file
@ -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()
|
||||
}
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user