Merge: nitweb: use catalog as homepage
authorJean Privat <jean@pryen.org>
Mon, 6 Jun 2016 15:12:12 +0000 (11:12 -0400)
committerJean Privat <jean@pryen.org>
Mon, 6 Jun 2016 15:12:12 +0000 (11:12 -0400)
Used the Catalog API with Nitweb to provide a homepage.

Demo: http://nitweb.moz-code.org/

Pull-Request: #2162
Reviewed-by: Jean Privat <jean@pryen.org>

13 files changed:
share/nitweb/directives/contributor-list.html [new file with mode: 0644]
share/nitweb/index.html
share/nitweb/javascripts/entities.js
share/nitweb/javascripts/index.js [new file with mode: 0644]
share/nitweb/javascripts/model.js
share/nitweb/javascripts/nitweb.js
share/nitweb/stylesheets/nitweb.css
share/nitweb/views/index.html
src/nitweb.nit
src/web/api_catalog.nit [new file with mode: 0644]
src/web/model_api.nit
src/web/web.nit
src/web/web_base.nit

diff --git a/share/nitweb/directives/contributor-list.html b/share/nitweb/directives/contributor-list.html
new file mode 100644 (file)
index 0000000..a46cce9
--- /dev/null
@@ -0,0 +1,11 @@
+<div ng-if='listContributors.length > 0'>
+       <h3 id={{listId}}>
+               <span>{{listTitle}}</span>
+       </h3>
+       <ul class='list-unstyled user-list'>
+               <li ng-repeat='contributor in listContributors'>
+                       <img class='avatar' src="https://secure.gravatar.com/avatar/{{contributor.hash}}?size=20&amp;default=retro">
+                       {{contributor.name}}
+               </li>
+       </ul>
+</div>
index 17bcd23..1b99a81 100644 (file)
@@ -56,5 +56,6 @@
                <script src='/javascripts/model.js'></script>
                <script src='/javascripts/entities.js'></script>
                <script src='/javascripts/ui.js'></script>
+               <script src='/javascripts/index.js'></script>
        </body>
 </html>
index 255602c..9d9a452 100644 (file)
@@ -94,6 +94,7 @@
                                restrict: 'E',
                                scope: {
                                        listEntities: '=',
+                                       listId: '@',
                                        listTitle: '@',
                                        listObjectFilter: '=',
                                },
diff --git a/share/nitweb/javascripts/index.js b/share/nitweb/javascripts/index.js
new file mode 100644 (file)
index 0000000..534f462
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+(function() {
+       angular
+               .module('index', ['model', 'ngSanitize'])
+
+               .run(['$anchorScroll', function($anchorScroll) {
+                       $anchorScroll.yOffset = 80;
+               }])
+
+               .controller('IndexCtrl', ['Catalog', '$routeParams', '$sce', '$scope', '$location', '$anchorScroll', function(Catalog, $routeParams, $sce, $scope, $location, $anchorScroll) {
+                       this.loadHighlighted = function() {
+                               Catalog.loadHightlighted(
+                                       function(data) {
+                                               $scope.highlighted = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadMostRequired = function() {
+                               Catalog.loadMostRequired(
+                                       function(data) {
+                                               $scope.required = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadByTags = function() {
+                               Catalog.loadByTags(
+                                       function(data) {
+                                               $scope.bytags = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadStats = function() {
+                               Catalog.loadStats(
+                                       function(data) {
+                                               $scope.stats = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadContributors = function() {
+                               Catalog.loadContributors(
+                                       function(data) {
+                                               $scope.contributors = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+
+                       this.scrollTo = function(hash) {
+                               $anchorScroll(hash);
+                       }
+
+                       this.loadHighlighted();
+                       this.loadStats();
+                       this.loadContributors();
+               }])
+
+               .directive('contributorList', ['Model', function(Model) {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       listId: '@',
+                                       listTitle: '@',
+                                       listContributors: '='
+                               },
+                               templateUrl: '/directives/contributor-list.html'
+                       };
+               }])
+})();
index 7d5051b..3bf927c 100644 (file)
@@ -22,6 +22,7 @@
 
                .factory('Model', [ '$http', function($http) {
                        return {
+
                                loadEntity: function(id, cb, cbErr) {
                                        $http.get(apiUrl + '/entity/' + id)
                                                .success(cb)
                                }
                        };
                }])
+
+               .factory('Catalog', [ '$http', function($http) {
+                       return {
+                               loadHightlighted: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/highlighted')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadMostRequired: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/required')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadByTags: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/bytags')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadStats: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/stats')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadContributors: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/contributors')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+                       }
+               }])
 })();
index 28291fb..93b7c3a 100644 (file)
  */
 
 (function() {
-       angular.module('nitweb', ['ngRoute', 'ngSanitize', 'entities'])
+       angular.module('nitweb', ['ngRoute', 'ngSanitize', 'entities', 'index'])
 
        .config(function($routeProvider, $locationProvider) {
                $routeProvider
                        .when('/', {
-                               templateUrl: 'views/index.html'
+                               templateUrl: 'views/index.html',
+                               controller: 'IndexCtrl',
+                               controllerAs: 'indexCtrl'
                        })
                        .when('/package/:id', {
                                templateUrl: 'views/package.html',
index 2d38457..eb82ee6 100644 (file)
@@ -206,6 +206,14 @@ entity-list:hover .btn-filter {
        margin-bottom: 0px;
 }
 /*
+ * Users
+ */
+
+.avatar {
+       border-radius: 2px;
+}
+
+/*
  * Code Highlighting
  */
 
index 9f9b93d..b7bee8e 100644 (file)
@@ -1 +1,70 @@
-<h1>Hello nitweb!</h1>
+<div class='container-fluid'>
+       <div class='page-header'>
+               <h2>Welcome to NitWeb!</h2>
+               <p class='text-muted'>The Nit knowledge base.</p>
+       </div>
+
+       <ul class='nav nav-tabs' role='tablist'>
+               <li role='presentation' class='active'>
+                       <a data-toggle='tab' role='tab' data-target='#highlighted' aria-controls='highlighted'>
+                               <span class='glyphicon glyphicon-book'/> Highlighed
+                       </a>
+               </li>
+               <li role='presentation'>
+                       <a data-toggle='tab' role='tab' data-target='#required' aria-controls='required'
+                               ng-click='indexCtrl.loadMostRequired()'>
+                               <span class='glyphicon glyphicon-book'/> Most required
+                       </a>
+               </li>
+               <li role='presentation'>
+                       <a data-toggle='tab' role='tab' data-target='#bytags' aria-controls='bytags'
+                               ng-click='indexCtrl.loadByTags()'>
+                               <span class='glyphicon glyphicon-book'/> By tags
+                       </a>
+               </li>
+       </ul>
+       <table class='table'>
+               <tr>
+                       <td ng-repeat='(key, value) in stats'>
+                               <h5><strong>{{value}}</strong>&nbsp;<span>{{key}}</span></h5>
+                       </td>
+               </tr>
+       </table>
+
+       <div class='container-fluid'>
+               <div class='col-xs-9'>
+                       <div class='tab-content'>
+                               <div role='tabpanel' class='tab-pane fade in active' id='highlighted'>
+                                       <entity-list list-title='Highlighted packages'
+                                               list-entities='highlighted'
+                                               list-object-filter='{}' />
+                               </div>
+                               <div role='tabpanel' class='tab-pane fade' id='required'>
+                                       <entity-list list-title='Most required'
+                                               list-entities='required'
+                                               list-object-filter='{}' />
+                               </div>
+                               <div role='tabpanel' class='tab-pane fade' id='bytags'>
+                                       <h3>Tags</h3>
+                                       <div class='container-fluid'>
+                                               <div class='col-xs-3' ng-repeat='(tag, packages) in bytags'>
+                                                       <span class='badge'>{{packages.length}}</span>
+                                                       <a ng-click='indexCtrl.scrollTo(tag)'>{{tag}}</a>
+                                               </div>
+                                       </div>
+                                       <div ng-repeat='(tag, packages) in bytags'>
+                                               <entity-list list-id='{{tag}}' list-title='{{tag}}'
+                                                       list-entities='packages'
+                                                       list-object-filter='{}' />
+                                       </div>
+                               </div>
+                       </div>
+               </div>
+               <div class='col-xs-3'>
+                       <contributor-list list-title='Maintainers'
+                                       list-contributors='contributors.maintainers' />
+                       <contributor-list list-title='Contributors'
+                                       list-contributors='contributors.contributors' />
+               </div>
+       </div>
+</div>
index 93d15aa..7eba23d 100644 (file)
@@ -43,13 +43,30 @@ private class NitwebPhase
                var model = mainmodule.model
                var modelbuilder = toolcontext.modelbuilder
 
+               # Build catalog
+               var catalog = new Catalog(modelbuilder)
+               for mpackage in model.mpackages do
+                       catalog.deps.add_node(mpackage)
+                       for mgroup in mpackage.mgroups do
+                               for mmodule in mgroup.mmodules do
+                                       for imported in mmodule.in_importation.direct_greaters do
+                                               var ip = imported.mpackage
+                                               if ip == null or ip == mpackage then continue
+                                               catalog.deps.add_edge(mpackage, ip)
+                                       end
+                               end
+                       end
+                       catalog.git_info(mpackage)
+                       catalog.package_page(mpackage)
+               end
+
                # Run the server
                var host = toolcontext.opt_host.value or else "localhost"
                var port = toolcontext.opt_port.value
 
                var app = new App
 
-               app.use("/api", new APIRouter(model, modelbuilder, mainmodule))
+               app.use("/api", new APIRouter(model, modelbuilder, mainmodule, catalog))
                app.use("/doc/:namespace", new DocAction(model, mainmodule, modelbuilder))
                app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
 
@@ -57,6 +74,33 @@ private class NitwebPhase
        end
 end
 
+# Group all api handlers in one router.
+class APIRouter
+       super Router
+
+       # Model to pass to handlers.
+       var model: Model
+
+       # ModelBuilder to pass to handlers.
+       var modelbuilder: ModelBuilder
+
+       # Mainmodule to pass to handlers.
+       var mainmodule: MModule
+
+       # Catalog to pass to handlers.
+       var catalog: Catalog
+
+       init do
+               use("/catalog", new APICatalogRouter(model, mainmodule, catalog))
+               use("/list", new APIList(model, mainmodule))
+               use("/search", new APISearch(model, mainmodule))
+               use("/random", new APIRandom(model, mainmodule))
+               use("/entity/:id", new APIEntity(model, mainmodule))
+               use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
+               use("/uml/:id", new APIEntityUML(model, mainmodule))
+       end
+end
+
 # build toolcontext
 var toolcontext = new ToolContext
 var tpl = new Template
diff --git a/src/web/api_catalog.nit b/src/web/api_catalog.nit
new file mode 100644 (file)
index 0000000..72d207f
--- /dev/null
@@ -0,0 +1,138 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module api_catalog
+
+import web_base
+import catalog
+
+# Group all api handlers in one router.
+class APICatalogRouter
+       super Router
+
+       # Model to pass to handlers.
+       var model: Model
+
+       # Mainmodule to pass to handlers.
+       var mainmodule: MModule
+
+       # Catalog to pass to handlers.
+       var catalog: Catalog
+
+       init do
+               use("/highlighted", new APICatalogHighLighted(model, mainmodule, catalog))
+               use("/required", new APICatalogMostRequired(model, mainmodule, catalog))
+               use("/bytags", new APICatalogByTags(model, mainmodule, catalog))
+               use("/contributors", new APICatalogContributors(model, mainmodule, catalog))
+               use("/stats", new APICatalogStats(model, mainmodule, catalog))
+       end
+end
+
+abstract class APICatalogHandler
+       super APIHandler
+
+       var catalog: Catalog
+
+       # List the 10 best packages from `cpt`
+       fun list_best(cpt: Counter[MPackage]): JsonArray do
+               var res = new JsonArray
+               var best = cpt.sort
+               for i in [1..10] do
+                       if i > best.length then break
+                       res.add best[best.length-i]
+               end
+               return res
+       end
+
+       # List packages by group.
+       fun list_by(map: MultiHashMap[Object, MPackage]): JsonObject do
+               var res = new JsonObject
+               var keys = map.keys.to_a
+               alpha_comparator.sort(keys)
+               for k in keys do
+                       var projs = map[k].to_a
+                       alpha_comparator.sort(projs)
+                       res[k.to_s.html_escape] = new JsonArray.from(projs)
+               end
+               return res
+       end
+end
+
+class APICatalogStats
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               var obj = new JsonObject
+               obj["packages"] = model.mpackages.length
+               obj["maintainers"] = catalog.maint2proj.length
+               obj["contributors"] = catalog.contrib2proj.length
+               obj["modules"] = catalog.mmodules.sum
+               obj["classes"] = catalog.mclasses.sum
+               obj["methods"] = catalog.mmethods.sum
+               obj["loc"] = catalog.loc.sum
+               res.json obj
+       end
+end
+
+class APICatalogHighLighted
+       super APICatalogHandler
+
+       redef fun get(req, res) do res.json list_best(catalog.score)
+end
+
+class APICatalogMostRequired
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               if catalog.deps.not_empty then
+                       var reqs = new Counter[MPackage]
+                       for p in model.mpackages do
+                               reqs[p] = catalog.deps[p].smallers.length - 1
+                       end
+                       res.json list_best(reqs)
+                       return
+               end
+               res.json new JsonArray
+       end
+end
+
+class APICatalogByTags
+       super APICatalogHandler
+
+       redef fun get(req, res) do res.json list_by(catalog.tag2proj)
+end
+
+class APICatalogContributors
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               var obj = new JsonObject
+               obj["maintainers"] = new JsonArray.from(catalog.maint2proj.keys)
+               obj["contributors"] = new JsonArray.from(catalog.contrib2proj.keys)
+               res.json obj
+       end
+end
+
+redef class Person
+       super Jsonable
+
+       redef fun to_json do
+               var obj = new JsonObject
+               obj["name"] = name
+               obj["email"] = email
+               obj["page"] = page
+               obj["hash"] = (email or else "").md5.to_lower
+               return obj.to_json
+       end
+end
index 9bbe5ad..080dcb2 100644 (file)
@@ -18,65 +18,6 @@ import web_base
 import highlight
 import uml
 
-# Specific handler for nitweb API.
-abstract class APIHandler
-       super ModelHandler
-
-       # The JSON API does not filter anything by default.
-       #
-       # So we can cache the model view.
-       var view: ModelView is lazy do
-               var view = new ModelView(model)
-               view.min_visibility = private_visibility
-               view.include_fictive = true
-               view.include_empty_doc = true
-               view.include_attribute = true
-               view.include_test_suite = true
-               return view
-       end
-
-       # Try to load the mentity from uri with `/:id`.
-       #
-       # Send 400 if `:id` is null.
-       # Send 404 if no entity is found.
-       # Return null in both cases.
-       fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do
-               var id = req.param("id")
-               if id == null then
-                       res.error 400
-                       return null
-               end
-               var mentity = find_mentity(view, id)
-               if mentity == null then
-                       res.error 404
-               end
-               return mentity
-       end
-end
-
-# Group all api handlers in one router.
-class APIRouter
-       super Router
-
-       # Model to pass to handlers.
-       var model: Model
-
-       # ModelBuilder to pass to handlers.
-       var modelbuilder: ModelBuilder
-
-       # Mainmodule to pass to handlers.
-       var mainmodule: MModule
-
-       init do
-               use("/list", new APIList(model, mainmodule))
-               use("/search", new APISearch(model, mainmodule))
-               use("/random", new APIRandom(model, mainmodule))
-               use("/entity/:id", new APIEntity(model, mainmodule))
-               use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
-               use("/uml/:id", new APIEntityUML(model, mainmodule))
-       end
-end
-
 # List all mentities.
 #
 # MEntities can be filtered on their kind using the `k` parameter.
index c6a12f0..2a22001 100644 (file)
@@ -17,3 +17,4 @@ module web
 
 import web_actions
 import model_api
+import api_catalog
index ec9bf75..fcaac53 100644 (file)
@@ -51,6 +51,42 @@ class ModelHandler
        end
 end
 
+# Specific handler for nitweb API.
+abstract class APIHandler
+       super ModelHandler
+
+       # The JSON API does not filter anything by default.
+       #
+       # So we can cache the model view.
+       var view: ModelView is lazy do
+               var view = new ModelView(model)
+               view.min_visibility = private_visibility
+               view.include_fictive = true
+               view.include_empty_doc = true
+               view.include_attribute = true
+               view.include_test_suite = true
+               return view
+       end
+
+       # Try to load the mentity from uri with `/:id`.
+       #
+       # Send 400 if `:id` is null.
+       # Send 404 if no entity is found.
+       # Return null in both cases.
+       fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do
+               var id = req.param("id")
+               if id == null then
+                       res.error 400
+                       return null
+               end
+               var mentity = find_mentity(view, id)
+               if mentity == null then
+                       res.error 404
+               end
+               return mentity
+       end
+end
+
 # A NitView is rendered by an action.
 interface NitView
        # Renders this view and returns something that can be written to a HTTP response.