This project has retired. For details please refer to its
Attic page.
PieChartController 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.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
35 import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
36 import org.apache.hadoop.chukwa.hicc.TimeHandler;
37 import org.apache.hadoop.chukwa.hicc.bean.Chart;
38 import org.apache.hadoop.chukwa.hicc.bean.ChartType;
39 import org.apache.hadoop.chukwa.hicc.bean.SeriesMetaData;
40 import org.apache.log4j.Logger;
41 import org.apache.velocity.Template;
42 import org.apache.velocity.VelocityContext;
43 import org.apache.velocity.app.VelocityEngine;
44
45 import com.google.gson.Gson;
46 import com.google.gson.reflect.TypeToken;
47
48 @Path("/piechart")
49 public class PieChartController extends ChartController{
50 static Logger LOG = Logger.getLogger(ChartController.class);
51 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
52
53 @Context
54 VelocityEngine velocity;
55
56
57
58
59
60
61
62 @GET
63 @Path("draw/{id}")
64 @Produces(MediaType.TEXT_HTML)
65 public String draw(@PathParam("id") String id) {
66 VelocityContext context = new VelocityContext();
67 StringWriter sw = null;
68 try {
69 Chart chart = ChukwaHBaseStore.getChart(id);
70 List<SeriesMetaData> series = chart.getSeries();
71 Gson gson = new Gson();
72 String seriesMetaData = gson.toJson(series);
73
74 context.put("chart", chart);
75 context.put("seriesMetaData", seriesMetaData);
76 context.put("chartTypeDonut", ChartType.DONUT);
77 Template template = velocity.getTemplate("pie.vm");
78 sw = new StringWriter();
79 template.merge(context, sw);
80 } catch (Exception e) {
81 e.printStackTrace();
82 return e.getMessage();
83 }
84 return sw.toString();
85 }
86
87
88
89
90 @PUT
91 @Path("preview")
92 public String preview(String buffer) {
93 VelocityContext context = new VelocityContext();
94 StringWriter sw = null;
95 try {
96 Gson gson = new Gson();
97 Chart chart = gson.fromJson(buffer, Chart.class);
98 List<SeriesMetaData> series = chart.getSeries();
99 String seriesMetaData = gson.toJson(series);
100
101 context.put("chart", chart);
102 context.put("seriesMetaData", seriesMetaData);
103 context.put("chartTypeDonut", ChartType.DONUT);
104 Template template = velocity.getTemplate("pie.vm");
105 sw = new StringWriter();
106 template.merge(context, sw);
107 } catch (Exception e) {
108 e.printStackTrace();
109 return e.getMessage();
110 }
111 return sw.toString();
112 }
113
114 @PUT
115 @Path("preview/series")
116 @Produces("application/json")
117 public String previewSeries(@Context HttpServletRequest request, String buffer) {
118 Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
119 }.getType();
120 long startTime = 0;
121 long endTime = 0;
122 TimeHandler time = new TimeHandler(request);
123 startTime = time.getStartTime();
124 endTime = time.getEndTime();
125 Gson gson = new Gson();
126 ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
127 List<String> data = ChukwaHBaseStore.getData(series, startTime, endTime);
128 String result = gson.toJson(data);
129 return result;
130 }
131 }