This project has retired. For details please refer to its
Attic page.
CirclesController 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 import java.util.concurrent.TimeUnit;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.DefaultValue;
29 import javax.ws.rs.GET;
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.QueryParam;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
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.Series;
42 import org.apache.hadoop.chukwa.hicc.bean.SeriesMetaData;
43 import org.apache.log4j.Logger;
44 import org.apache.velocity.Template;
45 import org.apache.velocity.VelocityContext;
46 import org.apache.velocity.app.VelocityEngine;
47
48 import com.google.gson.Gson;
49 import com.google.gson.reflect.TypeToken;
50
51 @Path("/circles")
52 public class CirclesController {
53 static Logger LOG = Logger.getLogger(CirclesController.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, @DefaultValue("false") @QueryParam("invert") boolean invert) {
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("circles.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 @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 context.put("chart", chart);
101 context.put("seriesMetaData", seriesMetaData);
102 Template template = velocity.getTemplate("circles.vm");
103 sw = new StringWriter();
104 template.merge(context, sw);
105 } catch (Exception e) {
106 e.printStackTrace();
107 return e.getMessage();
108 }
109 return sw.toString();
110 }
111
112 @PUT
113 @Path("preview/series")
114 @Produces("application/json")
115 public String previewSeries(@Context HttpServletRequest request, String buffer) {
116 Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
117 }.getType();
118 long startTime = 0;
119 long endTime = 0;
120 TimeHandler time = new TimeHandler(request);
121 startTime = time.getStartTime();
122 endTime = time.getEndTime();
123 Gson gson = new Gson();
124 ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
125 double percent;
126 List<String> data = ChukwaHBaseStore.getData(series, startTime, endTime);
127 if(series.size()>=2) {
128 double x = 0;
129 double y = 1;
130 try {
131 x = Double.parseDouble(data.get(0));
132 } catch(NumberFormatException e) {
133 x = 0;
134 }
135 try {
136 y = Double.parseDouble(data.get(1));
137 } catch(NumberFormatException e) {
138 y = 1;
139 }
140 percent = x / y * 100d;
141 } else {
142 double x = Double.parseDouble(data.get(0));
143 percent = x;
144 }
145 percent = Math.round(percent * 100d) / 100d;
146 Series answer = new Series("circle");
147 answer.add(endTime, percent);
148 String result = gson.toJson(answer);
149 return result;
150 }
151 }