This project has retired. For details please refer to its
Attic page.
DashboardController xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.hicc.rest;
19
20 import java.io.StringWriter;
21 import java.util.Set;
22 import java.net.InetAddress;
23
24 import javax.servlet.ServletContext;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.Status;
36
37 import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
38 import org.apache.hadoop.chukwa.hicc.bean.Dashboard;
39 import org.apache.hadoop.conf.Configuration;
40 import org.apache.hadoop.hbase.HBaseConfiguration;
41 import org.apache.hadoop.hbase.HConstants;
42 import org.apache.hadoop.hdfs.DFSConfigKeys;
43 import org.apache.hadoop.yarn.conf.YarnConfiguration;
44 import org.apache.log4j.Logger;
45 import org.apache.velocity.Template;
46 import org.apache.velocity.VelocityContext;
47 import org.apache.velocity.app.VelocityEngine;
48
49 import com.google.gson.Gson;
50
51 @Path("dashboard")
52 public class DashboardController {
53 static Logger LOG = Logger.getLogger(DashboardController.class);
54
55 @Context
56 private ServletContext context;
57 @Context
58 VelocityEngine velocity;
59
60
61
62
63
64
65
66
67
68
69
70
71 @GET
72 @Produces(MediaType.APPLICATION_JSON)
73 @Path("load/{id}")
74 public String load(@Context HttpServletRequest request, @PathParam("id") String id) {
75 Gson gson = new Gson();
76 Dashboard dash = ChukwaHBaseStore.getDashboard(id, request.getRemoteUser());
77 String json = gson.toJson(dash);
78 return json;
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93 @PUT
94 @Path("save/{id}")
95 @Consumes(MediaType.APPLICATION_JSON)
96 public Response save(@Context HttpServletRequest request, @PathParam("id") String id, String buffer) {
97 Gson gson = new Gson();
98 Dashboard dash = gson.fromJson(buffer, Dashboard.class);
99 boolean result = ChukwaHBaseStore.updateDashboard(id, request.getRemoteUser(), dash);
100 if(!result) {
101 return Response.status(Status.BAD_REQUEST).build();
102 }
103 return Response.ok().build();
104 }
105
106
107
108
109
110
111
112
113
114
115
116 @GET
117 @Path("whoami")
118 @Produces(MediaType.TEXT_PLAIN)
119 public String whoami(@Context HttpServletRequest request) {
120 return request.getRemoteUser();
121 }
122
123
124
125
126
127
128
129
130
131
132 @GET
133 @Path("quicklinks")
134 @Produces(MediaType.TEXT_HTML)
135 public String quicklinks() {
136 VelocityContext context = new VelocityContext();
137 StringWriter sw = null;
138 Configuration hconf = HBaseConfiguration.create();
139 Configuration hadoop = new Configuration();
140 String nn = "";
141 String rm = "";
142 String hm = "";
143 Set<String> sourceNames = ChukwaHBaseStore.getSourceNames("");
144 for (String source : sourceNames) {
145 String[] sourceParts = source.split(":");
146 if(sourceParts.length<2) {
147 continue;
148 }
149 if(sourceParts[1].equals("NameNode")) {
150 String[] parts = hadoop.get(DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY).split(":");
151 StringBuilder buffer = new StringBuilder();
152 try {
153 InetAddress address = InetAddress.getByName(sourceParts[0]);
154 buffer.append(address.getHostAddress());
155 } catch (Exception e) {
156 buffer.append(sourceParts[0]);
157 }
158 buffer.append(":");
159 buffer.append(parts[1]);
160 nn = buffer.toString();
161 } else if(sourceParts[1].equals("ResourceManager")) {
162 String[] parts = hadoop.get(YarnConfiguration.RM_WEBAPP_ADDRESS).split(":");
163 StringBuilder buffer = new StringBuilder();
164 try {
165 InetAddress address = InetAddress.getByName(sourceParts[0]);
166 buffer.append(address.getHostAddress());
167 } catch (Exception e) {
168 buffer.append(sourceParts[0]);
169 }
170 buffer.append(":");
171 buffer.append(parts[1]);
172 rm = buffer.toString();
173 } else if(sourceParts[1].equals("Master")) {
174 StringBuilder buffer = new StringBuilder();
175 try {
176 InetAddress address = InetAddress.getByName(sourceParts[0]);
177 buffer.append(address.getHostAddress());
178 } catch (Exception e) {
179 buffer.append(sourceParts[0]);
180 }
181 buffer.append(":");
182 buffer.append(hconf.getInt("hbase.master.info.port", HConstants.DEFAULT_MASTER_INFOPORT));
183 hm = buffer.toString();
184 }
185 }
186 try {
187 context.put("nn", nn);
188 context.put("rm", rm);
189 context.put("hm", hm);
190 Template template = velocity.getTemplate("quick-links.vm");
191 sw = new StringWriter();
192 template.merge(context, sw);
193 } catch (Exception e) {
194 e.printStackTrace();
195 return e.getMessage();
196 }
197 return sw.toString();
198 }
199 }