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.datastore.ChukwaHBaseStore;
39  import org.apache.hadoop.chukwa.hicc.TimeHandler;
40  import org.apache.hadoop.chukwa.hicc.bean.Series;
41  
42  import com.google.gson.Gson;
43  import com.google.gson.reflect.TypeToken;
44  
45  @Path("/metrics")
46  public class MetricsController {
47  
48    @GET
49    @Path("series/{metric}/{source}")
50    @Produces("application/json")
51    public String getSeries(@Context HttpServletRequest request, @PathParam("metric") String metric, @PathParam("source") String source, @QueryParam("start") String start, @QueryParam("end") String end) {
52      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
53      String buffer = "";
54      Series series;
55      long startTime = 0;
56      long endTime = 0;
57      TimeHandler time = new TimeHandler(request);
58      try {
59        if(start!=null) {
60          startTime = sdf.parse(start).getTime();
61        } else {
62          startTime = time.getStartTime();
63        }
64        if(end!=null) {
65          endTime = sdf.parse(end).getTime();
66        } else {
67          endTime = time.getEndTime();
68        }
69        series = ChukwaHBaseStore.getSeries(metric, source, startTime, endTime);
70        buffer = series.toString();
71      } catch (ParseException e) {
72        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
73            .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());
74      }
75      return buffer;
76    }
77  
78    @GET
79    @Path("series/{metricGroup}/{metric}/session/{sessionKey}")
80    @Produces("application/json")
81    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) {
82      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
83      String buffer = "";
84      long startTime = 0;
85      long endTime = 0;
86      TimeHandler time = new TimeHandler(request);
87      try {
88        if(start!=null) {
89          startTime = sdf.parse(start).getTime();
90        } else {
91          startTime = time.getStartTime();
92        }
93        if(end!=null) {
94          endTime = sdf.parse(end).getTime();
95        } else {
96          endTime = time.getEndTime();
97        }
98        if(skey!=null) {
99            HttpSession session = request.getSession();
100           String[] sourcekeys = (session.getAttribute(skey).toString()).split(",");
101           Type seriesListType =new TypeToken<ArrayList<Series>>(){}.getType();
102           ArrayList<Series> seriesList = new ArrayList<Series>();
103           for(String source : sourcekeys) {
104             if (source == null || source.equals("")) {
105               continue;
106             }
107             Series output = ChukwaHBaseStore.getSeries(metricGroup, metric, source, startTime, endTime);
108             seriesList.add(output);
109           }
110           buffer = new Gson().toJson(seriesList, seriesListType);
111       } else {
112         throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
113             .entity("No session attribute key defined.").build());
114       }
115     } catch (ParseException e) {
116       throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
117           .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());
118     }
119     return buffer;
120   }
121 
122   @GET
123   @Path("schema")
124   @Produces("application/json")
125   public String getTables() {
126     Set<String> metricGroups = ChukwaHBaseStore.getMetricGroups();
127     Type metricGroupsType = new TypeToken<List<String>>(){}.getType();
128     String groups = new Gson().toJson(metricGroups, metricGroupsType);
129     return groups;
130   }
131   
132   @GET
133   @Path("schema/{metricGroup}")
134   @Produces("application/json")
135   public String getMetrics(@PathParam("metricGroup") String metricGroup) {
136     Set<String> metricNames = ChukwaHBaseStore.getMetricNames(metricGroup);
137     Type metricsType = new TypeToken<List<String>>(){}.getType();
138     String metrics = new Gson().toJson(metricNames, metricsType);
139     return metrics;
140   }
141 
142   @GET
143   @Path("source/{metricGroup}")
144   @Produces("application/json")
145   public String getSourceNames(@Context HttpServletRequest request, @PathParam("metricGroup") String metricGroup) {
146     Set<String> sourceNames = ChukwaHBaseStore.getSourceNames(metricGroup);
147     Type rowsType = new TypeToken<List<String>>(){}.getType();
148     String rows = new Gson().toJson(sourceNames, rowsType);
149     return rows;
150   }
151 
152 }