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.RequestStatus;
018    import com.liferay.portal.kernel.monitoring.statistics.DataSampleProcessor;
019    import com.liferay.portal.kernel.monitoring.statistics.RequestStatistics;
020    import com.liferay.portal.model.Company;
021    import com.liferay.portal.model.CompanyConstants;
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            @Override
083            public void processDataSample(
084                    PortalRequestDataSample portalRequestDataSample) {
085    
086                    if (portalRequestDataSample.getCompanyId() != _companyId) {
087                            return;
088                    }
089    
090                    RequestStatus requestStatus =
091                            portalRequestDataSample.getRequestStatus();
092    
093                    if (requestStatus.equals(RequestStatus.ERROR)) {
094                            _requestStatistics.incrementError();
095                    }
096                    else if (requestStatus.equals(RequestStatus.SUCCESS)) {
097                            _requestStatistics.incrementSuccessDuration(
098                                    portalRequestDataSample.getDuration());
099                    }
100                    else if (requestStatus.equals(RequestStatus.TIMEOUT)) {
101                            _requestStatistics.incrementTimeout();
102                    }
103    
104                    long duration = portalRequestDataSample.getDuration();
105    
106                    if (_maxTime < duration) {
107                            _maxTime = duration;
108                    }
109                    else if (_minTime > duration) {
110                            _minTime = duration;
111                    }
112            }
113    
114            public void reset() {
115                    _maxTime = 0;
116                    _minTime = 0;
117    
118                    _requestStatistics.reset();
119            }
120    
121            private long _companyId;
122            private long _maxTime;
123            private long _minTime;
124            private RequestStatistics _requestStatistics;
125            private long _startTime = System.currentTimeMillis();
126            private String _webId;
127    
128    }