001    /**
002     * Copyright (c) 2000-2013 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.kernel.monitoring.MonitoringException;
018    import com.liferay.portal.kernel.monitoring.statistics.RequestStatistics;
019    import com.liferay.portal.kernel.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) throws MonitoringException {
119                    return getRequestStatistics(companyId).getMinTime();
120            }
121    
122            public long getMinTimeByCompany(String webId) throws MonitoringException {
123                    return getRequestStatistics(webId).getMinTime();
124            }
125    
126            public long getRequestCount() {
127                    int requestCount = 0;
128    
129                    for (CompanyStatistics companyStatistics :
130                                    _serverStatistics.getCompanyStatisticsSet()) {
131    
132                            requestCount +=
133                                    companyStatistics.getRequestStatistics().getRequestCount();
134                    }
135    
136                    return requestCount;
137            }
138    
139            public long getRequestCountByCompany(long companyId)
140                    throws MonitoringException {
141    
142                    return getRequestStatistics(companyId).getRequestCount();
143            }
144    
145            public long getRequestCountByCompany(String webId)
146                    throws MonitoringException {
147    
148                    return getRequestStatistics(webId).getRequestCount();
149            }
150    
151            public long getSuccessCount() {
152                    int successCount = 0;
153    
154                    for (CompanyStatistics companyStatistics :
155                                    _serverStatistics.getCompanyStatisticsSet()) {
156    
157                            successCount +=
158                                    companyStatistics.getRequestStatistics().getSuccessCount();
159                    }
160    
161                    return successCount;
162            }
163    
164            public long getSuccessCountByCompany(long companyId)
165                    throws MonitoringException {
166    
167                    return getRequestStatistics(companyId).getSuccessCount();
168            }
169    
170            public long getSuccessCountByCompany(String webId)
171                    throws MonitoringException {
172    
173                    return getRequestStatistics(webId).getSuccessCount();
174            }
175    
176            public long getTimeoutCount() {
177                    int timeoutCount = 0;
178    
179                    for (CompanyStatistics companyStatistics :
180                                    _serverStatistics.getCompanyStatisticsSet()) {
181    
182                            timeoutCount +=
183                                    companyStatistics.getRequestStatistics().getTimeoutCount();
184                    }
185    
186                    return timeoutCount;
187            }
188    
189            public long getTimeoutCountByCompany(long companyId)
190                    throws MonitoringException {
191    
192                    return getRequestStatistics(companyId).getTimeoutCount();
193            }
194    
195            public long getTimeoutCountByCompany(String webId)
196                    throws MonitoringException {
197    
198                    return getRequestStatistics(webId).getTimeoutCount();
199            }
200    
201            public void setServerStatistics(ServerStatistics serverStatistics) {
202                    _serverStatistics = serverStatistics;
203            }
204    
205            protected RequestStatistics getRequestStatistics(long companyId)
206                    throws MonitoringException {
207    
208                    try {
209                            CompanyStatistics companyStatistics =
210                                    _serverStatistics.getCompanyStatistics(companyId);
211    
212                            return companyStatistics.getRequestStatistics();
213                    }
214                    catch (Exception e) {
215                            throw new MonitoringException(
216                                    "Unable to get company with company id " + companyId, e);
217                    }
218            }
219    
220            protected RequestStatistics getRequestStatistics(String webId)
221                    throws MonitoringException {
222    
223                    try {
224                            CompanyStatistics companyStatistics =
225                                    _serverStatistics.getCompanyStatistics(webId);
226    
227                            return companyStatistics.getRequestStatistics();
228                    }
229                    catch (Exception e) {
230                            throw new MonitoringException(
231                                    "Unable to get company with web id " + webId, e);
232                    }
233            }
234    
235            private ServerStatistics _serverStatistics;
236    
237    }