This project has retired. For details please refer to its Attic page.
TileController 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.PUT;
30  import javax.ws.rs.Path;
31  import javax.ws.rs.PathParam;
32  import javax.ws.rs.Produces;
33  import javax.ws.rs.core.Context;
34  import javax.ws.rs.core.MediaType;
35  
36  import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
37  import org.apache.hadoop.chukwa.hicc.TimeHandler;
38  import org.apache.hadoop.chukwa.hicc.bean.Chart;
39  import org.apache.hadoop.chukwa.hicc.bean.SeriesMetaData;
40  import org.apache.log4j.Logger;
41  import org.apache.velocity.Template;
42  import org.apache.velocity.VelocityContext;
43  import org.apache.velocity.app.VelocityEngine;
44  
45  import com.google.gson.Gson;
46  import com.google.gson.reflect.TypeToken;
47  
48  @Path("/tile")
49  public class TileController extends ChartController{
50    static Logger LOG = Logger.getLogger(ChartController.class);
51    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
52  
53    @Context
54    VelocityEngine velocity;
55  
56    /**
57     * Render a banner
58     * 
59     * @param id Reference ID of Chart stored in HBase chukwa_meta table.
60     * @return html chart widget
61     * 
62     * @response.representation.200.doc Render a banner base on chart id
63     * @response.representation.200.mediaType text/html
64     * @response.representation.200.example Example available in HICC UI
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("tile.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     * Preview a banner tile
92     * 
93     * @param buffer is tile object in JSON
94     * @return html for rendering a banner tile
95     * 
96     * @response.representation.200.doc Preview a banner
97     * @response.representation.200.mediaType text/html
98     * @response.representation.200.example Example available in HICC UI
99     */
100   @PUT
101   @Path("preview")
102   @Consumes(MediaType.APPLICATION_JSON)
103   @Produces(MediaType.TEXT_HTML)
104   public String preview(String buffer) {
105     VelocityContext context = new VelocityContext();
106     StringWriter sw = null;
107     try {
108       Gson gson = new Gson();
109       Chart chart = gson.fromJson(buffer, Chart.class);
110       List<SeriesMetaData> series = chart.getSeries();
111       String seriesMetaData = gson.toJson(series);
112 
113       context.put("chart", chart);
114       context.put("seriesMetaData", seriesMetaData);
115       Template template = velocity.getTemplate("tile.vm");
116       sw = new StringWriter();
117       template.merge(context, sw);
118     } catch (Exception e) {
119       e.printStackTrace();
120       return e.getMessage();
121     }
122     return sw.toString();
123   }
124 
125   /**
126    * Preview a series JSON for banner tile
127    * 
128    * @param request HTTP request object
129    * @param buffer is banner tile configuration
130    * 
131    * @request.representation.example {@link Examples#CPU_SERIES_METADATA}
132    * @response.representation.200.doc Preview a banner series
133    * @response.representation.200.mediaType application/json
134    * @response.representation.200.example Example available in REST API
135    */
136   @PUT
137   @Path("preview/series")
138   @Produces(MediaType.APPLICATION_JSON)
139   public String previewSeries(@Context HttpServletRequest request, String buffer) {
140     Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
141     }.getType();
142     long startTime = 0;
143     long endTime = 0;
144     TimeHandler time = new TimeHandler(request);
145     startTime = time.getStartTime();
146     endTime = time.getEndTime();
147     Gson gson = new Gson();
148     ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
149     List<String> data = ChukwaHBaseStore.getData(series, startTime, endTime);
150     String result = gson.toJson(data);
151     return result;
152   }
153 }