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