View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.web.spring;
17  
18  import java.awt.Color;
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.jdiagnose.RemoteResult;
30  import org.jdiagnose.ResultInfo;
31  import org.jdiagnose.remote.provider.SystemHolder;
32  import org.jdiagnose.remote.provider.SystemProvider;
33  import org.jdiagnose.remote.system.RemoteAgentAtHost;
34  import org.jdiagnose.remote.system.RemoteDiagnostic;
35  import org.jdiagnose.remote.system.RemoteHost;
36  import org.jdiagnose.remote.system.RemoteSystem;
37  import org.jdiagnose.runtime.ResultState;
38  import org.jfree.chart.ChartFactory;
39  import org.jfree.chart.JFreeChart;
40  import org.jfree.chart.plot.PlotOrientation;
41  import org.jfree.chart.servlet.ServletUtilities;
42  import org.jfree.data.DefaultCategoryDataset;
43  import org.springframework.web.servlet.ModelAndView;
44  import org.springframework.web.servlet.mvc.ParameterizableViewController;
45  
46  /***
47   */
48  public class ChartHistogramController extends ParameterizableViewController {
49  
50      private static final org.apache.log4j.Category log = org.apache.log4j.Category
51              .getInstance(ChartHistogramController.class);
52  
53      private SystemProvider provider = null;
54  
55      private int chartWidth = 500;
56      private int chartHeight = 300;
57      private int timePeriod = 60 * 60 * 1000;
58      private long minimumTime = 24 * 60 * 60 * 1000;
59      
60      private static class Clear implements Comparable {
61  
62          private int value;
63  
64          public Clear(int value) {
65              this.value = value;
66          }
67  
68          public int compareTo(Object o) {
69              int otherValue = ((Clear) o).value;
70              return value - otherValue; 
71          }
72          
73          public boolean equals(Object o) {
74              if(o == null) return false;
75              int otherValue = ((Clear) o).value;
76              return value == otherValue; 
77          }
78          
79          public String toString() {
80              return "";
81          }
82          
83      }
84  
85      /***
86       * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest,
87       *      javax.servlet.http.HttpServletResponse)
88       */
89      protected ModelAndView handleRequestInternal(HttpServletRequest request,
90              HttpServletResponse response) throws Exception {
91          String hostParam = request.getParameter("host");
92          String agentParam = request.getParameter("agent");
93          String diagnosticParam = request.getParameter("diagnostic");
94          String systemParam = request.getParameter("system");
95          
96          if(hostParam == null || agentParam == null || diagnosticParam == null) {
97              return new ModelAndView(getViewName());
98          }
99          
100         SystemHolder holder = provider.getHolder(systemParam);
101         RemoteSystem remoteSystem = holder.getRemoteSystem();
102 
103         RemoteDiagnostic diagnostic = null;
104         
105         RemoteHost host = remoteSystem.getHost(hostParam);
106         if(host != null) {
107             RemoteAgentAtHost agentAtHost = host.getAgent(agentParam);
108             if(agentAtHost != null) {
109                 diagnostic = agentAtHost.getDiagnostic(diagnosticParam);
110             }
111         }
112         
113         if(diagnostic == null) {
114             return new ModelAndView(getViewName());
115         }
116 
117         double start = Long.MAX_VALUE;
118         double finish = 0L;
119         List successes = new ArrayList();
120         List failures = new ArrayList();
121         for (Iterator iterator = diagnostic.getRemoteResults(); iterator.hasNext();) {
122             RemoteResult result = (RemoteResult) iterator.next();
123             ResultInfo resultInfo = result.getResultInfo();
124             long startTime = resultInfo.getStartTime();
125             if(resultInfo.getState() == ResultState.SUCCEEDED) {
126                 successes.add(new Double(startTime));
127             } else if (resultInfo.getState() == ResultState.FAILED) {
128                 failures.add(new Double(startTime));
129             }
130             if(startTime < start) {
131                 start = startTime;
132             }
133             if(startTime > finish) {
134                 finish = startTime;
135             }
136         }
137         
138         double[] successValues = new double[successes.size()];
139 
140         {
141             int i = 0;
142             for (Iterator successIterator = successes.iterator(); successIterator.hasNext();) {
143                 Double value = (Double) successIterator.next();
144                 successValues[i++] = value.doubleValue();
145             }
146         }
147         
148         double[] failureValues = new double[failures.size()];
149 
150         {
151             int i = 0;
152             for (Iterator failureIterator = failures.iterator(); failureIterator.hasNext();) {
153                 Double value = (Double) failureIterator.next();
154                 failureValues[i++] = value.doubleValue();
155             }
156         }
157         
158         start--; finish++;
159         
160         if(finish - start < minimumTime) {
161             finish = start + minimumTime;
162         }
163         
164         int bins = (int) Math.rint(((finish - start) / timePeriod));
165 
166         double binRange = (finish - start) / bins;
167 
168         int[] failureBins = getBins(start, failureValues, binRange, bins);
169         int[] successBins = getBins(start, successValues, binRange, bins);
170 
171         DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
172 
173         for(int i = 0; i < bins; i++) {
174             dataSet.addValue(failureBins[i], "Failures", new Clear(i));
175             dataSet.addValue(successBins[i], "Successes", new Clear(i));
176         }
177         
178         JFreeChart chart = ChartFactory.createStackedBarChart(
179             diagnosticParam,
180             "Time", "State",
181             dataSet,
182             PlotOrientation.VERTICAL,
183             true,
184             true,
185             false
186         );
187         
188         chart.setBackgroundPaint(Color.white);
189 
190         String chartImgSrc = ServletUtilities.saveChartAsPNG(chart, chartWidth, chartHeight, request.getSession(true));
191         
192         Map model = new HashMap();
193         model.put("chartImgSrc", chartImgSrc);
194         model.put("startDate", new Date((long) start));
195         model.put("finishDate", new Date((long) finish));
196         
197         return new ModelAndView(getViewName(), model);
198     }
199 
200     /***
201      * @param start
202      * @param values
203      * @param binRange
204      * @return
205      */
206     protected int[] getBins(double start, double[] values, double binRange, int bins) {
207         int[] currentBins = new int[bins];
208         
209         for(int i = 0; i < values.length; i++) {
210             int index = (int) ((values[i] - start) / binRange);
211             if(index == currentBins.length) {
212                 index--;
213             }
214             currentBins[index]++;
215         }
216         return currentBins;
217     }
218 
219     public void setChartWidth(int chartWidth) {
220         this.chartWidth = chartWidth;
221     }
222 
223     public void setChartHeight(int chartHeight) {
224         this.chartHeight = chartHeight;
225     }
226 
227     public void setMinimumTime(long minimumTime) {
228         this.minimumTime = minimumTime;
229     }
230     public void setProvider(SystemProvider provider) {
231         this.provider = provider;
232     }
233     public void setTimePeriod(int timePeriod) {
234         this.timePeriod = timePeriod;
235     }
236 }