001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.monitoring.statistics.portal;
016    
017    import com.liferay.portal.monitoring.MonitoringException;
018    import com.liferay.portal.monitoring.statistics.RequestStatistics;
019    import com.liferay.portal.monitoring.statistics.SummaryStatistics;
020    
021    import java.util.Set;
022    
023    /**
024     * @author Michael C. Han
025     * @author Brian Wing Shun Chan
026     */
027    public class ServerSummaryStatistics implements SummaryStatistics {
028    
029            public long getAverageTime() {
030                    long averageTime = 0;
031    
032                    Set<CompanyStatistics> companyStatisticsSet =
033                            _serverStatistics.getCompanyStatisticsSet();
034    
035                    for (CompanyStatistics companyStatistics : companyStatisticsSet) {
036                            RequestStatistics requestStatistics =
037                                    companyStatistics.getRequestStatistics();
038    
039                            averageTime += requestStatistics.getAverageTime();
040                    }
041    
042                    return averageTime / companyStatisticsSet.size();
043            }
044    
045            public long getAverageTimeByCompany(long companyId)
046                    throws MonitoringException {
047    
048                    return getRequestStatistics(companyId).getAverageTime();
049            }
050    
051            public long getAverageTimeByCompany(String webId)
052                    throws MonitoringException {
053    
054                    return getRequestStatistics(webId).getAverageTime();
055            }
056    
057            public long getErrorCount() {
058                    int errorCount = 0;
059    
060                    for (CompanyStatistics companyStatistics :
061                                    _serverStatistics.getCompanyStatisticsSet()) {
062    
063                            errorCount +=
064                                    companyStatistics.getRequestStatistics().getErrorCount();
065                    }
066    
067                    return errorCount;
068            }
069    
070            public long getErrorCountByCompany(long companyId)
071                    throws MonitoringException {
072    
073                    return getRequestStatistics(companyId).getErrorCount();
074            }
075    
076            public long getErrorCountByCompany(String webId)
077                    throws MonitoringException {
078    
079                    return getRequestStatistics(webId).getErrorCount();
080            }
081    
082            public long getMaxTime() {
083                    long maxTime = 0;
084    
085                    for (CompanyStatistics companyStatistics :
086                                    _serverStatistics.getCompanyStatisticsSet()) {
087    
088                            if (companyStatistics.getMaxTime() > maxTime) {
089                                    maxTime = companyStatistics.getMaxTime();
090                            }
091                    }
092    
093                    return maxTime;
094            }
095    
096            public long getMaxTimeByCompany(long companyId) throws MonitoringException {
097                    return getRequestStatistics(companyId).getMaxTime();
098            }
099    
100            public long getMaxTimeByCompany(String webId) throws MonitoringException {
101                    return getRequestStatistics(webId).getMaxTime();
102            }
103    
104            public long getMinTime() {
105                    long minTime = 0;
106    
107                    for (CompanyStatistics companyStatistics :
108                                    _serverStatistics.getCompanyStatisticsSet()) {
109    
110                            if (companyStatistics.getMinTime() < minTime) {
111                                    minTime = companyStatistics.getMinTime();
112                            }
113                    }
114    
115                    return minTime;
116            }
117    
118            public long getMinTimeByCompany(long companyId)
119                    throws MonitoringException {
120    
121                    return getRequestStatistics(companyId).getMinTime();
122            }
123    
124            public long getMinTimeByCompany(String webId)
125                    throws MonitoringException {
126    
127                    return getRequestStatistics(webId).getMinTime();
128            }
129    
130            public long getRequestCount() {
131                    int requestCount = 0;
132    
133                    for (CompanyStatistics companyStatistics :
134                                    _serverStatistics.getCompanyStatisticsSet()) {
135    
136                            requestCount +=
137                                    companyStatistics.getRequestStatistics().getRequestCount();
138                    }
139    
140                    return requestCount;
141            }
142    
143            public long getRequestCountByCompany(long companyId)
144                    throws MonitoringException {
145    
146                    return getRequestStatistics(companyId).getRequestCount();
147            }
148    
149            public long getRequestCountByCompany(String webId)
150                    throws MonitoringException {
151    
152                    return getRequestStatistics(webId).getRequestCount();
153            }
154    
155            public long getSuccessCount() {
156                    int successCount = 0;
157    
158                    for (CompanyStatistics companyStatistics :
159                                    _serverStatistics.getCompanyStatisticsSet()) {
160    
161                            successCount +=
162                                    companyStatistics.getRequestStatistics().getSuccessCount();
163                    }
164    
165                    return successCount;
166            }
167    
168            public long getSuccessCountByCompany(long companyId)
169                    throws MonitoringException {
170    
171                    return getRequestStatistics(companyId).getSuccessCount();
172            }
173    
174            public long getSuccessCountByCompany(String webId)
175                    throws MonitoringException {
176    
177                    return getRequestStatistics(webId).getSuccessCount();
178            }
179    
180            public long getTimeoutCount() {
181                    int timeoutCount = 0;
182    
183                    for (CompanyStatistics companyStatistics :
184                                    _serverStatistics.getCompanyStatisticsSet()) {
185    
186                            timeoutCount +=
187                                    companyStatistics.getRequestStatistics().getTimeoutCount();
188                    }
189    
190                    return timeoutCount;
191            }
192    
193            public long getTimeoutCountByCompany(long companyId)
194                    throws MonitoringException {
195    
196                    return getRequestStatistics(companyId).getTimeoutCount();
197            }
198    
199            public long getTimeoutCountByCompany(String webId)
200                    throws MonitoringException {
201    
202                    return getRequestStatistics(webId).getTimeoutCount();
203            }
204    
205            public void setServerStatistics(ServerStatistics serverStatistics) {
206                    _serverStatistics = serverStatistics;
207            }
208    
209            protected RequestStatistics getRequestStatistics(long companyId)
210                    throws MonitoringException {
211    
212                    try {
213                            CompanyStatistics companyStatistics =
214                                    _serverStatistics.getCompanyStatistics(companyId);
215    
216                            return companyStatistics.getRequestStatistics();
217                    }
218                    catch (Exception e) {
219                            throw new MonitoringException(
220                                    "Unable to get company with company id " + companyId, e);
221                    }
222            }
223    
224            protected RequestStatistics getRequestStatistics(String webId)
225                    throws MonitoringException {
226    
227                    try {
228                            CompanyStatistics companyStatistics =
229                                    _serverStatistics.getCompanyStatistics(webId);
230    
231                            return companyStatistics.getRequestStatistics();
232                    }
233                    catch (Exception e) {
234                            throw new MonitoringException(
235                                    "Unable to get company with web id " + webId, e);
236                    }
237            }
238    
239            private ServerStatistics _serverStatistics;
240    
241    }