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.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.ChartType;
40  import org.apache.hadoop.chukwa.hicc.bean.SeriesMetaData;
41  import org.apache.log4j.Logger;
42  import org.apache.velocity.Template;
43  import org.apache.velocity.VelocityContext;
44  import org.apache.velocity.app.VelocityEngine;
45  
46  import com.google.gson.Gson;
47  import com.google.gson.reflect.TypeToken;
48  
49  @Path("/piechart")
50  public class PieChartController extends ChartController{
51    static Logger LOG = Logger.getLogger(ChartController.class);
52    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
53  
54    @Context
55    VelocityEngine velocity;
56  
57    /**
58     * Render pie chart using chartist.js
59     * 
60     * @param id Reference ID of Chart stored in HBase chukwa_meta table.
61     * @return html chart widget
62     * 
63     * @response.representation.200.doc Preview a pie chart
64     * @response.representation.200.mediaType text/html
65     * @response.representation.200.example Example available in HICC UI
66     */
67    @GET
68    @Path("draw/{id}")
69    @Produces(MediaType.TEXT_HTML)
70    public String draw(@PathParam("id") String id) {
71      VelocityContext context = new VelocityContext();
72      StringWriter sw = null;
73      try {
74        Chart chart = ChukwaHBaseStore.getChart(id);
75        List<SeriesMetaData> series = chart.getSeries();
76        Gson gson = new Gson();
77        String seriesMetaData = gson.toJson(series);
78  
79        context.put("chart", chart);
80        context.put("seriesMetaData", seriesMetaData);
81        context.put("chartTypeDonut", ChartType.DONUT);
82        Template template = velocity.getTemplate("pie.vm");
83        sw = new StringWriter();
84        template.merge(context, sw);
85      } catch (Exception e) {
86        e.printStackTrace();
87        return e.getMessage();
88      }
89      return sw.toString();
90    }
91  
92    /**
93     * Preview a pie chart
94     * 
95     * @param buffer is pie chart object
96     * 
97     * @response.representation.200.doc Preview a pie chart
98     * @response.representation.200.mediaType text/html
99     * @response.representation.200.example Example available in HICC UI
100    */
101   @PUT
102   @Path("preview")
103   @Consumes(MediaType.APPLICATION_JSON)
104   @Produces(MediaType.TEXT_HTML)
105   public String preview(String buffer) {
106     VelocityContext context = new VelocityContext();
107     StringWriter sw = null;
108     try {
109       Gson gson = new Gson();
110       Chart chart = gson.fromJson(buffer, Chart.class);
111       List<SeriesMetaData> series = chart.getSeries();
112       String seriesMetaData = gson.toJson(series);
113 
114       context.put("chart", chart);
115       context.put("seriesMetaData", seriesMetaData);
116       context.put("chartTypeDonut", ChartType.DONUT);
117       Template template = velocity.getTemplate("pie.vm");
118       sw = new StringWriter();
119       template.merge(context, sw);
120     } catch (Exception e) {
121       e.printStackTrace();
122       return e.getMessage();
123     }
124     return sw.toString();
125   }
126 
127   /**
128    * Preview a series JSON for pie chart
129    * 
130    * @param request HTTP request object
131    * @param buffer is pie chart configuration
132    * 
133    * @request.representation.example {@link Examples#CPU_SERIES_METADATA}
134    * @response.representation.200.doc Preview a pie chart series
135    * @response.representation.200.mediaType application/json
136    * @response.representation.200.example Example available in REST API
137    */
138   @PUT
139   @Path("preview/series")
140   @Consumes(MediaType.APPLICATION_JSON)
141   @Produces("application/json")
142   public String previewSeries(@Context HttpServletRequest request, String buffer) {
143     Type listType = new TypeToken<ArrayList<SeriesMetaData>>() {
144     }.getType();
145     long startTime = 0;
146     long endTime = 0;
147     TimeHandler time = new TimeHandler(request);
148     startTime = time.getStartTime();
149     endTime = time.getEndTime();
150     Gson gson = new Gson();
151     ArrayList<SeriesMetaData> series = gson.fromJson(buffer, listType);
152     List<String> data = ChukwaHBaseStore.getData(series, startTime, endTime);
153     String result = gson.toJson(data);
154     return result;
155   }
156 }