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 @GET
66 @Path("draw/{id}")
67 @Produces(MediaType.TEXT_HTML)
68 public String draw(@PathParam("id") String id) {
69 VelocityContext context = new VelocityContext();
70 StringWriter sw = null;
71 try {
72 Chart chart = ChukwaHBaseStore.getChart(id);
73 List<SeriesMetaData> series = chart.getSeries();
74 Gson gson = new Gson();
75 String seriesMetaData = gson.toJson(series);
76
77 context.put("chart", chart);
78 context.put("seriesMetaData", seriesMetaData);
79 Template template = velocity.getTemplate("chart.vm");
80 sw = new StringWriter();
81 template.merge(context, sw);
82 } catch (Exception e) {
83 e.printStackTrace();
84 return e.getMessage();
85 }
86 return sw.toString();
87 }
88
89
90
91
92
93
94 @GET
95 @Path("describe/{id}")
96 @Produces(MediaType.APPLICATION_JSON)
97 public String describe(@PathParam("id") String id) {
98 Chart chart = ChukwaHBaseStore.getChart(id);
99 Gson gson = new Gson();
100 String buffer = gson.toJson(chart);
101 return buffer;
102 }
103
104
105
106
107
108
109
110 @POST
111 @Path("save")
112 @Consumes(MediaType.APPLICATION_JSON)
113 public Response create(String buffer) {
114 Gson gson = new Gson();
115 Chart chart = gson.fromJson(buffer, Chart.class);
116 String id = ChukwaHBaseStore.createChart(chart);
117 if (id != null) {
118 return Response.ok(id).build();
119 }
120 return Responses.notAcceptable().build();
121 }
122
123
124
125
126
127
128
129
130 @PUT
131 @Path("save/{id}")
132 @Consumes(MediaType.APPLICATION_JSON)
133 public Response save(@PathParam("id") String id, String buffer) {
134 Gson gson = new Gson();
135 Chart chart = gson.fromJson(buffer, Chart.class);
136 ChukwaHBaseStore.putChart(id, chart);
137 return Response.ok().build();
138
139 }
140
141
142
143
144
145
146 @PUT
147 @Path("preview")
148 public String preview(String buffer) {
149 VelocityContext context = new VelocityContext();
150 StringWriter sw = null;
151 try {
152 Gson gson = new Gson();
153 Chart chart = gson.fromJson(buffer, Chart.class);
154 List<SeriesMetaData> series = chart.getSeries();
155 String seriesMetaData = gson.toJson(series);
156
157 context.put("chart", chart);
158 context.put("seriesMetaData", seriesMetaData);
159 Template template = velocity.getTemplate("chart.vm");
160 sw = new StringWriter();
161 template.merge(context, sw);
162 } catch (Exception e) {
163 e.printStackTrace();
164 return e.getMessage();
165 }
166 return sw.toString();
167 }
168
169 @PUT
170 @Path("preview/series")
171 @Produces("application/json")
172 public String previewSeries(@Context HttpServletRequest request, String buffer) {
173 Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
174 }.getType();
175 long startTime = 0;
176 long endTime = 0;
177 TimeHandler time = new TimeHandler(request);
178 startTime = time.getStartTime();
179 endTime = time.getEndTime();
180 Gson gson = new Gson();
181 ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
182 series = ChukwaHBaseStore.getChartSeries(series, startTime, endTime);
183 String result = gson.toJson(series);
184 return result;
185 }
186
187 }