001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.monitoring.statistics.portal;
016    
017    import com.liferay.portal.model.Company;
018    import com.liferay.portal.model.CompanyConstants;
019    import com.liferay.portal.monitoring.RequestStatus;
020    import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
021    import com.liferay.portal.monitoring.statistics.RequestStatistics;
022    import com.liferay.portal.service.CompanyLocalService;
023    
024    /**
025     * @author Rajesh Thiagarajan
026     * @author Michael C. Han
027     * @author Brian Wing Shun Chan
028     */
029    public class CompanyStatistics
030            implements DataSampleProcessor<PortalRequestDataSample> {
031    
032            public CompanyStatistics() {
033                    _companyId = CompanyConstants.SYSTEM;
034                    _webId = CompanyConstants.SYSTEM_STRING;
035                    _requestStatistics = new RequestStatistics(_webId);
036            }
037    
038            public CompanyStatistics(
039                    CompanyLocalService companyLocalService, String webId) {
040    
041                    try {
042                            Company company = companyLocalService.getCompanyByWebId(webId);
043    
044                            _companyId = company.getCompanyId();
045                            _webId = webId;
046                            _requestStatistics = new RequestStatistics(_webId);
047                    }
048                    catch (Exception e) {
049                            throw new IllegalStateException(
050                                    "Unable to get company with web id " + webId, e);
051                    }
052            }
053    
054            public long getCompanyId() {
055                    return _companyId;
056            }
057    
058            public long getMaxTime() {
059                    return _maxTime;
060            }
061    
062            public long getMinTime() {
063                    return _minTime;
064            }
065    
066            public RequestStatistics getRequestStatistics() {
067                    return _requestStatistics;
068            }
069    
070            public long getStartTime() {
071                    return _startTime;
072            }
073    
074            public long getUptime() {
075                    return System.currentTimeMillis() - _startTime;
076            }
077    
078            public String getWebId() {
079                    return _webId;
080            }
081    
082            public void processDataSample(
083                    PortalRequestDataSample portalRequestDataSample) {
084    
085                    if (portalRequestDataSample.getCompanyId() != _companyId) {
086                            return;
087                    }
088    
089                    RequestStatus requestStatus =
090                            portalRequestDataSample.getRequestStatus();
091    
092                    if (requestStatus.equals(RequestStatus.ERROR)) {
093                            _requestStatistics.incrementError();
094                    }
095                    else if (requestStatus.equals(RequestStatus.SUCCESS)) {
096                            _requestStatistics.incrementSuccessDuration(
097                                    portalRequestDataSample.getDuration());
098                    }
099                    else if (requestStatus.equals(RequestStatus.TIMEOUT)) {
100                            _requestStatistics.incrementTimeout();
101                    }
102    
103                    long duration = portalRequestDataSample.getDuration();
104    
105                    if (_maxTime < duration) {
106                            _maxTime = duration;
107                    }
108                    else if (_minTime > duration) {
109                            _minTime = duration;
110                    }
111            }
112    
113            public void reset() {
114                    _maxTime = 0;
115                    _minTime = 0;
116    
117                    _requestStatistics.reset();
118            }
119    
120            private long _companyId;
121            private long _maxTime;
122            private long _minTime;
123            private RequestStatistics _requestStatistics;
124            private long _startTime = System.currentTimeMillis();
125            private String _webId;
126    
127    }