This project has retired. For details please refer to its Attic page.
PieChartController 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.ChartType;
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("/piechart")
49  public class PieChartController 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 pie chart using chartist.js
58     * 
59     * @param id Reference ID of Chart stored in HBase chukwa_meta table.
60     * @return html chart widget
61     */
62    @GET
63    @Path("draw/{id}")
64    @Produces(MediaType.TEXT_HTML)
65    public String draw(@PathParam("id") String id) {
66      VelocityContext context = new VelocityContext();
67      StringWriter sw = null;
68      try {
69        Chart chart = ChukwaHBaseStore.getChart(id);
70        List<SeriesMetaData> series = chart.getSeries();
71        Gson gson = new Gson();
72        String seriesMetaData = gson.toJson(series);
73  
74        context.put("chart", chart);
75        context.put("seriesMetaData", seriesMetaData);
76        context.put("chartTypeDonut", ChartType.DONUT);
77        Template template = velocity.getTemplate("pie.vm");
78        sw = new StringWriter();
79        template.merge(context, sw);
80      } catch (Exception e) {
81        e.printStackTrace();
82        return e.getMessage();
83      }
84      return sw.toString();
85    }
86  
87    /**
88     * Preview a pie chart
89     */
90    @PUT
91    @Path("preview")
92    public String preview(String buffer) {
93      VelocityContext context = new VelocityContext();
94      StringWriter sw = null;
95      try {
96        Gson gson = new Gson();
97        Chart chart = gson.fromJson(buffer, Chart.class);
98        List<SeriesMetaData> series = chart.getSeries();
99        String seriesMetaData = gson.toJson(series);
100 
101       context.put("chart", chart);
102       context.put("seriesMetaData", seriesMetaData);
103       context.put("chartTypeDonut", ChartType.DONUT);
104       Template template = velocity.getTemplate("pie.vm");
105       sw = new StringWriter();
106       template.merge(context, sw);
107     } catch (Exception e) {
108       e.printStackTrace();
109       return e.getMessage();
110     }
111     return sw.toString();
112   }
113 
114   @PUT
115   @Path("preview/series")
116   @Produces("application/json")
117   public String previewSeries(@Context HttpServletRequest request, String buffer) {
118     Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
119     }.getType();
120     long startTime = 0;
121     long endTime = 0;
122     TimeHandler time = new TimeHandler(request);
123     startTime = time.getStartTime();
124     endTime = time.getEndTime();
125     Gson gson = new Gson();
126     ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
127     List<String> data = ChukwaHBaseStore.getData(series, startTime, endTime);
128     String result = gson.toJson(data);
129     return result;
130   }
131 }