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.GET;
28  import javax.ws.rs.PUT;
29  import javax.ws.rs.Path;
30  import javax.ws.rs.PathParam;
31  import javax.ws.rs.Produces;
32  import javax.ws.rs.core.Context;
33  import javax.ws.rs.core.MediaType;
34  
35  import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
36  import org.apache.hadoop.chukwa.hicc.TimeHandler;
37  import org.apache.hadoop.chukwa.hicc.bean.Chart;
38  import org.apache.hadoop.chukwa.hicc.bean.SeriesMetaData;
39  import org.apache.log4j.Logger;
40  import org.apache.velocity.Template;
41  import org.apache.velocity.VelocityContext;
42  import org.apache.velocity.app.VelocityEngine;
43  
44  import com.google.gson.Gson;
45  import com.google.gson.reflect.TypeToken;
46  
47  @Path("/tile")
48  public class TileController extends ChartController{
49    static Logger LOG = Logger.getLogger(ChartController.class);
50    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
51  
52    @Context
53    VelocityEngine velocity;
54  
55    /**
56     * Render a banner
57     * 
58     * @param id Reference ID of Chart stored in HBase chukwa_meta table.
59     * @return html chart widget
60     */
61    @GET
62    @Path("draw/{id}")
63    @Produces(MediaType.TEXT_HTML)
64    public String draw(@PathParam("id") String id) {
65      VelocityContext context = new VelocityContext();
66      StringWriter sw = null;
67      try {
68        Chart chart = ChukwaHBaseStore.getChart(id);
69        List<SeriesMetaData> series = chart.getSeries();
70        Gson gson = new Gson();
71        String seriesMetaData = gson.toJson(series);
72  
73        context.put("chart", chart);
74        context.put("seriesMetaData", seriesMetaData);
75        Template template = velocity.getTemplate("tile.vm");
76        sw = new StringWriter();
77        template.merge(context, sw);
78      } catch (Exception e) {
79        e.printStackTrace();
80        return e.getMessage();
81      }
82      return sw.toString();
83    }
84  
85    /**
86     * Preview a banner tile
87     */
88    @PUT
89    @Path("preview")
90    public String preview(String buffer) {
91      VelocityContext context = new VelocityContext();
92      StringWriter sw = null;
93      try {
94        Gson gson = new Gson();
95        Chart chart = gson.fromJson(buffer, Chart.class);
96        List<SeriesMetaData> series = chart.getSeries();
97        String seriesMetaData = gson.toJson(series);
98  
99        context.put("chart", chart);
100       context.put("seriesMetaData", seriesMetaData);
101       Template template = velocity.getTemplate("tile.vm");
102       sw = new StringWriter();
103       template.merge(context, sw);
104     } catch (Exception e) {
105       e.printStackTrace();
106       return e.getMessage();
107     }
108     return sw.toString();
109   }
110 
111   @PUT
112   @Path("preview/series")
113   @Produces("application/json")
114   public String previewSeries(@Context HttpServletRequest request, String buffer) {
115     Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
116     }.getType();
117     long startTime = 0;
118     long endTime = 0;
119     TimeHandler time = new TimeHandler(request);
120     startTime = time.getStartTime();
121     endTime = time.getEndTime();
122     Gson gson = new Gson();
123     ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
124     List<String> data = ChukwaHBaseStore.getData(series, startTime, endTime);
125     String result = gson.toJson(data);
126     return result;
127   }
128 }