f0c70c696a37cceb48e68ddbb6e33209d23cc724
[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 # Group all api handlers in one router.
21 class APICatalogRouter
22 super APIRouter
23
24 # Catalog to pass to handlers.
25 var catalog: Catalog
26
27 init do
28 use("/highlighted", new APICatalogHighLighted(config, catalog))
29 use("/required", new APICatalogMostRequired(config, catalog))
30 use("/bytags", new APICatalogByTags(config, catalog))
31 use("/contributors", new APICatalogContributors(config, catalog))
32 use("/stats", new APICatalogStats(config, catalog))
33 end
34 end
35
36 abstract class APICatalogHandler
37 super APIHandler
38
39 var catalog: Catalog
40
41 # List the 10 best packages from `cpt`
42 fun list_best(cpt: Counter[MPackage]): JsonArray do
43 var res = new JsonArray
44 var best = cpt.sort
45 for i in [1..10] do
46 if i > best.length then break
47 res.add best[best.length-i]
48 end
49 return res
50 end
51
52 # List packages by group.
53 fun list_by(map: MultiHashMap[Object, MPackage]): JsonObject do
54 var res = new JsonObject
55 var keys = map.keys.to_a
56 alpha_comparator.sort(keys)
57 for k in keys do
58 var projs = map[k].to_a
59 alpha_comparator.sort(projs)
60 res[k.to_s.html_escape] = new JsonArray.from(projs)
61 end
62 return res
63 end
64 end
65
66 class APICatalogStats
67 super APICatalogHandler
68
69 redef fun get(req, res) do
70 var obj = new JsonObject
71 obj["packages"] = config.model.mpackages.length
72 obj["maintainers"] = catalog.maint2proj.length
73 obj["contributors"] = catalog.contrib2proj.length
74 obj["modules"] = catalog.mmodules.sum
75 obj["classes"] = catalog.mclasses.sum
76 obj["methods"] = catalog.mmethods.sum
77 obj["loc"] = catalog.loc.sum
78 res.json obj
79 end
80 end
81
82 class APICatalogHighLighted
83 super APICatalogHandler
84
85 redef fun get(req, res) do res.json list_best(catalog.score)
86 end
87
88 class APICatalogMostRequired
89 super APICatalogHandler
90
91 redef fun get(req, res) do
92 if catalog.deps.not_empty then
93 var reqs = new Counter[MPackage]
94 for p in config.model.mpackages do
95 reqs[p] = catalog.deps[p].smallers.length - 1
96 end
97 res.json list_best(reqs)
98 return
99 end
100 res.json new JsonArray
101 end
102 end
103
104 class APICatalogByTags
105 super APICatalogHandler
106
107 redef fun get(req, res) do res.json list_by(catalog.tag2proj)
108 end
109
110 class APICatalogContributors
111 super APICatalogHandler
112
113 redef fun get(req, res) do
114 var obj = new JsonObject
115 obj["maintainers"] = new JsonArray.from(catalog.maint2proj.keys)
116 obj["contributors"] = new JsonArray.from(catalog.contrib2proj.keys)
117 res.json obj
118 end
119 end
120
121 redef class Person
122 super Jsonable
123
124 redef fun to_json do
125 var obj = new JsonObject
126 obj["name"] = name
127 obj["email"] = email
128 obj["page"] = page
129 obj["hash"] = (email or else "").md5.to_lower
130 return obj.to_json
131 end
132 end