Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SourceForge] Fixes, tests and cleaning up #1626

Merged
merged 3 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5500,48 +5501,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)$/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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'
});