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