Skip to content

Commit

Permalink
Update API so it uses new models; fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pfleidi committed Jan 17, 2016
1 parent 6714480 commit 2d14d07
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
12 changes: 1 addition & 11 deletions models/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,12 @@ module.exports = function download(bookshelf) {
}, {
/* class methods */

metaData: function (qb) {
stats: function (qb) {
return this.query(function (qb) {
qb
.sum('transferred_bytes as sentBytes')
.count('* as count');
});
},

files: function (qb) {
return this.query(function (qb) {
qb
.select('file_name as fileName')
.count('* as count')
.sum('transferred_bytes as sentBytes')
.groupBy('file_name');
});
}
});

Expand Down
13 changes: 13 additions & 0 deletions models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ module.exports = function file(bookshelf) {
downloads: function () {
return this.hasMany('Download');
}
}, {
/* class methods */

stats: function (qb) {
return this.query((qb) => {
qb
.select('path')
.count('downloads.id as count')
.sum('downloads.transferred_bytes as sentBytes')
.innerJoin('downloads', 'files.id', 'downloads.file_id')
.groupBy('files.path');
});
}
});

return bookshelf.model('File', File);
Expand Down
20 changes: 12 additions & 8 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ const Promise = require('bluebird');

module.exports = function api(config, models) {
let Download = models.Download;
let File = models.File;

function fetchMetaData() {
return Download.metaData().fetch().then(function (data) {
function fetchDownloadStats() {
return Download.stats().fetch()
.then(function (data) {
data.attributes.sentBytes = data.attributes.sentBytes || 0;

return data.attributes;
});
}

function fetchFiles() {
let files = Download.files().fetchAll();
function fetchFilesStats() {
let files = File.stats().fetchAll();

let mappedFiles = files.then(function (files) {
return files.map(function (file) {
Expand All @@ -27,11 +29,13 @@ module.exports = function api(config, models) {
}

return function (req, res) {
Promise.all([fetchMetaData(), fetchFiles()]).then(function (results) {
let metaData = results[0];
let filesData = results[1];
Promise.all(
[fetchDownloadStats(), fetchFilesStats()]
).then(function (results) {
let downloadStats = results[0];
let filesStats = results[1];

res.json(_.merge(metaData, { files: filesData }));
res.json(_.merge(downloadStats, { files: filesStats }));
});
};
};
12 changes: 8 additions & 4 deletions test/integration/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ describe('API', function () {
var file = '/testfile.img';

beforeEach(function (done) {
server.get(file).expect(200, done);
server.get(file).expect(200, () => {
setTimeout(done, 20);
});
});

it ('returns a valid files list', function (done) {
Expand All @@ -46,7 +48,7 @@ describe('API', function () {

"files": [
{
"fileName": file,
"path": file,
"count": 1,
"sentBytes": 5242880
}
Expand All @@ -62,7 +64,9 @@ describe('API', function () {
server
.get(file)
.set('Range', 'bytes=1000000-2000000')
.expect(206, done);
.expect(206, () => {
setTimeout(done, 20);
});
});

it ('returns a valid files list', function (done) {
Expand All @@ -76,7 +80,7 @@ describe('API', function () {

"files": [
{
"fileName": file,
"path": file,
"count": 1,
"sentBytes": 1000001
}
Expand Down

0 comments on commit 2d14d07

Please sign in to comment.