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.Date;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.jdiagnose.RemoteResult;
28  import org.jdiagnose.remote.provider.SystemHolder;
29  import org.jdiagnose.remote.provider.SystemProvider;
30  import org.jdiagnose.remote.system.RemoteAgentAtHost;
31  import org.jdiagnose.remote.system.RemoteDiagnostic;
32  import org.jdiagnose.remote.system.RemoteHost;
33  import org.jdiagnose.remote.system.RemoteSystem;
34  import org.jfree.chart.ChartFactory;
35  import org.jfree.chart.JFreeChart;
36  import org.jfree.chart.servlet.ServletUtilities;
37  import org.jfree.data.time.Millisecond;
38  import org.jfree.data.time.TimeSeries;
39  import org.jfree.data.time.TimeSeriesCollection;
40  import org.springframework.web.servlet.ModelAndView;
41  import org.springframework.web.servlet.mvc.ParameterizableViewController;
42  
43  /***
44   */
45  public class ChartDiagnosticResultsController extends ParameterizableViewController {
46  
47      private static final org.apache.log4j.Category log = org.apache.log4j.Category
48              .getInstance(ChartDiagnosticResultsController.class);
49  
50      private int chartWidth = 500;
51      private int chartHeight = 300;
52      
53      private SystemProvider provider = null;
54  
55      /***
56       * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest,
57       *      javax.servlet.http.HttpServletResponse)
58       */
59      protected ModelAndView handleRequestInternal(HttpServletRequest request,
60              HttpServletResponse response) throws Exception {
61          
62          String systemParam = request.getParameter("system");
63          String hostParam = request.getParameter("host");
64          String agentParam = request.getParameter("agent");
65          String diagnosticParam = request.getParameter("diagnostic");
66          
67          if(hostParam == null || agentParam == null || diagnosticParam == null || systemParam == null) {
68              return new ModelAndView(getViewName());
69          }
70          
71          SystemHolder holder = provider.getHolder(systemParam);
72          RemoteSystem remoteSystem = holder.getRemoteSystem();
73  
74          RemoteDiagnostic diagnostic = null;
75          
76          RemoteHost host = remoteSystem.getHost(hostParam);
77          if(host != null) {
78              RemoteAgentAtHost agentAtHost = host.getAgent(agentParam);
79              if(agentAtHost != null) {
80                  diagnostic = agentAtHost.getDiagnostic(diagnosticParam);
81              }
82          }
83          
84          if(diagnostic == null) {
85              return new ModelAndView(getViewName());
86          }
87  
88          long start = 0L;
89          long finish = 0L;
90          
91          TimeSeries resultSeries = new TimeSeries(diagnosticParam, Millisecond.class);
92          for (Iterator iterator = diagnostic.getRemoteResults(); iterator.hasNext();) {
93              RemoteResult diagnosticResult = (RemoteResult) iterator.next();
94              if(start == 0L) {
95                  start = diagnosticResult.getResultInfo().getStartTime(); 
96              }
97              resultSeries.addOrUpdate(new Millisecond(new Date(diagnosticResult.getResultInfo().getStartTime())),
98                      diagnosticResult.getResultInfo().getDuration());
99              if(!iterator.hasNext()) {
100                 finish = diagnosticResult.getResultInfo().getStartTime();
101             }
102         }
103 
104         TimeSeriesCollection dataset = new TimeSeriesCollection();
105 
106         dataset.addSeries(resultSeries);
107         JFreeChart chart = ChartFactory.createTimeSeriesChart(
108             diagnosticParam,
109             "Date", "Duration",
110             dataset,
111             true,
112             true,
113             false
114         );
115         
116         chart.setBackgroundPaint(Color.white);
117 
118         String chartImgSrc = ServletUtilities.saveChartAsPNG(chart, chartWidth, chartHeight, request.getSession(true));
119 
120         Map model = new HashMap();
121         model.put("chartImgSrc", chartImgSrc);
122         model.put("startDate", new Date(start == 0 ? System.currentTimeMillis() : start));
123         model.put("finishDate", new Date(finish == 0 ? System.currentTimeMillis() : finish));
124         
125         return new ModelAndView(getViewName(), model);
126     }
127 
128     public void setChartWidth(int chartWidth) {
129         this.chartWidth = chartWidth;
130     }
131 
132     public void setChartHeight(int chartHeight) {
133         this.chartHeight = chartHeight;
134     }
135 
136     public void setProvider(SystemProvider provider) {
137         this.provider = provider;
138     }
139 }