Skip to content

Commit

Permalink
[SourceForge] Fixes, tests and cleaning up (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB authored Apr 2, 2018
1 parent f2efd75 commit f98d17e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 33 deletions.
65 changes: 32 additions & 33 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const countBy = require('lodash.countby');
const dom = require('xmldom').DOMParser;
const jp = require('jsonpath');
const moment = require('moment');
const path = require('path');
const prettyBytes = require('pretty-bytes');
const queryString = require('query-string');
Expand Down Expand Up @@ -5470,48 +5471,46 @@ cache(function(data, match, sendBadge, request) {
}));

// SourceForge integration.
camp.route(/^\/sourceforge\/([^/]+)\/([^/]*)\/?(.*).(svg|png|gif|jpg|json)$/,
camp.route(/^\/sourceforge\/(dt|dm|dw|dd)\/([^/]*)\/?(.*).(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var info = match[1]; // eg, 'dm'
var project = match[2]; // eg, 'sevenzip`.
var folder = match[3];
var format = match[4];
var apiUrl = 'http://sourceforge.net/projects/' + project + '/files/' + folder + '/stats/json';
var badgeData = getBadgeData('sourceforge', data);
var time_period, start_date, end_date;
if (info.charAt(0) === 'd') {
badgeData.text[0] = getLabel('downloads', data);
// get yesterday since today is incomplete
end_date = new Date((new Date()).getTime() - 1000*60*60*24);
switch (info.charAt(1)) {
case 'm':
start_date = new Date(end_date.getTime() - 1000*60*60*24*30);
time_period = '/month';
break;
case 'w':
start_date = new Date(end_date.getTime() - 1000*60*60*24*6); // 6, since date range is inclusive
time_period = '/week';
break;
case 'd':
start_date = end_date;
time_period = '/day';
break;
case 't':
start_date = new Date(99, 0, 1);
time_period = '/total';
break;
}
const info = match[1]; // eg, 'dm'
const project = match[2]; // eg, 'sevenzip`.
const folder = match[3];
const format = match[4];
let apiUrl = 'http://sourceforge.net/projects/' + project + '/files/' + folder + '/stats/json';
const badgeData = getBadgeData('sourceforge', data);
let time_period, start_date;
badgeData.text[0] = getLabel('downloads', data);
// get yesterday since today is incomplete
const end_date = moment().subtract(24, 'hours');
switch (info.charAt(1)) {
case 'm':
start_date = moment(end_date).subtract(30, 'days');
time_period = '/month';
break;
case 'w':
start_date = moment(end_date).subtract(6, 'days'); // 6, since date range is inclusive
time_period = '/week';
break;
case 'd':
start_date = end_date;
time_period = '/day';
break;
case 't':
start_date = moment(0);
time_period = '';
break;
}
apiUrl += '?start_date=' + start_date.toISOString().slice(0,10) + '&end_date=' + end_date.toISOString().slice(0,10);
apiUrl += '?start_date=' + start_date.format("YYYY-MM-DD") + '&end_date=' + end_date.format("YYYY-MM-DD");
request(apiUrl, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
var data = JSON.parse(buffer);
var downloads = data.total;
const data = JSON.parse(buffer);
const downloads = data.total;
badgeData.text[1] = metric(downloads) + time_period;
badgeData.colorscheme = downloadCountColor(downloads);
sendBadge(format, badgeData);
Expand Down
54 changes: 54 additions & 0 deletions services/sourceforge/sourceforge.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('../service-tester');
const {
isMetric,
isMetricOverTimePeriod
} = require('../test-validators');

const t = new ServiceTester({ id: 'sourceforge', title: 'SourceForge' });
module.exports = t;

t.create('total downloads')
.get('/dt/sevenzip.json')
.expectJSONTypes(Joi.object().keys({
name: 'downloads',
value: isMetric,
}));

t.create('monthly downloads')
.get('/dm/sevenzip.json')
.expectJSONTypes(Joi.object().keys({
name: 'downloads',
value: isMetricOverTimePeriod,
}));

t.create('weekly downloads')
.get('/dw/sevenzip.json')
.expectJSONTypes(Joi.object().keys({
name: 'downloads',
value: isMetricOverTimePeriod,
}));

t.create('daily downloads')
.get('/dd/sevenzip.json')
.expectJSONTypes(Joi.object().keys({
name: 'downloads',
value: isMetricOverTimePeriod,
}));

t.create('invalid project')
.get('/dd/invalid.json')
.expectJSON({
name: 'downloads',
value: 'invalid',
});

t.create('total downloads (connection error)')
.get('/dt/sevenzip.json')
.networkOff()
.expectJSON({
name: 'downloads',
value: 'inaccessible'
});

0 comments on commit f98d17e

Please sign in to comment.