mirror of
https://github.com/Foltik/Shimapan
synced 2025-02-26 00:55:20 -05:00
Remove old stats routes
This commit is contained in:
parent
c6011b751c
commit
8c440ffa6f
@ -9,45 +9,12 @@ const wrap = require('../../util/wrap');
|
||||
const bodyVerifier = require('../../util/verifyBody').bodyVerifier;
|
||||
const requireAuth = require('../../util/auth').requireAuth;
|
||||
|
||||
const oneDay = 1000 * 60 * 60 * 24;
|
||||
|
||||
function filterAggregateStats(obj) {
|
||||
if (obj._id)
|
||||
delete obj._id;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function mergeAggregateStats(obj1, obj2) {
|
||||
filterAggregateStats(obj1);
|
||||
filterAggregateStats(obj2);
|
||||
|
||||
let res = {};
|
||||
Object.assign(res, obj1, obj2);
|
||||
return res;
|
||||
}
|
||||
|
||||
function mergeAggregations(res1, res2) {
|
||||
const arr = res1.concat(res2);
|
||||
|
||||
let res = {};
|
||||
|
||||
for (let obj of arr) {
|
||||
if (res[obj._id])
|
||||
res[obj._id] = mergeAggregateStats(res[obj._id], obj);
|
||||
else
|
||||
res[obj._id] = filterAggregateStats(obj);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
const uploadProps = [
|
||||
{name: 'after', type: 'date', optional: true},
|
||||
{name: 'before', type: 'date', optional: true},
|
||||
{name: 'limit', type: 'number', min: 1, max: 10000, optional: true}
|
||||
];
|
||||
|
||||
router.get('/uploads', requireAuth('stats.get'), bodyVerifier(uploadProps), wrap(async (req, res) => {
|
||||
let constraints = {uploader: req.username};
|
||||
|
||||
@ -84,11 +51,13 @@ router.get('/uploads', requireAuth('stats.get'), bodyVerifier(uploadProps), wrap
|
||||
res.status(200).json(uploads);
|
||||
}));
|
||||
|
||||
|
||||
const viewProps = [
|
||||
{name: 'after', type: 'date', optional: true},
|
||||
{name: 'before', type: 'date', optional: true},
|
||||
{name: 'limit', type: 'number', min: 1, max: 10000, optional: true}
|
||||
];
|
||||
|
||||
router.get('/views', requireAuth('stats.get'), bodyVerifier(viewProps), wrap(async (req, res) => {
|
||||
let constraints = {uploader: req.username};
|
||||
|
||||
@ -119,104 +88,4 @@ router.get('/views', requireAuth('stats.get'), bodyVerifier(viewProps), wrap(asy
|
||||
res.status(200).json(views);
|
||||
}));
|
||||
|
||||
router.get('/week', requireAuth('stats.get'), wrap(async (req, res) => {
|
||||
const currentDate = new Date();
|
||||
|
||||
const uploadStats = await (Upload.collection.aggregate([
|
||||
{
|
||||
$match: {
|
||||
'date': {$gt: new Date(currentDate - 7 * oneDay)},
|
||||
'uploader': req.username
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
'date': {
|
||||
$concat: [
|
||||
{$substr: ['$date', 5, 2]},
|
||||
'-',
|
||||
{$substr: ['$date', 8, 2]}
|
||||
]
|
||||
},
|
||||
'size': '$file.size'
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
'_id': '$date',
|
||||
'uploads': {$sum: 1},
|
||||
'size': {$sum: '$size'}
|
||||
}
|
||||
}
|
||||
]).toArray());
|
||||
const viewStats = await (View.collection.aggregate([
|
||||
{
|
||||
$match: {
|
||||
'date': {$gt: new Date(currentDate - 7 * oneDay)},
|
||||
'uploader': req.username
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
'date': {
|
||||
$concat: [
|
||||
{$substr: ['$date', 5, 2]},
|
||||
'-',
|
||||
{$substr: ['$date', 8, 2]}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
'_id': '$date',
|
||||
'views': {$sum: 1},
|
||||
}
|
||||
}
|
||||
]).toArray());
|
||||
|
||||
const stats = mergeAggregations(uploadStats, viewStats);
|
||||
|
||||
res.status(200).json(stats);
|
||||
}));
|
||||
|
||||
router.get('/all', requireAuth('stats.get'), wrap(async (req, res) => {
|
||||
const uploadStats = await (Upload.collection.aggregate([
|
||||
{
|
||||
$match: {
|
||||
'uploader': req.username
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
'size': '$file.size'
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
'_id': 'total',
|
||||
'count': {$sum: 1},
|
||||
'size': {$sum: '$size'}
|
||||
}
|
||||
}
|
||||
]).toArray());
|
||||
const viewStats = await (View.collection.aggregate([
|
||||
{
|
||||
$match: {
|
||||
'uploader': req.username
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
'_id': 'total',
|
||||
'views': {$sum: 1},
|
||||
}
|
||||
}
|
||||
]).toArray());
|
||||
|
||||
const stats = mergeAggregations(uploadStats, viewStats);
|
||||
|
||||
res.status(200).json(stats);
|
||||
}));
|
||||
|
||||
module.exports = router;
|
126
test/api.js
126
test/api.js
@ -944,55 +944,6 @@ describe('Users', () => {
|
||||
});
|
||||
|
||||
describe('Stats', () => {
|
||||
const setupUploadsAndViews = async () => {
|
||||
const currentDate = new Date();
|
||||
const oneDay = 1000 * 60 * 60 * 24;
|
||||
|
||||
const get_uid = i => {
|
||||
return 'abcde' + String.fromCharCode(i + 97)
|
||||
};
|
||||
|
||||
for(let i = 0; i < 8; i++) {
|
||||
await util.insertUpload({
|
||||
uid: get_uid(i),
|
||||
views: 0,
|
||||
uploader: 'user',
|
||||
uploaderKey: null,
|
||||
date: new Date(currentDate - i * oneDay),
|
||||
file: {
|
||||
size: 1
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
await util.insertUpload({
|
||||
uid: 'zyxwvu',
|
||||
uploader: 'someguy',
|
||||
date: new Date(currentDate - 3 * oneDay),
|
||||
file: {
|
||||
size: 1
|
||||
}
|
||||
});
|
||||
|
||||
for(let i = 0; i < 8; i++) {
|
||||
await util.insertView({
|
||||
uid: get_uid(i),
|
||||
uploader: 'user',
|
||||
remoteAddress: '::1',
|
||||
userAgent: 'fiyerfocks',
|
||||
date: new Date(currentDate - i * oneDay),
|
||||
});
|
||||
}
|
||||
|
||||
await util.insertView({
|
||||
uid: 'zyxwvu',
|
||||
uploader: 'someguy',
|
||||
remoteAddress: '::1',
|
||||
userAgent: 'fiyerfocks',
|
||||
date: new Date(currentDate - 3 * oneDay)
|
||||
});
|
||||
};
|
||||
|
||||
describe('/GET uploads', () => {
|
||||
describe('0 Valid Request', () => {
|
||||
const currentDate = new Date();
|
||||
@ -1253,83 +1204,6 @@ describe('Stats', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('/GET week', () => {
|
||||
describe('0 Valid Request', () => {
|
||||
it('must return valid stats for the past week', async () => {
|
||||
await setupUploadsAndViews();
|
||||
|
||||
const oneDay = 1000 * 60 * 60 * 24;
|
||||
const currentDate = new Date();
|
||||
|
||||
await util.createSession(agent, ['stats.get'], 'user');
|
||||
const stats = (await util.getStatsWeek(agent)).body;
|
||||
console.log(stats);
|
||||
|
||||
for(let i = 0; i < 7; i++) {
|
||||
let date = new Date(currentDate - i * oneDay).toISOString();
|
||||
let dateStr = date.substr(5, 2) + '-' + date.substr(8, 2);
|
||||
let dayStats = stats[dateStr];
|
||||
dayStats.should.be.a('object', 'Stats should exist for the day ' + dateStr);
|
||||
dayStats.uploads.should.equal(1, 'Should be only one upload for the day ' + dateStr);
|
||||
dayStats.size.should.equal(1, 'Should be only one byte uploaded for the day ' + dateStr);
|
||||
dayStats.views.should.equal(1, 'Should be only one view for the day ' + dateStr);
|
||||
}
|
||||
|
||||
let pastDate = new Date(currentDate - 7 * oneDay).toISOString();
|
||||
let pastDateStr = pastDate.substr(5, 2) + '-' + pastDate.substr(8, 2);
|
||||
stats.should.not.have.property(pastDateStr, 'No stats should exist past 1 week ago');
|
||||
|
||||
return util.logout(agent);
|
||||
});
|
||||
|
||||
it('must return an empty set when there are no stats', async () => {
|
||||
await util.createSession(agent, ['stats.get'], 'user');
|
||||
const stats_self = (await util.getStatsWeek(agent)).body;
|
||||
stats_self.should.deep.equal({});
|
||||
|
||||
|
||||
await setupUploadsAndViews();
|
||||
await util.createSession(agent, ['stats.get'], 'other_user');
|
||||
const stats_other = (await util.getStatsWeek(agent)).body;
|
||||
stats_other.should.deep.equal({}, 'No stats should be returned from another user');
|
||||
|
||||
return util.logout(agent);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('/GET all', () => {
|
||||
describe('0 Valid Request', () => {
|
||||
it('must return valid stats for all time', async () => {
|
||||
await setupUploadsAndViews();
|
||||
|
||||
await util.createSession(agent, ['stats.get'], 'user');
|
||||
const stats = (await util.getStatsAll(agent)).body;
|
||||
|
||||
stats.should.have.property('total');
|
||||
stats.total.should.have.property('count').equal(8);
|
||||
stats.total.should.have.property('size').equal(8);
|
||||
stats.total.should.have.property('views').equal(8);
|
||||
|
||||
return util.logout(agent);
|
||||
});
|
||||
|
||||
it('must return an empty set when there are no stats', async () => {
|
||||
await util.createSession(agent, ['stats.get'], 'user');
|
||||
const stats_self = (await util.getStatsAll(agent)).body;
|
||||
stats_self.should.deep.equal({}, 'No stats should be returned');
|
||||
await util.logout(agent);
|
||||
|
||||
await setupUploadsAndViews();
|
||||
await util.createSession(agent, ['stats.get'], 'other_user');
|
||||
const stats_other = (await util.getStatsAll(agent)).body;
|
||||
stats_other.should.deep.equal({}, 'No stats should be returned from another user');
|
||||
|
||||
return util.logout(agent);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
after(() => server.close(() => process.exit(0)));
|
||||
|
Loading…
Reference in New Issue
Block a user