1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.monitoring.statistics.portal;
24  
25  import com.liferay.portal.monitoring.MonitoringException;
26  import com.liferay.portal.monitoring.statistics.RequestStatistics;
27  import com.liferay.portal.monitoring.statistics.SummaryStatistics;
28  
29  import java.util.Set;
30  
31  /**
32   * <a href="ServerSummaryStatistics.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Michael C. Han
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class ServerSummaryStatistics implements SummaryStatistics {
39  
40      public ServerSummaryStatistics(ServerStatistics serverStatistics) {
41          _serverStatistics = serverStatistics;
42      }
43  
44      public long getAverageTime() {
45          long averageTime = 0;
46  
47          Set<CompanyStatistics> companyStatisticsSet =
48              _serverStatistics.getCompanyStatisticsSet();
49  
50          for (CompanyStatistics companyStatistics : companyStatisticsSet) {
51              RequestStatistics requestStatistics =
52                  companyStatistics.getRequestStatistics();
53  
54              averageTime += requestStatistics.getAverageTime();
55          }
56  
57          return averageTime / companyStatisticsSet.size();
58      }
59  
60      public long getAverageTimeByCompany(long companyId)
61          throws MonitoringException {
62  
63          return getRequestStatistics(companyId).getAverageTime();
64      }
65  
66      public long getAverageTimeByCompany(String webId)
67          throws MonitoringException {
68  
69          return getRequestStatistics(webId).getAverageTime();
70      }
71  
72      public long getErrorCount() {
73          int errorCount = 0;
74  
75          for (CompanyStatistics companyStatistics :
76                  _serverStatistics.getCompanyStatisticsSet()) {
77  
78              errorCount +=
79                  companyStatistics.getRequestStatistics().getErrorCount();
80          }
81  
82          return errorCount;
83      }
84  
85      public long getErrorCountByCompany(long companyId)
86          throws MonitoringException {
87  
88          return getRequestStatistics(companyId).getErrorCount();
89      }
90  
91      public long getErrorCountByCompany(String webId)
92          throws MonitoringException {
93  
94          return getRequestStatistics(webId).getErrorCount();
95      }
96  
97      public long getMaxTime() {
98          long maxTime = 0;
99  
100         for (CompanyStatistics companyStatistics :
101                 _serverStatistics.getCompanyStatisticsSet()) {
102 
103             if (companyStatistics.getMaxTime() > maxTime) {
104                 maxTime = companyStatistics.getMaxTime();
105             }
106         }
107 
108         return maxTime;
109     }
110 
111     public long getMaxTimeByCompany(long companyId) throws MonitoringException {
112         return getRequestStatistics(companyId).getMaxTime();
113     }
114 
115     public long getMaxTimeByCompany(String webId) throws MonitoringException {
116         return getRequestStatistics(webId).getMaxTime();
117     }
118 
119     public long getMinTime() {
120         long minTime = 0;
121 
122         for (CompanyStatistics companyStatistics :
123                 _serverStatistics.getCompanyStatisticsSet()) {
124 
125             if (companyStatistics.getMinTime() < minTime) {
126                 minTime = companyStatistics.getMinTime();
127             }
128         }
129 
130         return minTime;
131     }
132 
133     public long getMinTimeByCompany(long companyId)
134         throws MonitoringException {
135 
136         return getRequestStatistics(companyId).getMinTime();
137     }
138 
139     public long getMinTimeByCompany(String webId)
140         throws MonitoringException {
141 
142         return getRequestStatistics(webId).getMinTime();
143     }
144 
145     public long getRequestCount() {
146         int requestCount = 0;
147 
148         for (CompanyStatistics companyStatistics :
149                 _serverStatistics.getCompanyStatisticsSet()) {
150 
151             requestCount +=
152                 companyStatistics.getRequestStatistics().getRequestCount();
153         }
154 
155         return requestCount;
156     }
157 
158     public long getRequestCountByCompany(long companyId)
159         throws MonitoringException {
160 
161         return getRequestStatistics(companyId).getRequestCount();
162     }
163 
164     public long getRequestCountByCompany(String webId)
165         throws MonitoringException {
166 
167         return getRequestStatistics(webId).getRequestCount();
168     }
169 
170     public long getSuccessCount() {
171         int successCount = 0;
172 
173         for (CompanyStatistics companyStatistics :
174                 _serverStatistics.getCompanyStatisticsSet()) {
175 
176             successCount +=
177                 companyStatistics.getRequestStatistics().getSuccessCount();
178         }
179 
180         return successCount;
181     }
182 
183     public long getSuccessCountByCompany(long companyId)
184         throws MonitoringException {
185 
186         return getRequestStatistics(companyId).getSuccessCount();
187     }
188 
189     public long getSuccessCountByCompany(String webId)
190         throws MonitoringException {
191 
192         return getRequestStatistics(webId).getSuccessCount();
193     }
194 
195     public long getTimeoutCount() {
196         int timeoutCount = 0;
197 
198         for (CompanyStatistics companyStatistics :
199                 _serverStatistics.getCompanyStatisticsSet()) {
200 
201             timeoutCount +=
202                 companyStatistics.getRequestStatistics().getTimeoutCount();
203         }
204 
205         return timeoutCount;
206     }
207 
208     public long getTimeoutCountByCompany(long companyId)
209         throws MonitoringException {
210 
211         return getRequestStatistics(companyId).getTimeoutCount();
212     }
213 
214     public long getTimeoutCountByCompany(String webId)
215         throws MonitoringException {
216 
217         return getRequestStatistics(webId).getTimeoutCount();
218     }
219 
220     protected RequestStatistics getRequestStatistics(long companyId)
221         throws MonitoringException {
222 
223         try {
224             CompanyStatistics companyStatistics =
225                 _serverStatistics.getCompanyStatistics(companyId);
226 
227             return companyStatistics.getRequestStatistics();
228         }
229         catch (Exception e) {
230             throw new MonitoringException(
231                 "Unable to get company with company id " + companyId, e);
232         }
233     }
234 
235     protected RequestStatistics getRequestStatistics(String webId)
236         throws MonitoringException {
237 
238         try {
239             CompanyStatistics companyStatistics =
240                 _serverStatistics.getCompanyStatistics(webId);
241 
242             return companyStatistics.getRequestStatistics();
243         }
244         catch (Exception e) {
245             throw new MonitoringException(
246                 "Unable to get company with web id " + webId, e);
247         }
248     }
249 
250     private ServerStatistics _serverStatistics;
251 
252 }