1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.monitoring.statistics.portal;
16  
17  import com.liferay.portal.model.Company;
18  import com.liferay.portal.model.CompanyConstants;
19  import com.liferay.portal.monitoring.RequestStatus;
20  import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
21  import com.liferay.portal.monitoring.statistics.RequestStatistics;
22  import com.liferay.portal.service.CompanyLocalService;
23  
24  /**
25   * <a href="CompanyStatistics.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Rajesh Thiagarajan
28   * @author Michael C. Han
29   * @author Brian Wing Shun Chan
30   */
31  public class CompanyStatistics
32      implements DataSampleProcessor<PortalRequestDataSample> {
33  
34      public CompanyStatistics() {
35          _companyId = CompanyConstants.SYSTEM;
36          _webId = CompanyConstants.SYSTEM_STRING;
37          _requestStatistics = new RequestStatistics(_webId);
38      }
39  
40      public CompanyStatistics(
41          CompanyLocalService companyLocalService, String webId) {
42  
43          try {
44              Company company = companyLocalService.getCompanyByWebId(webId);
45  
46              _companyId = company.getCompanyId();
47              _webId = webId;
48              _requestStatistics = new RequestStatistics(_webId);
49          }
50          catch (Exception e) {
51              throw new IllegalStateException(
52                  "Unable to get company with web id " + webId, e);
53          }
54      }
55  
56      public long getCompanyId() {
57          return _companyId;
58      }
59  
60      public long getMaxTime() {
61          return _maxTime;
62      }
63  
64      public long getMinTime() {
65          return _minTime;
66      }
67  
68      public RequestStatistics getRequestStatistics() {
69          return _requestStatistics;
70      }
71  
72      public long getStartTime() {
73          return _startTime;
74      }
75  
76      public long getUptime() {
77          return System.currentTimeMillis() - _startTime;
78      }
79  
80      public String getWebId() {
81          return _webId;
82      }
83  
84      public void processDataSample(
85          PortalRequestDataSample portalRequestDataSample) {
86  
87          if (portalRequestDataSample.getCompanyId() != _companyId) {
88              return;
89          }
90  
91          RequestStatus requestStatus =
92              portalRequestDataSample.getRequestStatus();
93  
94          if (requestStatus.equals(RequestStatus.ERROR)) {
95              _requestStatistics.incrementError();
96          }
97          else if (requestStatus.equals(RequestStatus.SUCCESS)) {
98              _requestStatistics.incrementSuccessDuration(
99                  portalRequestDataSample.getDuration());
100         }
101         else if (requestStatus.equals(RequestStatus.TIMEOUT)) {
102             _requestStatistics.incrementTimeout();
103         }
104 
105         long duration = portalRequestDataSample.getDuration();
106 
107         if (_maxTime < duration) {
108             _maxTime = duration;
109         }
110         else if (_minTime > duration) {
111             _minTime = duration;
112         }
113     }
114 
115     public void reset() {
116         _maxTime = 0;
117         _minTime = 0;
118 
119         _requestStatistics.reset();
120     }
121 
122     private long _companyId;
123     private long _maxTime;
124     private long _minTime;
125     private RequestStatistics _requestStatistics;
126     private long _startTime = System.currentTimeMillis();
127     private String _webId;
128 
129 }