This project has retired. For details please refer to its
Attic page.
MetricsController 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.lang.reflect.Type;
21 import java.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpSession;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37
38 import org.apache.hadoop.chukwa.datacollection.agent.rest.Examples;
39 import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
40 import org.apache.hadoop.chukwa.hicc.TimeHandler;
41 import org.apache.hadoop.chukwa.hicc.bean.Series;
42
43 import com.google.gson.Gson;
44 import com.google.gson.reflect.TypeToken;
45
46 @Path("/metrics")
47 public class MetricsController {
48
49
50
51
52
53
54
55
56
57
58
59
60 @GET
61 @Path("series/{metric}/{source}")
62 @Produces("application/json")
63 public String getSeries(@Context HttpServletRequest request, @PathParam("metric") String metric, @PathParam("source") String source, @QueryParam("start") String start, @QueryParam("end") String end) {
64 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
65 String buffer = "";
66 Series series;
67 long startTime = 0;
68 long endTime = 0;
69 TimeHandler time = new TimeHandler(request);
70 try {
71 if(start!=null) {
72 startTime = sdf.parse(start).getTime();
73 } else {
74 startTime = time.getStartTime();
75 }
76 if(end!=null) {
77 endTime = sdf.parse(end).getTime();
78 } else {
79 endTime = time.getEndTime();
80 }
81 series = ChukwaHBaseStore.getSeries(metric, source, startTime, endTime);
82 buffer = series.toString();
83 } catch (ParseException e) {
84 throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
85 .entity("Start/End date parse error. Format: yyyyMMddHHmmss.").build());
86 }
87 return buffer;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 @GET
103 @Path("series/{metricGroup}/{metric}/session/{sessionKey}")
104 @Produces("application/json")
105 public String getSeriesBySessionAttribute(@Context HttpServletRequest request, @PathParam("metricGroup") String metricGroup, @PathParam("metric") String metric, @PathParam("sessionKey") String skey, @QueryParam("start") String start, @QueryParam("end") String end) {
106 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
107 String buffer = "";
108 long startTime = 0;
109 long endTime = 0;
110 TimeHandler time = new TimeHandler(request);
111 try {
112 if(start!=null) {
113 startTime = sdf.parse(start).getTime();
114 } else {
115 startTime = time.getStartTime();
116 }
117 if(end!=null) {
118 endTime = sdf.parse(end).getTime();
119 } else {
120 endTime = time.getEndTime();
121 }
122 if(skey!=null) {
123 HttpSession session = request.getSession();
124 String[] sourcekeys = (session.getAttribute(skey).toString()).split(",");
125 Type seriesListType =new TypeToken<ArrayList<Series>>(){}.getType();
126 ArrayList<Series> seriesList = new ArrayList<Series>();
127 for(String source : sourcekeys) {
128 if (source == null || source.equals("")) {
129 continue;
130 }
131 Series output = ChukwaHBaseStore.getSeries(metricGroup, metric, source, startTime, endTime);
132 seriesList.add(output);
133 }
134 buffer = new Gson().toJson(seriesList, seriesListType);
135 } else {
136 throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
137 .entity("No session attribute key defined.").build());
138 }
139 } catch (ParseException e) {
140 throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
141 .entity("Start/End date parse error. Format: yyyyMMddHHmmss.").build());
142 }
143 return buffer;
144 }
145
146
147
148
149
150
151 @GET
152 @Path("schema")
153 @Produces("application/json")
154 public String getTables() {
155 Set<String> metricGroups = ChukwaHBaseStore.getMetricGroups();
156 Type metricGroupsType = new TypeToken<List<String>>(){}.getType();
157 String groups = new Gson().toJson(metricGroups, metricGroupsType);
158 return groups;
159 }
160
161
162
163
164
165
166
167
168 @GET
169 @Path("schema/{metricGroup}")
170 @Produces("application/json")
171 public String getMetrics(@PathParam("metricGroup") String metricGroup) {
172 Set<String> metricNames = ChukwaHBaseStore.getMetricNames(metricGroup);
173 Type metricsType = new TypeToken<List<String>>(){}.getType();
174 String metrics = new Gson().toJson(metricNames, metricsType);
175 return metrics;
176 }
177
178
179
180
181
182
183
184
185
186 @GET
187 @Path("source/{metricGroup}")
188 @Produces("application/json")
189 public String getSourceNames(@Context HttpServletRequest request, @PathParam("metricGroup") String metricGroup) {
190 Set<String> sourceNames = ChukwaHBaseStore.getSourceNames(metricGroup);
191 Type rowsType = new TypeToken<List<String>>(){}.getType();
192 String rows = new Gson().toJson(sourceNames, rowsType);
193 return rows;
194 }
195
196 }