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            @Override
030            public long getAverageTime() {
031                    long averageTime = 0;
032    
033                    Set<CompanyStatistics> companyStatisticsSet =
034                            _serverStatistics.getCompanyStatisticsSet();
035    
036                    for (CompanyStatistics companyStatistics : companyStatisticsSet) {
037                            RequestStatistics requestStatistics =
038                                    companyStatistics.getRequestStatistics();
039    
040                            averageTime += requestStatistics.getAverageTime();
041                    }
042    
043                    return averageTime / companyStatisticsSet.size();
044            }
045    
046            @Override
047            public long getAverageTimeByCompany(long companyId)
048                    throws MonitoringException {
049    
050                    return getRequestStatistics(companyId).getAverageTime();
051            }
052    
053            @Override
054            public long getAverageTimeByCompany(String webId)
055                    throws MonitoringException {
056    
057                    return getRequestStatistics(webId).getAverageTime();
058            }
059    
060            @Override
061            public long getErrorCount() {
062                    int errorCount = 0;
063    
064                    for (CompanyStatistics companyStatistics :
065                                    _serverStatistics.getCompanyStatisticsSet()) {
066    
067                            errorCount +=
068                                    companyStatistics.getRequestStatistics().getErrorCount();
069                    }
070    
071                    return errorCount;
072            }
073    
074            @Override
075            public long getErrorCountByCompany(long companyId)
076                    throws MonitoringException {
077    
078                    return getRequestStatistics(companyId).getErrorCount();
079            }
080    
081            @Override
082            public long getErrorCountByCompany(String webId)
083                    throws MonitoringException {
084    
085                    return getRequestStatistics(webId).getErrorCount();
086            }
087    
088            @Override
089            public long getMaxTime() {
090                    long maxTime = 0;
091    
092                    for (CompanyStatistics companyStatistics :
093                                    _serverStatistics.getCompanyStatisticsSet()) {
094    
095                            if (companyStatistics.getMaxTime() > maxTime) {
096                                    maxTime = companyStatistics.getMaxTime();
097                            }
098                    }
099    
100                    return maxTime;
101            }
102    
103            @Override
104            public long getMaxTimeByCompany(long companyId) throws MonitoringException {
105                    return getRequestStatistics(companyId).getMaxTime();
106            }
107    
108            @Override
109            public long getMaxTimeByCompany(String webId) throws MonitoringException {
110                    return getRequestStatistics(webId).getMaxTime();
111            }
112    
113            @Override
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            @Override
129            public long getMinTimeByCompany(long companyId) throws MonitoringException {
130                    return getRequestStatistics(companyId).getMinTime();
131            }
132    
133            @Override
134            public long getMinTimeByCompany(String webId) throws MonitoringException {
135                    return getRequestStatistics(webId).getMinTime();
136            }
137    
138            @Override
139            public long getRequestCount() {
140                    int requestCount = 0;
141    
142                    for (CompanyStatistics companyStatistics :
143                                    _serverStatistics.getCompanyStatisticsSet()) {
144    
145                            requestCount +=
146                                    companyStatistics.getRequestStatistics().getRequestCount();
147                    }
148    
149                    return requestCount;
150            }
151    
152            @Override
153            public long getRequestCountByCompany(long companyId)
154                    throws MonitoringException {
155    
156                    return getRequestStatistics(companyId).getRequestCount();
157            }
158    
159            @Override
160            public long getRequestCountByCompany(String webId)
161                    throws MonitoringException {
162    
163                    return getRequestStatistics(webId).getRequestCount();
164            }
165    
166            @Override
167            public long getSuccessCount() {
168                    int successCount = 0;
169    
170                    for (CompanyStatistics companyStatistics :
171                                    _serverStatistics.getCompanyStatisticsSet()) {
172    
173                            successCount +=
174                                    companyStatistics.getRequestStatistics().getSuccessCount();
175                    }
176    
177                    return successCount;
178            }
179    
180            @Override
181            public long getSuccessCountByCompany(long companyId)
182                    throws MonitoringException {
183    
184                    return getRequestStatistics(companyId).getSuccessCount();
185            }
186    
187            @Override
188            public long getSuccessCountByCompany(String webId)
189                    throws MonitoringException {
190    
191                    return getRequestStatistics(webId).getSuccessCount();
192            }
193    
194            @Override
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            @Override
209            public long getTimeoutCountByCompany(long companyId)
210                    throws MonitoringException {
211    
212                    return getRequestStatistics(companyId).getTimeoutCount();
213            }
214    
215            @Override
216            public long getTimeoutCountByCompany(String webId)
217                    throws MonitoringException {
218    
219                    return getRequestStatistics(webId).getTimeoutCount();
220            }
221    
222            public void setServerStatistics(ServerStatistics serverStatistics) {
223                    _serverStatistics = serverStatistics;
224            }
225    
226            protected RequestStatistics getRequestStatistics(long companyId)
227                    throws MonitoringException {
228    
229                    try {
230                            CompanyStatistics companyStatistics =
231                                    _serverStatistics.getCompanyStatistics(companyId);
232    
233                            return companyStatistics.getRequestStatistics();
234                    }
235                    catch (Exception e) {
236                            throw new MonitoringException(
237                                    "Unable to get company with company id " + companyId, e);
238                    }
239            }
240    
241            protected RequestStatistics getRequestStatistics(String webId)
242                    throws MonitoringException {
243    
244                    try {
245                            CompanyStatistics companyStatistics =
246                                    _serverStatistics.getCompanyStatistics(webId);
247    
248                            return companyStatistics.getRequestStatistics();
249                    }
250                    catch (Exception e) {
251                            throw new MonitoringException(
252                                    "Unable to get company with web id " + webId, e);
253                    }
254            }
255    
256            private ServerStatistics _serverStatistics;
257    
258    }