1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
20 # Group all api handlers in one router.
21 class APICatalogRouter
24 # Model to pass to handlers.
27 # Mainmodule to pass to handlers.
28 var mainmodule
: MModule
30 # Catalog to pass to handlers.
34 use
("/highlighted", new APICatalogHighLighted(model
, mainmodule
, catalog
))
35 use
("/required", new APICatalogMostRequired(model
, mainmodule
, catalog
))
36 use
("/bytags", new APICatalogByTags(model
, mainmodule
, catalog
))
37 use
("/contributors", new APICatalogContributors(model
, mainmodule
, catalog
))
38 use
("/stats", new APICatalogStats(model
, mainmodule
, catalog
))
42 abstract class APICatalogHandler
47 # List the 10 best packages from `cpt`
48 fun list_best
(cpt
: Counter[MPackage]): JsonArray do
49 var res
= new JsonArray
52 if i
> best
.length
then break
53 res
.add best
[best
.length-i
]
58 # List packages by group.
59 fun list_by
(map
: MultiHashMap[Object, MPackage]): JsonObject do
60 var res
= new JsonObject
61 var keys
= map
.keys
.to_a
62 alpha_comparator
.sort
(keys
)
64 var projs
= map
[k
].to_a
65 alpha_comparator
.sort
(projs
)
66 res
[k
.to_s
.html_escape
] = new JsonArray.from
(projs
)
73 super APICatalogHandler
75 redef fun get
(req
, res
) do
76 var obj
= new JsonObject
77 obj
["packages"] = model
.mpackages
.length
78 obj
["maintainers"] = catalog
.maint2proj
.length
79 obj
["contributors"] = catalog
.contrib2proj
.length
80 obj
["modules"] = catalog
.mmodules
.sum
81 obj
["classes"] = catalog
.mclasses
.sum
82 obj
["methods"] = catalog
.mmethods
.sum
83 obj
["loc"] = catalog
.loc
.sum
88 class APICatalogHighLighted
89 super APICatalogHandler
91 redef fun get
(req
, res
) do res
.json list_best
(catalog
.score
)
94 class APICatalogMostRequired
95 super APICatalogHandler
97 redef fun get
(req
, res
) do
98 if catalog
.deps
.not_empty
then
99 var reqs
= new Counter[MPackage]
100 for p
in model
.mpackages
do
101 reqs
[p
] = catalog
.deps
[p
].smallers
.length
- 1
103 res
.json list_best
(reqs
)
106 res
.json
new JsonArray
110 class APICatalogByTags
111 super APICatalogHandler
113 redef fun get
(req
, res
) do res
.json list_by
(catalog
.tag2proj
)
116 class APICatalogContributors
117 super APICatalogHandler
119 redef fun get
(req
, res
) do
120 var obj
= new JsonObject
121 obj
["maintainers"] = new JsonArray.from
(catalog
.maint2proj
.keys
)
122 obj
["contributors"] = new JsonArray.from
(catalog
.contrib2proj
.keys
)
131 var obj
= new JsonObject
135 obj
["hash"] = (email
or else "").md5
.to_lower