2017-03-25 00:35:30 +00:00
|
|
|
const redis = require('redis')
|
|
|
|
const onFinished = require('on-finished')
|
2016-04-30 23:52:43 +00:00
|
|
|
|
|
|
|
const URLFormat = /^\/((?:@[^\/@]+\/)?[^\/@]+)(?:@([^\/]+))?(\/.*)?$/
|
|
|
|
|
2017-03-25 00:35:30 +00:00
|
|
|
const logStats = (redisURL) => {
|
2016-04-30 23:52:43 +00:00
|
|
|
const redisClient = redis.createClient(redisURL)
|
|
|
|
|
|
|
|
return (req, res, next) => {
|
|
|
|
onFinished(res, () => {
|
|
|
|
const path = req.path
|
|
|
|
|
|
|
|
if (res.statusCode === 200 && path.charAt(path.length - 1) !== '/') {
|
2016-05-10 18:19:11 +00:00
|
|
|
//redisClient.zincrby([ 'request-paths', 1, path ])
|
2016-04-30 23:52:43 +00:00
|
|
|
|
|
|
|
const match = URLFormat.exec(path)
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
const packageName = match[1]
|
|
|
|
redisClient.zincrby([ 'package-requests', 1, packageName ])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
2017-03-25 00:35:30 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
logStats
|
|
|
|
}
|