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 redef class NitwebConfig
22 # Catalog to pass to handlers.
23 var catalog
: Catalog is lazy
do
24 var catalog
= new Catalog(modelbuilder
)
25 for mpackage
in model
.mpackages
do
26 catalog
.deps
.add_node
(mpackage
)
27 for mgroup
in mpackage
.mgroups
do
28 for mmodule
in mgroup
.mmodules
do
29 for imported
in mmodule
.in_importation
.direct_greaters
do
30 var ip
= imported
.mpackage
31 if ip
== null or ip
== mpackage
then continue
32 catalog
.deps
.add_edge
(mpackage
, ip
)
36 catalog
.git_info
(mpackage
)
37 catalog
.package_page
(mpackage
)
46 use
("/catalog/highlighted", new APICatalogHighLighted(config
))
47 use
("/catalog/required", new APICatalogMostRequired(config
))
48 use
("/catalog/bytags", new APICatalogByTags(config
))
49 use
("/catalog/contributors", new APICatalogContributors(config
))
50 use
("/catalog/stats", new APICatalogStats(config
))
54 abstract class APICatalogHandler
57 # List the 10 best packages from `cpt`
58 fun list_best
(cpt
: Counter[MPackage]): JsonArray do
59 var res
= new JsonArray
62 if i
> best
.length
then break
63 res
.add best
[best
.length-i
]
68 # List packages by group.
69 fun list_by
(map
: MultiHashMap[Object, MPackage]): JsonObject do
70 var res
= new JsonObject
71 var keys
= map
.keys
.to_a
72 alpha_comparator
.sort
(keys
)
74 var projs
= map
[k
].to_a
75 alpha_comparator
.sort
(projs
)
76 res
[k
.to_s
.html_escape
] = new JsonArray.from
(projs
)
83 super APICatalogHandler
85 redef fun get
(req
, res
) do
86 var obj
= new JsonObject
87 obj
["packages"] = config
.model
.mpackages
.length
88 obj
["maintainers"] = config
.catalog
.maint2proj
.length
89 obj
["contributors"] = config
.catalog
.contrib2proj
.length
90 obj
["modules"] = config
.catalog
.mmodules
.sum
91 obj
["classes"] = config
.catalog
.mclasses
.sum
92 obj
["methods"] = config
.catalog
.mmethods
.sum
93 obj
["loc"] = config
.catalog
.loc
.sum
98 class APICatalogHighLighted
99 super APICatalogHandler
101 redef fun get
(req
, res
) do res
.json list_best
(config
.catalog
.score
)
104 class APICatalogMostRequired
105 super APICatalogHandler
107 redef fun get
(req
, res
) do
108 if config
.catalog
.deps
.not_empty
then
109 var reqs
= new Counter[MPackage]
110 for p
in config
.model
.mpackages
do
111 reqs
[p
] = config
.catalog
.deps
[p
].smallers
.length
- 1
113 res
.json list_best
(reqs
)
116 res
.json
new JsonArray
120 class APICatalogByTags
121 super APICatalogHandler
123 redef fun get
(req
, res
) do res
.json list_by
(config
.catalog
.tag2proj
)
126 class APICatalogContributors
127 super APICatalogHandler
129 redef fun get
(req
, res
) do
130 var obj
= new JsonObject
131 obj
["maintainers"] = new JsonArray.from
(config
.catalog
.maint2proj
.keys
)
132 obj
["contributors"] = new JsonArray.from
(config
.catalog
.contrib2proj
.keys
)
141 var obj
= new JsonObject
145 obj
["hash"] = (email
or else "").md5
.to_lower