From 4e6f3dc6101ed0ccc8d690cdd3c2627d6aaf561a Mon Sep 17 00:00:00 2001 From: Hannes buseyne Date: Fri, 5 Aug 2016 19:19:00 +0200 Subject: [PATCH 1/2] solved only latest version topics --- api/controllers/RstudioController.js | 2 +- api/models/Alias.js | 31 ++++++++++++++-------------- api/services/RStudioService.js | 4 ++-- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/api/controllers/RstudioController.js b/api/controllers/RstudioController.js index 4c2b37d8..93fb8f1e 100644 --- a/api/controllers/RstudioController.js +++ b/api/controllers/RstudioController.js @@ -37,7 +37,7 @@ module.exports = { var topic = req.param("topic"); //if topicNames and packageNames where found by the local help function, search for it in the specified packages (might be none) if(packageNames != null && topicNames != null){ - return RStudioService.helpFindByTopicsAndPackages(topicNames,packageNames).then(function(json){ + return RStudioService.helpFindByTopicsAndPackages(topic,topicNames,packageNames).then(function(json){ if(json.length == 0){ //with no results : fuzzy search return ElasticSearchService.helpSearchQuery(topic,['aliases'],true,2).then(function(json){ diff --git a/api/models/Alias.js b/api/models/Alias.js index fbdf6ef0..56fa9691 100644 --- a/api/models/Alias.js +++ b/api/models/Alias.js @@ -64,12 +64,12 @@ module.exports = { include:[{ model:PackageVersion, as:'package_version', - attributes:['package_name'], + attributes:['id','package_name'], include:[{ model:Package, - as:'package', + as:'package_latest', required:true, - attributes:[], + attributes:['latest_version_id'], include:[{ model:DownloadStatistic, as:'last_month_stats', @@ -78,14 +78,13 @@ module.exports = { where:{date :{ $gte: new Date(new Date() - 30*24 * 60 * 60 * 1000) }} - }], - where:{latest_version_id:Sequelize.col('topic.package_version.id')} + }] }] }] }], where:{name:alias}, - group:['topic.name','topic.package_version.id','topic.id','Alias.name','Alias.id'], - order:[sequelize.fn('SUM', sequelize.col('topic.package_version.package.last_month_stats.direct_downloads'))] + 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){ return { @@ -101,22 +100,24 @@ module.exports = { console.log(err.message); }); }, - orderedFindByTopicsAndPackages:function(topics,packageNames){ + orderedFindByTopicsAndPackages:function(alias,topics,packageNames){ return Alias.findAll({ attributes: ['id',['name','alias']], include:[{ model:Topic, as:'topic', + required:true, attributes:['id','name','description'], include:[{ model:PackageVersion, as:'package_version', - attributes:['package_name'], + required:true, + attributes:['package_name','id'], include:[{ model:Package, - as:'package', + as:'package_latest', required:true, - attributes:[], + attributes:['latest_version_id'], include:[{ model:DownloadStatistic, as:'last_month_stats', @@ -126,9 +127,6 @@ module.exports = { $gte: new Date(new Date() - 30*24 * 60 * 60 * 1000) }} }], - where:{ - latest_version_id:Sequelize.col('topic.package_version.id'), - } }], where:{ package_name:{ @@ -142,8 +140,9 @@ module.exports = { } } }], - group:['topic.name','topic.package_version.id','topic.id','Alias.name','Alias.id'], - order:[sequelize.fn('SUM', sequelize.col('topic.package_version.package.last_month_stats.direct_downloads'))] + where:{name:alias}, + 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){ return { diff --git a/api/services/RStudioService.js b/api/services/RStudioService.js index 38f0d5a8..5768a97d 100644 --- a/api/services/RStudioService.js +++ b/api/services/RStudioService.js @@ -3,8 +3,8 @@ var _ = require('lodash'); cheerio = require('cheerio'); module.exports = { - helpFindByTopicsAndPackages :function(topicNames,packageNames){ - return Alias.orderedFindByTopicsAndPackages(topicNames,packageNames).then(_processResults); + helpFindByTopicsAndPackages :function(alias,topicNames,packageNames){ + return Alias.orderedFindByTopicsAndPackages(alias,topicNames,packageNames).then(_processResults); }, helpFindByAlias:function(alias){ return Alias.orderedFindByAlias(alias).then(_processResults); From fdcb896489e17148b0018ed798c476e2b4eada3c Mon Sep 17 00:00:00 2001 From: Hannes buseyne Date: Mon, 8 Aug 2016 10:29:41 +0200 Subject: [PATCH 2/2] added package not found --- api/controllers/RstudioController.js | 2 +- views/rStudio/package_not_found.ejs | 34 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 views/rStudio/package_not_found.ejs diff --git a/api/controllers/RstudioController.js b/api/controllers/RstudioController.js index 93fb8f1e..a6a676b9 100644 --- a/api/controllers/RstudioController.js +++ b/api/controllers/RstudioController.js @@ -86,7 +86,7 @@ module.exports = { findPackage:function(req,res){ var package = req.param("packageName"); return RStudioService.findLatestVersion(package).then(function(version){ - if(version === null) return res.rstudio_redirect(301, '/packages/' + encodeURIComponent(packageName)); + if(version === null) return res.ok([],'rStudio/package_not_found.ejs'); else { return res.rstudio_redirect(301,'/packages/'+package+'/versions/'+version.version); } diff --git a/views/rStudio/package_not_found.ejs b/views/rStudio/package_not_found.ejs new file mode 100644 index 00000000..865bce56 --- /dev/null +++ b/views/rStudio/package_not_found.ejs @@ -0,0 +1,34 @@ +<%- partial ('../shared/_navbar.ejs') %> +<% var packages = data; %> +
+
+
+

There where no packages found matching your search term

+ <%if(packages.length>0){ %> + Perhaps you meant one of these: +
+ + + + + + + + + + <% for(var i = 0; i < packages.length; ++i) { %> + <%var package = package[i];%> + + + + + <% } %> + + +
PackageDescription
<%= package.name %><%-package.description%>
No Results!
+
+ <%}%> +
+
+
+<%- partial ('../shared/_footer.ejs') %> \ No newline at end of file