294d072ce346ab45a82e3fcd546a092ec2b661c2
[nit.git] / src / web / api_catalog.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 module api_catalog
16
17 import web_base
18 import catalog
19
20 redef class NitwebConfig
21
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)
33 end
34 end
35 end
36 catalog.git_info(mpackage)
37 catalog.package_page(mpackage)
38 end
39 return catalog
40 end
41 end
42
43 # Group all api handlers in one router.
44 class APICatalogRouter
45 super APIRouter
46
47 init do
48 use("/highlighted", new APICatalogHighLighted(config))
49 use("/required", new APICatalogMostRequired(config))
50 use("/bytags", new APICatalogByTags(config))
51 use("/contributors", new APICatalogContributors(config))
52 use("/stats", new APICatalogStats(config))
53 end
54 end
55
56 abstract class APICatalogHandler
57 super APIHandler
58
59 # List the 10 best packages from `cpt`
60 fun list_best(cpt: Counter[MPackage]): JsonArray do
61 var res = new JsonArray
62 var best = cpt.sort
63 for i in [1..10] do
64 if i > best.length then break
65 res.add best[best.length-i]
66 end
67 return res
68 end
69
70 # List packages by group.
71 fun list_by(map: MultiHashMap[Object, MPackage]): JsonObject do
72 var res = new JsonObject
73 var keys = map.keys.to_a
74 alpha_comparator.sort(keys)
75 for k in keys do
76 var projs = map[k].to_a
77 alpha_comparator.sort(projs)
78 res[k.to_s.html_escape] = new JsonArray.from(projs)
79 end
80 return res
81 end
82 end
83
84 class APICatalogStats
85 super APICatalogHandler
86
87 redef fun get(req, res) do
88 var obj = new JsonObject
89 obj["packages"] = config.model.mpackages.length
90 obj["maintainers"] = config.catalog.maint2proj.length
91 obj["contributors"] = config.catalog.contrib2proj.length
92 obj["modules"] = config.catalog.mmodules.sum
93 obj["classes"] = config.catalog.mclasses.sum
94 obj["methods"] = config.catalog.mmethods.sum
95 obj["loc"] = config.catalog.loc.sum
96 res.json obj
97 end
98 end
99
100 class APICatalogHighLighted
101 super APICatalogHandler
102
103 redef fun get(req, res) do res.json list_best(config.catalog.score)
104 end
105
106 class APICatalogMostRequired
107 super APICatalogHandler
108
109 redef fun get(req, res) do
110 if config.catalog.deps.not_empty then
111 var reqs = new Counter[MPackage]
112 for p in config.model.mpackages do
113 reqs[p] = config.catalog.deps[p].smallers.length - 1
114 end
115 res.json list_best(reqs)
116 return
117 end
118 res.json new JsonArray
119 end
120 end
121
122 class APICatalogByTags
123 super APICatalogHandler
124
125 redef fun get(req, res) do res.json list_by(config.catalog.tag2proj)
126 end
127
128 class APICatalogContributors
129 super APICatalogHandler
130
131 redef fun get(req, res) do
132 var obj = new JsonObject
133 obj["maintainers"] = new JsonArray.from(config.catalog.maint2proj.keys)
134 obj["contributors"] = new JsonArray.from(config.catalog.contrib2proj.keys)
135 res.json obj
136 end
137 end
138
139 redef class Person
140 super Jsonable
141
142 redef fun to_json do
143 var obj = new JsonObject
144 obj["name"] = name
145 obj["email"] = email
146 obj["page"] = page
147 obj["hash"] = (email or else "").md5.to_lower
148 return obj.to_json
149 end
150 end