diff --git a/server.js b/server.js index b96faa1dc7f0e..ef0dae433c860 100644 --- a/server.js +++ b/server.js @@ -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'); @@ -5500,39 +5501,37 @@ 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'; @@ -5540,8 +5539,8 @@ cache(function(data, match, sendBadge, request) { 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); diff --git a/services/sourceforge/sourceforge.tester.js b/services/sourceforge/sourceforge.tester.js new file mode 100644 index 0000000000000..b045812248fa7 --- /dev/null +++ b/services/sourceforge/sourceforge.tester.js @@ -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' + });