This project has retired. For details please refer to its
Attic page.
TileController 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.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("/tile")
49 public class TileController 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
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("tile.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 @PUT
101 @Path("preview")
102 @Consumes(MediaType.APPLICATION_JSON)
103 @Produces(MediaType.TEXT_HTML)
104 public String preview(String buffer) {
105 VelocityContext context = new VelocityContext();
106 StringWriter sw = null;
107 try {
108 Gson gson = new Gson();
109 Chart chart = gson.fromJson(buffer, Chart.class);
110 List<SeriesMetaData> series = chart.getSeries();
111 String seriesMetaData = gson.toJson(series);
112
113 context.put("chart", chart);
114 context.put("seriesMetaData", seriesMetaData);
115 Template template = velocity.getTemplate("tile.vm");
116 sw = new StringWriter();
117 template.merge(context, sw);
118 } catch (Exception e) {
119 e.printStackTrace();
120 return e.getMessage();
121 }
122 return sw.toString();
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136 @PUT
137 @Path("preview/series")
138 @Produces(MediaType.APPLICATION_JSON)
139 public String previewSeries(@Context HttpServletRequest request, String buffer) {
140 Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
141 }.getType();
142 long startTime = 0;
143 long endTime = 0;
144 TimeHandler time = new TimeHandler(request);
145 startTime = time.getStartTime();
146 endTime = time.getEndTime();
147 Gson gson = new Gson();
148 ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
149 List<String> data = ChukwaHBaseStore.getData(series, startTime, endTime);
150 String result = gson.toJson(data);
151 return result;
152 }
153 }