This project has retired. For details please refer to its Attic page.
MetricsController xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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     * Query metrics stored in HBase table
51     * 
52     * @param request is HTTP request object
53     * @param metric is metric name
54     * @param source is data source
55     * @param start is start time
56     * @param end is end time
57     * @return Metrics JSON
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     * Query metric series by session key, this is useful to query same metric from
92     * multiple data sources, such as multiple hosts
93     * 
94     * @param request is HTTP request object
95     * @param metricGroup is metric group name
96     * @param metric is metric name
97     * @param skey is session key which maps to multiple data sources
98     * @param start is start time
99     * @param end is end time
100    * @return List of metric series
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    * Query all metric group names in HBase
148    * 
149    * @return a list of metric groups
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    * Query metric names by metric group
163    * 
164    * @param metricGroup is name of metric group
165    * @return a list of metric names
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    * Query metrics source names by metric group
180    * 
181    * @param request HTTP Request object
182    * @param metricGroup is name of metric group
183    * @return a list of metric source names
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 }