*: update redefs of `to_json`
[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 redef class APIRouter
44 redef init do
45 super
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))
51 end
52 end
53
54 abstract class APICatalogHandler
55 super APIHandler
56
57 # List the 10 best packages from `cpt`
58 fun list_best(cpt: Counter[MPackage]): JsonArray do
59 var res = new JsonArray
60 var best = cpt.sort
61 for i in [1..10] do
62 if i > best.length then break
63 res.add best[best.length-i]
64 end
65 return res
66 end
67
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)
73 for k in keys do
74 var projs = map[k].to_a
75 alpha_comparator.sort(projs)
76 res[k.to_s.html_escape] = new JsonArray.from(projs)
77 end
78 return res
79 end
80 end
81
82 class APICatalogStats
83 super APICatalogHandler
84
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
94 res.json obj
95 end
96 end
97
98 class APICatalogHighLighted
99 super APICatalogHandler
100
101 redef fun get(req, res) do res.json list_best(config.catalog.score)
102 end
103
104 class APICatalogMostRequired
105 super APICatalogHandler
106
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
112 end
113 res.json list_best(reqs)
114 return
115 end
116 res.json new JsonArray
117 end
118 end
119
120 class APICatalogByTags
121 super APICatalogHandler
122
123 redef fun get(req, res) do res.json list_by(config.catalog.tag2proj)
124 end
125
126 class APICatalogContributors
127 super APICatalogHandler
128
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)
133 res.json obj
134 end
135 end
136
137 redef class Person
138 super Jsonable
139
140 redef fun core_serialize_to(v) do
141 v.serialize_attribute("name", name)
142 v.serialize_attribute("email", email)
143 v.serialize_attribute("page", page)
144 v.serialize_attribute("hash", (email or else "").md5.to_lower)
145 end
146 end