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  public class ServerSummaryStatistics implements SummaryStatistics {
38  
39      public long getAverageTime() {
40          long averageTime = 0;
41  
42          Set<CompanyStatistics> companyStatisticsSet =
43              _serverStatistics.getCompanyStatisticsSet();
44  
45          for (CompanyStatistics companyStatistics : companyStatisticsSet) {
46              RequestStatistics requestStatistics =
47                  companyStatistics.getRequestStatistics();
48  
49              averageTime += requestStatistics.getAverageTime();
50          }
51  
52          return averageTime / companyStatisticsSet.size();
53      }
54  
55      public long getAverageTimeByCompany(long companyId)
56          throws MonitoringException {
57  
58          return getRequestStatistics(companyId).getAverageTime();
59      }
60  
61      public long getAverageTimeByCompany(String webId)
62          throws MonitoringException {
63  
64          return getRequestStatistics(webId).getAverageTime();
65      }
66  
67      public long getErrorCount() {
68          int errorCount = 0;
69  
70          for (CompanyStatistics companyStatistics :
71                  _serverStatistics.getCompanyStatisticsSet()) {
72  
73              errorCount +=
74                  companyStatistics.getRequestStatistics().getErrorCount();
75          }
76  
77          return errorCount;
78      }
79  
80      public long getErrorCountByCompany(long companyId)
81          throws MonitoringException {
82  
83          return getRequestStatistics(companyId).getErrorCount();
84      }
85  
86      public long getErrorCountByCompany(String webId)
87          throws MonitoringException {
88  
89          return getRequestStatistics(webId).getErrorCount();
90      }
91  
92      public long getMaxTime() {
93          long maxTime = 0;
94  
95          for (CompanyStatistics companyStatistics :
96                  _serverStatistics.getCompanyStatisticsSet()) {
97  
98              if (companyStatistics.getMaxTime() > maxTime) {
99                  maxTime = companyStatistics.getMaxTime();
100             }
101         }
102 
103         return maxTime;
104     }
105 
106     public long getMaxTimeByCompany(long companyId) throws MonitoringException {
107         return getRequestStatistics(companyId).getMaxTime();
108     }
109 
110     public long getMaxTimeByCompany(String webId) throws MonitoringException {
111         return getRequestStatistics(webId).getMaxTime();
112     }
113 
114     public long getMinTime() {
115         long minTime = 0;
116 
117         for (CompanyStatistics companyStatistics :
118                 _serverStatistics.getCompanyStatisticsSet()) {
119 
120             if (companyStatistics.getMinTime() < minTime) {
121                 minTime = companyStatistics.getMinTime();
122             }
123         }
124 
125         return minTime;
126     }
127 
128     public long getMinTimeByCompany(long companyId)
129         throws MonitoringException {
130 
131         return getRequestStatistics(companyId).getMinTime();
132     }
133 
134     public long getMinTimeByCompany(String webId)
135         throws MonitoringException {
136 
137         return getRequestStatistics(webId).getMinTime();
138     }
139 
140     public long getRequestCount() {
141         int requestCount = 0;
142 
143         for (CompanyStatistics companyStatistics :
144                 _serverStatistics.getCompanyStatisticsSet()) {
145 
146             requestCount +=
147                 companyStatistics.getRequestStatistics().getRequestCount();
148         }
149 
150         return requestCount;
151     }
152 
153     public long getRequestCountByCompany(long companyId)
154         throws MonitoringException {
155 
156         return getRequestStatistics(companyId).getRequestCount();
157     }
158 
159     public long getRequestCountByCompany(String webId)
160         throws MonitoringException {
161 
162         return getRequestStatistics(webId).getRequestCount();
163     }
164 
165     public long getSuccessCount() {
166         int successCount = 0;
167 
168         for (CompanyStatistics companyStatistics :
169                 _serverStatistics.getCompanyStatisticsSet()) {
170 
171             successCount +=
172                 companyStatistics.getRequestStatistics().getSuccessCount();
173         }
174 
175         return successCount;
176     }
177 
178     public long getSuccessCountByCompany(long companyId)
179         throws MonitoringException {
180 
181         return getRequestStatistics(companyId).getSuccessCount();
182     }
183 
184     public long getSuccessCountByCompany(String webId)
185         throws MonitoringException {
186 
187         return getRequestStatistics(webId).getSuccessCount();
188     }
189 
190     public long getTimeoutCount() {
191         int timeoutCount = 0;
192 
193         for (CompanyStatistics companyStatistics :
194                 _serverStatistics.getCompanyStatisticsSet()) {
195 
196             timeoutCount +=
197                 companyStatistics.getRequestStatistics().getTimeoutCount();
198         }
199 
200         return timeoutCount;
201     }
202 
203     public long getTimeoutCountByCompany(long companyId)
204         throws MonitoringException {
205 
206         return getRequestStatistics(companyId).getTimeoutCount();
207     }
208 
209     public long getTimeoutCountByCompany(String webId)
210         throws MonitoringException {
211 
212         return getRequestStatistics(webId).getTimeoutCount();
213     }
214 
215     public void setServerStatistics(ServerStatistics serverStatistics) {
216         _serverStatistics = serverStatistics;
217     }
218 
219     protected RequestStatistics getRequestStatistics(long companyId)
220         throws MonitoringException {
221 
222         try {
223             CompanyStatistics companyStatistics =
224                 _serverStatistics.getCompanyStatistics(companyId);
225 
226             return companyStatistics.getRequestStatistics();
227         }
228         catch (Exception e) {
229             throw new MonitoringException(
230                 "Unable to get company with company id " + companyId, e);
231         }
232     }
233 
234     protected RequestStatistics getRequestStatistics(String webId)
235         throws MonitoringException {
236 
237         try {
238             CompanyStatistics companyStatistics =
239                 _serverStatistics.getCompanyStatistics(webId);
240 
241             return companyStatistics.getRequestStatistics();
242         }
243         catch (Exception e) {
244             throw new MonitoringException(
245                 "Unable to get company with web id " + webId, e);
246         }
247     }
248 
249     private ServerStatistics _serverStatistics;
250 
251 }