1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.library.web.spring;
17
18 import java.awt.Color;
19 import java.math.BigDecimal;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.jdiagnose.RemoteResult;
31 import org.jdiagnose.ResultInfo;
32 import org.jdiagnose.remote.provider.SystemHolder;
33 import org.jdiagnose.remote.provider.SystemProvider;
34 import org.jdiagnose.remote.system.RemoteAgentAtHost;
35 import org.jdiagnose.remote.system.RemoteDiagnostic;
36 import org.jdiagnose.remote.system.RemoteHost;
37 import org.jdiagnose.remote.system.RemoteSystem;
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 ChartDurationController extends ParameterizableViewController {
49
50 private static final org.apache.log4j.Category log = org.apache.log4j.Category
51 .getInstance(ChartDurationController.class);
52
53 private int chartWidth = 500;
54 private int chartHeight = 300;
55 private int bins = 20;
56
57 private SystemProvider provider = null;
58
59 /***
60 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest,
61 * javax.servlet.http.HttpServletResponse)
62 */
63 protected ModelAndView handleRequestInternal(HttpServletRequest request,
64 HttpServletResponse response) throws Exception {
65
66 String systemParam = request.getParameter("system");
67 String hostParam = request.getParameter("host");
68 String agentParam = request.getParameter("agent");
69 String diagnosticParam = request.getParameter("diagnostic");
70
71 if(hostParam == null || agentParam == null || diagnosticParam == null) {
72 return new ModelAndView(getViewName());
73 }
74
75 SystemHolder holder = provider.getHolder(systemParam);
76 RemoteSystem remoteSystem = holder.getRemoteSystem();
77
78 RemoteDiagnostic diagnostic = null;
79
80 RemoteHost host = remoteSystem.getHost(hostParam);
81 if(host != null) {
82 RemoteAgentAtHost agentAtHost = host.getAgent(agentParam);
83 if(agentAtHost != null) {
84 diagnostic = agentAtHost.getDiagnostic(diagnosticParam);
85 }
86 }
87
88 if(diagnostic == null) {
89 return new ModelAndView(getViewName());
90 }
91
92 double minimum = Long.MAX_VALUE;
93 double maximum = 0L;
94
95 long start = Long.MAX_VALUE;
96 long finish = 0L;
97
98 List durations = new ArrayList();
99 List failures = new ArrayList();
100 for (Iterator iterator = diagnostic.getRemoteResults(); iterator.hasNext();) {
101 RemoteResult result = (RemoteResult) iterator.next();
102 ResultInfo resultInfo = result.getResultInfo();
103 long duration = resultInfo.getDuration();
104 durations.add(new Double(duration));
105 if(duration < minimum) {
106 minimum = duration;
107 }
108 if(duration > maximum) {
109 maximum = duration;
110 }
111 long startTime = resultInfo.getStartTime();
112 if(startTime < start) {
113 start = startTime;
114 }
115 if(startTime > finish) {
116 finish = startTime;
117 }
118 }
119
120 double[] durationValues = new double[durations.size()];
121
122 {
123 int i = 0;
124 for (Iterator durationIterator = durations.iterator(); durationIterator.hasNext();) {
125 Double value = (Double) durationIterator.next();
126 durationValues[i++] = value.doubleValue();
127 }
128 }
129
130 minimum -= 0.00000000001;
131 maximum += 0.00000000001;
132
133 double binRange = (maximum - minimum) / bins;
134
135 int[] durationBins = getBins(minimum, durationValues, binRange, bins);
136
137 DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
138
139 for(int i = 0; i < bins; i++) {
140 dataSet.addValue(durationBins[i], "Duration", new BigDecimal(binRange * i).setScale(0, BigDecimal.ROUND_HALF_EVEN));
141 }
142
143 JFreeChart chart = ChartFactory.createStackedBarChart(
144 diagnosticParam,
145 "Duration", "Total",
146 dataSet,
147 PlotOrientation.HORIZONTAL,
148 true,
149 true,
150 false
151 );
152
153 chart.setBackgroundPaint(Color.white);
154
155 String chartImgSrc = ServletUtilities.saveChartAsPNG(chart, chartWidth, chartHeight, request.getSession(true));
156
157 Map model = new HashMap();
158 model.put("chartImgSrc", chartImgSrc);
159 model.put("startDate", new Date((long) start));
160 model.put("finishDate", new Date((long) finish));
161
162 return new ModelAndView(getViewName(), model);
163 }
164
165 /***
166 * @param start
167 * @param values
168 * @param binRange
169 * @return
170 */
171 protected int[] getBins(double start, double[] values, double binRange, int bins) {
172 int[] currentBins = new int[bins];
173
174 for(int i = 0; i < values.length; i++) {
175 int index = (int) ((values[i] - start) / binRange);
176 if(index == currentBins.length) {
177 index--;
178 }
179 currentBins[index]++;
180 }
181 return currentBins;
182 }
183
184 public void setChartWidth(int chartWidth) {
185 this.chartWidth = chartWidth;
186 }
187
188 public void setChartHeight(int chartHeight) {
189 this.chartHeight = chartHeight;
190 }
191
192 public void setProvider(SystemProvider provider) {
193 this.provider = provider;
194 }
195 public void setBins(int bins) {
196 this.bins = bins;
197 }
198 }