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 html chart widget
64     */
65    @GET
66    @Path("draw/{id}")
67    @Produces(MediaType.TEXT_HTML)
68    public String draw(@PathParam("id") String id) {
69      VelocityContext context = new VelocityContext();
70      StringWriter sw = null;
71      try {
72        Chart chart = ChukwaHBaseStore.getChart(id);
73        List<SeriesMetaData> series = chart.getSeries();
74        Gson gson = new Gson();
75        String seriesMetaData = gson.toJson(series);
76  
77        context.put("chart", chart);
78        context.put("seriesMetaData", seriesMetaData);
79        Template template = velocity.getTemplate("chart.vm");
80        sw = new StringWriter();
81        template.merge(context, sw);
82      } catch (Exception e) {
83        e.printStackTrace();
84        return e.getMessage();
85      }
86      return sw.toString();
87    }
88  
89    /**
90     * Describe chart meta data
91     * @param id 
92     * @return 
93     */
94    @GET
95    @Path("describe/{id}")
96    @Produces(MediaType.APPLICATION_JSON)
97    public String describe(@PathParam("id") String id) {
98      Chart chart = ChukwaHBaseStore.getChart(id);
99      Gson gson = new Gson();
100     String buffer = gson.toJson(chart);
101     return buffer;
102   }
103 
104   /**
105    * Create a new chart meta data
106    * 
107    * @param buffer JSON description of a chart object
108    * @return web response
109    */
110   @POST
111   @Path("save")
112   @Consumes(MediaType.APPLICATION_JSON)
113   public Response create(String buffer) {
114     Gson gson = new Gson();
115     Chart chart = gson.fromJson(buffer, Chart.class);
116     String id = ChukwaHBaseStore.createChart(chart);
117     if (id != null) {
118       return Response.ok(id).build();
119     }
120     return Responses.notAcceptable().build();
121   }
122 
123   /**
124    * Save chart meta data
125    * 
126    * @param id chart id
127    * @param buffer JSON description of a chart object
128    * @return web response
129    */
130   @PUT
131   @Path("save/{id}")
132   @Consumes(MediaType.APPLICATION_JSON)
133   public Response save(@PathParam("id") String id, String buffer) {
134     Gson gson = new Gson();
135     Chart chart = gson.fromJson(buffer, Chart.class);
136     ChukwaHBaseStore.putChart(id, chart);
137     return Response.ok().build();
138     
139   }
140 
141   /**
142    * Preview a chart
143    * @param buffer 
144    * @return 
145    */
146   @PUT
147   @Path("preview")
148   public String preview(String buffer) {
149     VelocityContext context = new VelocityContext();
150     StringWriter sw = null;
151     try {
152       Gson gson = new Gson();
153       Chart chart = gson.fromJson(buffer, Chart.class);
154       List<SeriesMetaData> series = chart.getSeries();
155       String seriesMetaData = gson.toJson(series);
156 
157       context.put("chart", chart);
158       context.put("seriesMetaData", seriesMetaData);
159       Template template = velocity.getTemplate("chart.vm");
160       sw = new StringWriter();
161       template.merge(context, sw);
162     } catch (Exception e) {
163       e.printStackTrace();
164       return e.getMessage();
165     }
166     return sw.toString();
167   }
168 
169   @PUT
170   @Path("preview/series")
171   @Produces("application/json")
172   public String previewSeries(@Context HttpServletRequest request, String buffer) {
173     Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
174     }.getType();
175     long startTime = 0;
176     long endTime = 0;
177     TimeHandler time = new TimeHandler(request);
178     startTime = time.getStartTime();
179     endTime = time.getEndTime();
180     Gson gson = new Gson();
181     ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
182     series = ChukwaHBaseStore.getChartSeries(series, startTime, endTime);
183     String result = gson.toJson(series);
184     return result;
185   }
186 
187 }