This project has retired. For details please refer to its
Attic page.
ChartController 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.lang.reflect.Type;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.Context;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37
38 import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
39 import org.apache.hadoop.chukwa.hicc.TimeHandler;
40 import org.apache.hadoop.chukwa.hicc.bean.Chart;
41 import org.apache.hadoop.chukwa.hicc.bean.SeriesMetaData;
42 import org.apache.log4j.Logger;
43 import org.apache.velocity.Template;
44 import org.apache.velocity.VelocityContext;
45 import org.apache.velocity.app.VelocityEngine;
46
47 import com.google.gson.Gson;
48 import com.google.gson.reflect.TypeToken;
49 import com.sun.jersey.api.Responses;
50
51 @Path("/chart")
52 public class ChartController {
53 static Logger LOG = Logger.getLogger(ChartController.class);
54 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
55
56 @Context
57 VelocityEngine velocity;
58
59
60
61
62
63
64
65
66 @GET
67 @Path("draw/{id}")
68 @Produces(MediaType.TEXT_HTML)
69 public String draw(@PathParam("id") String id) {
70 VelocityContext context = new VelocityContext();
71 StringWriter sw = null;
72 try {
73 Chart chart = ChukwaHBaseStore.getChart(id);
74 List<SeriesMetaData> series = chart.getSeries();
75 Gson gson = new Gson();
76 String seriesMetaData = gson.toJson(series);
77
78 context.put("chart", chart);
79 context.put("seriesMetaData", seriesMetaData);
80 Template template = velocity.getTemplate("chart.vm");
81 sw = new StringWriter();
82 template.merge(context, sw);
83 } catch (Exception e) {
84 e.printStackTrace();
85 return e.getMessage();
86 }
87 return sw.toString();
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101 @GET
102 @Path("describe/{id}")
103 @Produces(MediaType.APPLICATION_JSON)
104 public String describe(@PathParam("id") String id) {
105 Chart chart = ChukwaHBaseStore.getChart(id);
106 Gson gson = new Gson();
107 String buffer = gson.toJson(chart);
108 return buffer;
109 }
110
111
112
113
114
115
116
117
118
119
120 @POST
121 @Path("save")
122 @Consumes(MediaType.APPLICATION_JSON)
123 public Response create(String buffer) {
124 Gson gson = new Gson();
125 Chart chart = gson.fromJson(buffer, Chart.class);
126 String id = ChukwaHBaseStore.createChart(chart);
127 if (id != null) {
128 return Response.ok(id).build();
129 }
130 return Responses.notAcceptable().build();
131 }
132
133
134
135
136
137
138
139
140
141
142
143 @PUT
144 @Path("save/{id}")
145 @Consumes(MediaType.APPLICATION_JSON)
146 public Response save(@PathParam("id") String id, String buffer) {
147 Gson gson = new Gson();
148 Chart chart = gson.fromJson(buffer, Chart.class);
149 ChukwaHBaseStore.putChart(id, chart);
150 return Response.ok().build();
151
152 }
153
154
155
156
157
158
159
160
161
162
163 @PUT
164 @Path("preview")
165 @Consumes(MediaType.APPLICATION_JSON)
166 @Produces(MediaType.TEXT_HTML)
167 public String preview(String buffer) {
168 VelocityContext context = new VelocityContext();
169 StringWriter sw = null;
170 try {
171 Gson gson = new Gson();
172 Chart chart = gson.fromJson(buffer, Chart.class);
173 List<SeriesMetaData> series = chart.getSeries();
174 String seriesMetaData = gson.toJson(series);
175
176 context.put("chart", chart);
177 context.put("seriesMetaData", seriesMetaData);
178 Template template = velocity.getTemplate("chart.vm");
179 sw = new StringWriter();
180 template.merge(context, sw);
181 } catch (Exception e) {
182 e.printStackTrace();
183 return e.getMessage();
184 }
185 return sw.toString();
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200 @PUT
201 @Path("preview/series")
202 @Consumes(MediaType.APPLICATION_JSON)
203 @Produces("application/json")
204 public String previewSeries(@Context HttpServletRequest request, String buffer) {
205 Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
206 }.getType();
207 long startTime = 0;
208 long endTime = 0;
209 TimeHandler time = new TimeHandler(request);
210 startTime = time.getStartTime();
211 endTime = time.getEndTime();
212 Gson gson = new Gson();
213 ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
214 series = ChukwaHBaseStore.getChartSeries(series, startTime, endTime);
215 String result = gson.toJson(series);
216 return result;
217 }
218
219 }