Skip to content

Commit

Permalink
clean up/linting/potential leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludovic Vannoorenberghe committed Feb 3, 2017
1 parent 34d9098 commit 9f78705
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 162 deletions.
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
parserOptions: {
ecmaVersion: 5,
sourceType: 'module',
},
rules: {
'no-undef': 'error',
'no-unused-vars': ['error', { vars: 'local', args: 'after-used' }],
'no-use-before-define': 'error'
},
}
32 changes: 16 additions & 16 deletions api/controllers/BadgeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

var formatNumber = function(number){
if(number > 999999) return (number/1000000).toFixed(1) + 'M';
else if (number >999) return (number/1000).toFixed(1) + 'K';
else return number;
};

module.exports = {

/**
Expand Down Expand Up @@ -77,9 +83,9 @@ module.exports = {
downloads = data["sum_indirect"] + data["sum_direct"]
statsName = "total downloads"
}
length = 20+ (String(downloads).length + period.length+1)*6
statistics = {
downloads:_formatNumber(downloads),
var length = 20+ (String(downloads).length + period.length+1)*6
var statistics = {
downloads:formatNumber(downloads),
statsName:statsName,
period:period,
length:length
Expand Down Expand Up @@ -108,25 +114,19 @@ module.exports = {
*/
getLatestVersion: function(req, res) {
var packageName = req.param("packageName");
Package.getLatestVersionNumber(packageName).then(function(package){
if(package===null){
version="not published";
color="#e05d44";
Package.getLatestVersionNumber(packageName).then(function(_package){
if(_package===null){
var version="not published";
var color="#e05d44";
}
else{
version=package.latest_version.dataValues.version;
color="#33aacc";
var version=_package.latest_version.dataValues.version;
var color="#33aacc";
}
length=40+version.length*6;
var length = 40+version.length*6;
res.type('image/svg+xml');
res.locals.layout = null;
res.view('badges/version_badge.ejs',{data:{version:version,color:color,length:length}});
});
},
};

_formatNumber=function(number){
if(number > 999999) return (number/1000000).toFixed(1) + 'M';
else if (number >999) return (number/1000).toFixed(1) + 'K';
else return number;
};
3 changes: 0 additions & 3 deletions api/controllers/CampusController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
*
* @description :: Server-side logic for the integration of the campus-app with Rdocs
*/
var Promise = require('bluebird');
var _ = require('lodash');


module.exports = {

Expand Down
16 changes: 7 additions & 9 deletions api/controllers/CollaboratorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ var request = require('request');
var Promise = require('bluebird');
var numeral = require('numeral');



var self = module.exports = {
module.exports = {



Expand Down Expand Up @@ -64,14 +62,14 @@ var self = module.exports = {
if(packages === null) return res.notFound();
else {
var json = {name: name };
repositories = {
var repositories = {
cran: 0,
bioconductor: 0,
github: 0
};

Promise.map(packages, function(package) {
var latest = package.latest_version.toJSON();
Promise.map(packages, function(_package) {
var latest = _package.latest_version.toJSON();
if (latest.maintainer.name === name) {
latest.is_maintainer = true;
}
Expand All @@ -88,12 +86,12 @@ var self = module.exports = {
if(!json.email && collaborators.length > 0 && collaborators[0].email) {
json.email = collaborators[0].email;
}
repositories[package.repository.name] = repositories[package.repository.name]+1 || 1;
repositories[_package.repository.name] = repositories[_package.repository.name]+1 || 1;

return Package.getPackagePercentile(latest.package_name).then(function(percentileObject) {
latest.percentile = isNaN(percentileObject.percentile) ? -1 : percentileObject.percentile;
latest.totalDownloads = percentileObject.total;
latest.repoName = package.repository.name;
latest.repoName = _package.repository.name;
return latest;
});

Expand Down Expand Up @@ -121,7 +119,7 @@ var self = module.exports = {
getNumberOfDirectDownloads: function(req,res){
var name = req.param('name');
DownloadStatistic.getNumberOfDirectDownloads(name).then(function(results){
row = results[0];
var row = results[0];
row.totalStr = row.total ? numeral(row.total).format('0,0') : '';
res.json(results[0]);
});
Expand Down
5 changes: 2 additions & 3 deletions api/controllers/ExampleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
* @apiGroup Example
* @apiPermission require to be authenticated
* @apiDescription Create a new review in the specified topic.
*
*
*
*
* @apiParam {Int} topicId Id of the topic to post to review
Expand Down Expand Up @@ -80,7 +80,7 @@ module.exports = {
*
* @apiParam {Integer} exampleId The id of the example.
* @apiParam {String} text Contents of the updated example (styling via markdown).
*
*
* @apiSuccess {String} status done when the operation was successful, forbidden if the user isn't properly authenticated.
*/
updateExample: function(req, res) {
Expand Down Expand Up @@ -113,7 +113,6 @@ module.exports = {
var limit = Utils.parseLimit(req),
offset = Utils.parseSkip(req),
sort = Utils.parseSort(req) || 'created_at DESC';
var user = req.user;

return Example.findAll({
where: {
Expand Down
20 changes: 10 additions & 10 deletions api/controllers/PackageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ module.exports = {
{ model: PackageVersion, as: 'versions' },
],
order: [[sequelize.fn('ORDER_VERSION', sequelize.col('version')), 'ASC' ]]
}).then(function(package) {
if(package === null) {
}).then(function(_package) {
if(_package === null) {
return res.rstudio_redirect(301, '/search?q=' + encodeURIComponent(packageName));
//there seems to be a problem with redirected requests if text/html is set as contentype for the ajax request, so I just
//adapt this so Rstudio still gets the html
} else if(req.wantsJSON &&!req.param("viewer_pane")==1) {
return res.json(package);
return res.json(_package);
} else {
if (package.versions.length === 0)
if (_package.versions.length === 0)
return Package.findOne({
where: {name: packageName},
include: [{ model: PackageVersion, attributes: ['package_name', 'version', 'id'], as: 'reverse_dependencies'}]
}).then(function(packageInstance) {
if(packageInstance === null) return res.notFound();
var package = packageInstance.toJSON();
package.pageTitle = packageInstance.name;
return res.ok(package, 'package/show.ejs');
var _package = packageInstance.toJSON();
_package.pageTitle = packageInstance.name;
return res.ok(_package, 'package/show.ejs');
});
else return res.rstudio_redirect(301, package.versions[package.versions.length - 1].uri);
else return res.rstudio_redirect(301, _package.versions[_package.versions.length - 1].uri);
}
}).catch(function(err) {
return res.negotiate(err);
Expand Down Expand Up @@ -130,8 +130,8 @@ module.exports = {
include: [
{ model: TaskView, as: 'inViews', attributes:['name'] }
]
}).then(function(package){
var views = package.inViews;
}).then(function(_package){
var views = _package.inViews;
views.forEach(function(view){
RedisService.del('rdocs_view_show_' + view.name);
});
Expand Down
8 changes: 4 additions & 4 deletions api/controllers/PackageVersionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
var result = PackageVersion.createWithDescriptionFile({input: req.body});
result.then(function(value) {
res.location('/api/packages/' + value.package_name + '/versions/' + value.version);
key = 'view_package_version_' + value.package_name + '_' + value.version;
var key = 'view_package_version_' + value.package_name + '_' + value.version;
RedisService.del(key);
res.json(value);
}).catch(Sequelize.UniqueConstraintError, function (err) {
Expand Down Expand Up @@ -165,7 +165,7 @@ module.exports = {
},

_getDownloadStatistics: function (res, packageName) {
key = 'rdocs_download_stats_' + packageName;
var key = 'rdocs_download_stats_' + packageName;

return RedisService.getJSONFromCache(key, res, RedisService.DAILY, function() {

Expand Down Expand Up @@ -348,7 +348,7 @@ module.exports = {
* @apiSuccess {Integer} nodes.group The group to which this package belongs in the graph. The dependency and his dependencies are grouped together. When conflict the second level dependency is grouped with the most popular dependency.
* @apiSuccess {Object[]} links The links in the graph represented as a list.
* @apiSuccess {Integer} links.source The dependant package (as number in the nodes list).
* @apiSuccess {Integer} links.target The depending package (as number in the nodes list).
* @apiSuccess {Integer} links.target The depending package (as number in the nodes list).
*/
getDependencyGraph: function(req,res) {
var rootPackage = req.param('name');
Expand Down Expand Up @@ -421,7 +421,7 @@ module.exports = {
* @apiSuccess {Integer} nodes.group The group to which this package belongs in the graph. The reverse dependency and his reverse dependencies are grouped together. When conflict the second level reverse dependency is grouped with the most popular reverse dependency.
* @apiSuccess {Object[]} links The links in the graph represented as a list.
* @apiSuccess {Integer} links.source The depending package (as number in the nodes list).
* @apiSuccess {Integer} links.target The dependant package (as number in the nodes list).
* @apiSuccess {Integer} links.target The dependant package (as number in the nodes list).
*/
getReverseDependencyGraph: function(req,res) {
var rootPackage = req.param('name');
Expand Down
16 changes: 8 additions & 8 deletions api/controllers/RstudioController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
var Promise = require('bluebird');
var _ = require('lodash');

var splitByCommaOrNull = function(param){
return (typeof param != "undefined" && param.length>0)? param.split(",") : null;
}

module.exports = {

Expand All @@ -22,7 +25,7 @@ module.exports = {
**/
viewResults : function(req,res){
matches = req.param("matches")
var matches = req.param("matches")
if(matches === 0){
return ElasticSearchService.helpSearchQuery(req.param("topic"),['aliases'],true,2).then(function(json){
return res.ok(json,'rStudio/topic_not_found.ejs');
Expand Down Expand Up @@ -54,8 +57,8 @@ module.exports = {
*/
case "help":
//parse parameters
var packageNames = _splitByCommaOrNull(req.param("packages"));
var topicNames = _splitByCommaOrNull(req.param("topic_names"));
var packageNames = splitByCommaOrNull(req.param("packages"));
var topicNames = splitByCommaOrNull(req.param("topic_names"));
var topic = req.param("topic");
var help_data = RStudioService.help(packageNames,topicNames,topic);
break;
Expand All @@ -65,8 +68,8 @@ module.exports = {
*/
case "help_search":
//parse parameters
var packageNames = _splitByCommaOrNull(req.param('matching_packages'));
var topicNames = _splitByCommaOrNull(req.param('matching_titles'));
var packageNames = splitByCommaOrNull(req.param('matching_packages'));
var topicNames = splitByCommaOrNull(req.param('matching_titles'));
var pattern = req.param("query");
var fields = req.param("fields").split(",");
var fuzzy = (req.param("type") === "fuzzy")? fuzzy = true : fuzzy = false;
Expand Down Expand Up @@ -147,6 +150,3 @@ module.exports = {
}
};

_splitByCommaOrNull = function(param){
return (typeof param != "undefined" && param.length>0)? param.split(",") : null;
}
10 changes: 5 additions & 5 deletions api/controllers/TaskViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ module.exports = {
}]
}).then(function(view) {
var jsonViews = view.toJSON();
var packages = _.map(jsonViews.packages, function(package) {
var packages = _.map(jsonViews.packages, function(_package) {
var rating;
if(!package.latest_version || package.latest_version.reviews.length === 0) {
if(!_package.latest_version || _package.latest_version.reviews.length === 0) {
rating = 0;
} else {
rating = _.meanBy(package.latest_version.reviews, function(r) {
rating = _.meanBy(_package.latest_version.reviews, function(r) {
return r.rating;
});
}
package.rating = rating;
return package;
_package.rating = rating;
return _package;
});
jsonViews.packages = packages;
jsonViews.pageTitle = view.name;
Expand Down
7 changes: 3 additions & 4 deletions api/controllers/WorkerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
var packageVersion = req.body.package.version;
var result = Topic.createWithRdFile({input: req.body, packageName: packageName, packageVersion: packageVersion});
result.then(function(value) {
key = 'view_topic_' + value.id;
var key = 'view_topic_' + value.id;
RedisService.del(key);
res.json(value);
})
Expand Down Expand Up @@ -69,9 +69,9 @@ module.exports = {
console.log("Nothing new");
return res.send(200, "done");
}

res.send(200, "scheduled");
DownloadStatsService.reverseDependenciesCache = {}; //clean old cache
Promise.map(diffs, function (nDay) {
return Promise.map(diffs, function (nDay) {
console.log("Started indexing for today - " + nDay + "days");
return CronService.splittedAggregatedDownloadstats(nDay)
.catch({message: "empty"}, function() {
Expand All @@ -86,7 +86,6 @@ module.exports = {
.then(function (result) {
console.log("Finished indexing splitted stats");
DownloadStatsService.reverseDependenciesCache = {}; //clean cache
res.send(200, "done");
}).catch(function(err) {
return res.negotiate(err);
});
Expand Down
14 changes: 7 additions & 7 deletions api/models/Alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
_ = require('lodash');
var _ = require('lodash');
var Promise = require('bluebird');

module.exports = {
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports = {
group:['topic.name','topic.package_version.id','topic.id','Alias.id','topic.package_version.package_latest.name'],
order:[sequelize.fn('SUM', sequelize.col('topic.package_version.package_latest.last_month_stats.direct_downloads'))]
}).then(function(data){
allResults = _.map(data,function(record){
var allResults = _.map(data,function(record){
return {
id:record.topic.id,
package_name:record.topic.package_version.package_name,
Expand All @@ -98,7 +98,7 @@ module.exports = {
function_description:record.topic.description
};
})
return allResults;
return allResults;
}).catch(function(err){
console.log(err.message);
});
Expand Down Expand Up @@ -140,7 +140,7 @@ module.exports = {
group:['topic.name','topic.package_version.id','topic.id','Alias.id','topic.package_version.package_latest.name'],
order:[sequelize.fn('SUM', sequelize.col('topic.package_version.package_latest.last_month_stats.direct_downloads'))]
}).then(function(data){
allResults = _.map(data,function(record){
var allResults = _.map(data,function(record){
return {
id:record.topic.id,
package_name:record.topic.package_version.package_name,
Expand All @@ -149,7 +149,7 @@ module.exports = {
function_description:record.topic.description
};
})
return allResults;
return allResults;
}).catch(function(err){
console.log(err.message);
});
Expand Down Expand Up @@ -191,11 +191,11 @@ module.exports = {
group:['name','package_version.id','id','package_version.package_latest.name'],
order:[sequelize.fn('SUM', sequelize.col('package_version.package_latest.last_month_stats.direct_downloads'))]
}).then(function(data){
allResults= _.map(data,function(record){
var allResults= _.map(data,function(record){
return Alias.findAll({
where: {topic_id:record.id}
}).then(function(aliases){
alias = (aliases != null && aliases.length > 0)? aliases[0].name : "";
var alias = (aliases != null && aliases.length > 0)? aliases[0].name : "";
return {
id:record.id,
package_name:record.package_version.package_name,
Expand Down
Loading

0 comments on commit 9f78705

Please sign in to comment.