This project has retired. For details please refer to its Attic page.
ChartController 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.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.POST;
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.core.Context;
35  import javax.ws.rs.core.MediaType;
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.Chart;
41  import org.apache.hadoop.chukwa.hicc.bean.SeriesMetaData;
42  import org.apache.log4j.Logger;
43  import org.apache.velocity.Template;
44  import org.apache.velocity.VelocityContext;
45  import org.apache.velocity.app.VelocityEngine;
46  
47  import com.google.gson.Gson;
48  import com.google.gson.reflect.TypeToken;
49  import com.sun.jersey.api.Responses;
50  
51  @Path("/chart")
52  public class ChartController {
53    static Logger LOG = Logger.getLogger(ChartController.class);
54    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
55  
56    @Context
57    VelocityEngine velocity;
58    
59    /**
60     * Render chart using flot.js
61     * 
62     * @param id Reference ID of Chart stored in HBase chukwa_meta table.
63     * @return chart widget
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("chart.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     * Describe chart meta data
92     * 
93     * @param id Chart ID
94     * @return chart meta data
95     * 
96     * @response.representation.200.doc Display chart configuration options
97     * @response.representation.200.mediaType application/json
98     * @response.representation.200.example {@link Examples#CPU_UTILIZATION}
99     * 
100    */
101   @GET
102   @Path("describe/{id}")
103   @Produces(MediaType.APPLICATION_JSON)
104   public String describe(@PathParam("id") String id) {
105     Chart chart = ChukwaHBaseStore.getChart(id);
106     Gson gson = new Gson();
107     String buffer = gson.toJson(chart);
108     return buffer;
109   }
110 
111   /**
112    * Create a new chart meta data
113    * 
114    * @param buffer holds incoming JSON of Chart object
115    * @return Web response code
116    * 
117    * @request.representation.example {@link Examples#MEMORY_UTILIZATION}
118    * 
119    */
120   @POST
121   @Path("save")
122   @Consumes(MediaType.APPLICATION_JSON)
123   public Response create(String buffer) {
124     Gson gson = new Gson();
125     Chart chart = gson.fromJson(buffer, Chart.class);
126     String id = ChukwaHBaseStore.createChart(chart);
127     if (id != null) {
128       return Response.ok(id).build();
129     }
130     return Responses.notAcceptable().build();
131   }
132 
133   /**
134    * Save chart meta data
135    * 
136    * @param id is unique identifier of Chart object
137    * @param buffer holds incoming JSON of Chart object
138    * @return Web response code
139    * 
140    * @request.representation.example {@link Examples#DISK_UTILIZATION}
141    * 
142    */
143   @PUT
144   @Path("save/{id}")
145   @Consumes(MediaType.APPLICATION_JSON)
146   public Response save(@PathParam("id") String id, String buffer) {
147     Gson gson = new Gson();
148     Chart chart = gson.fromJson(buffer, Chart.class);
149     ChukwaHBaseStore.putChart(id, chart);
150     return Response.ok().build();
151     
152   }
153 
154   /**
155    * Display a chart base on chart configuration from REST API input
156    * 
157    * @param buffer holds incoming JSON of Chart object
158    * @return segment of chart HTML output
159    * 
160    * @request.representation.example {@link Examples#NETWORK_UTILIZATION}
161    *
162    */
163   @PUT
164   @Path("preview")
165   @Consumes(MediaType.APPLICATION_JSON)
166   @Produces(MediaType.TEXT_HTML)
167   public String preview(String buffer) {
168     VelocityContext context = new VelocityContext();
169     StringWriter sw = null;
170     try {
171       Gson gson = new Gson();
172       Chart chart = gson.fromJson(buffer, Chart.class);
173       List<SeriesMetaData> series = chart.getSeries();
174       String seriesMetaData = gson.toJson(series);
175 
176       context.put("chart", chart);
177       context.put("seriesMetaData", seriesMetaData);
178       Template template = velocity.getTemplate("chart.vm");
179       sw = new StringWriter();
180       template.merge(context, sw);
181     } catch (Exception e) {
182       e.printStackTrace();
183       return e.getMessage();
184     }
185     return sw.toString();
186   }
187 
188   /**
189    * Display metrics series in JSON
190    * 
191    * @param request HTTP request object
192    * @param buffer list of SeriesMetaData
193    * @return metrics JSON
194    * 
195    * @request.representation.example {@link Examples#CPU_SERIES_METADATA}
196    * @response.representation.200.doc Display series data in JSON
197    * @response.representation.200.mediaType application/json
198    * 
199    */
200   @PUT
201   @Path("preview/series")
202   @Consumes(MediaType.APPLICATION_JSON)
203   @Produces("application/json")
204   public String previewSeries(@Context HttpServletRequest request, String buffer) {
205     Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
206     }.getType();
207     long startTime = 0;
208     long endTime = 0;
209     TimeHandler time = new TimeHandler(request);
210     startTime = time.getStartTime();
211     endTime = time.getEndTime();
212     Gson gson = new Gson();
213     ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
214     series = ChukwaHBaseStore.getChartSeries(series, startTime, endTime);
215     String result = gson.toJson(series);
216     return result;
217   }
218 
219 }