1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  /**
24   * The contents of this file are subject to the terms of the Common Development
25   * and Distribution License (the License). You may not use this file except in
26   * compliance with the License.
27   *
28   * You can obtain a copy of the License at http://www.sun.com/cddl/cddl.html and
29   * legal/CDDLv1.0.txt. See the License for the specific language governing
30   * permission and limitations under the License.
31   *
32   * When distributing Covered Code, include this CDDL Header Notice in each file
33   * and include the License file at legal/CDDLv1.0.txt.
34   *
35   * If applicable, add the following below the CDDL Header, with the fields
36   * enclosed by brackets [] replaced by your own identifying information:
37   * "Portions Copyrighted [year] [name of copyright owner]"
38   *
39   * Copyright 2009 Sun Microsystems Inc. All rights reserved.
40   */
41  
42  package com.liferay.portal.monitoring.statistics.portlet;
43  
44  import com.liferay.portal.model.Company;
45  import com.liferay.portal.model.CompanyConstants;
46  import com.liferay.portal.monitoring.MonitoringException;
47  import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
48  import com.liferay.portal.monitoring.statistics.RequestStatistics;
49  import com.liferay.portal.service.CompanyLocalService;
50  
51  import java.util.Collection;
52  import java.util.HashSet;
53  import java.util.Map;
54  import java.util.Set;
55  import java.util.concurrent.ConcurrentHashMap;
56  
57  /**
58   * <a href="CompanyStatistics.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Karthik Sudarshan
61   * @author Michael C. Han
62   * @author Brian Wing Shun Chan
63   */
64  public class CompanyStatistics
65      implements DataSampleProcessor<PortletRequestDataSample> {
66  
67      public CompanyStatistics() {
68          _companyId = CompanyConstants.SYSTEM;
69          _webId = CompanyConstants.SYSTEM_STRING;
70      }
71  
72      public CompanyStatistics(
73          CompanyLocalService companyLocalService, String webId) {
74  
75          try {
76              Company company = companyLocalService.getCompanyByWebId(webId);
77  
78              _companyId = company.getCompanyId();
79              _webId = webId;
80          }
81          catch (Exception e) {
82              throw new IllegalStateException(
83                  "Unable to get company with web id " + webId, e);
84          }
85      }
86  
87      public RequestStatistics getActionRequestStatistics(String portletId)
88          throws MonitoringException {
89  
90          PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
91              portletId);
92  
93          if (portletStatistics == null) {
94              throw new MonitoringException(
95                  "No statistics for portlet id " + portletId);
96          }
97  
98          return portletStatistics.getActionRequestStatistics();
99      }
100 
101     public Set<RequestStatistics> getActionRequestStatisticsSet() {
102         Set<RequestStatistics> actionStatisticsSet =
103             new HashSet<RequestStatistics>();
104 
105         for (PortletStatistics portletStatistics :
106                 _portletStatisticsByPortletId.values()) {
107 
108             actionStatisticsSet.add(
109                 portletStatistics.getActionRequestStatistics());
110         }
111 
112         return actionStatisticsSet;
113     }
114 
115     public long getCompanyId() {
116         return _companyId;
117     }
118 
119     public RequestStatistics getEventRequestStatistics(String portletId)
120         throws MonitoringException {
121 
122         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
123             portletId);
124 
125         if (portletStatistics == null) {
126             throw new MonitoringException(
127                 "No statistics for portlet id " + portletId);
128         }
129 
130         return portletStatistics.getEventRequestStatistics();
131     }
132 
133     public Set<RequestStatistics> getEventRequestStatisticsSet() {
134         Set<RequestStatistics> eventStatisticsSet =
135             new HashSet<RequestStatistics>();
136 
137         for (PortletStatistics portletStatistics :
138                 _portletStatisticsByPortletId.values()) {
139 
140             eventStatisticsSet.add(
141                 portletStatistics.getEventRequestStatistics());
142         }
143 
144         return eventStatisticsSet;
145     }
146 
147     public long getMaxTime() {
148         return _maxTime;
149     }
150 
151     public long getMinTime() {
152         return _minTime;
153     }
154 
155     public Collection<String> getPortletIds() {
156         return _portletStatisticsByPortletId.keySet();
157     }
158 
159     public RequestStatistics getRenderRequestStatistics(String portletId)
160         throws MonitoringException {
161 
162         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
163             portletId);
164 
165         if (portletStatistics == null) {
166             throw new MonitoringException(
167                 "No statistics for portlet id " + portletId);
168         }
169 
170         return portletStatistics.getRenderRequestStatistics();
171     }
172 
173     public Set<RequestStatistics> getRenderRequestStatisticsSet() {
174         Set<RequestStatistics> renderStatisticsSet =
175             new HashSet<RequestStatistics>();
176 
177         for (PortletStatistics portletStatistics :
178                 _portletStatisticsByPortletId.values()) {
179 
180             renderStatisticsSet.add(
181                 portletStatistics.getRenderRequestStatistics());
182         }
183 
184         return renderStatisticsSet;
185     }
186 
187     public RequestStatistics getResourceRequestStatistics(String portletId)
188         throws MonitoringException {
189 
190         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
191             portletId);
192 
193         if (portletStatistics == null) {
194             throw new MonitoringException(
195                 "No statistics for portlet id " + portletId);
196         }
197 
198         return portletStatistics.getResourceRequestStatistics();
199     }
200 
201     public Set<RequestStatistics> getResourceRequestStatisticsSet() {
202         Set<RequestStatistics> resourceStatisticsSet =
203             new HashSet<RequestStatistics>();
204 
205         for (PortletStatistics portletStatistics :
206                 _portletStatisticsByPortletId.values()) {
207 
208             resourceStatisticsSet.add(
209                 portletStatistics.getResourceRequestStatistics());
210         }
211 
212         return resourceStatisticsSet;
213     }
214 
215     public String getWebId() {
216         return _webId;
217     }
218 
219     public void processDataSample(
220             PortletRequestDataSample portletRequestDataSample)
221         throws MonitoringException {
222 
223         if (portletRequestDataSample.getCompanyId() != _companyId) {
224             return;
225         }
226 
227         String portletId = portletRequestDataSample.getPortletId();
228 
229         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
230             portletId);
231 
232         if (portletStatistics == null) {
233             portletStatistics = new PortletStatistics(
234                 portletId, portletRequestDataSample.getName(),
235                 portletRequestDataSample.getDisplayName());
236 
237             _portletStatisticsByPortletId.put(portletId, portletStatistics);
238         }
239 
240         portletStatistics.processDataSample(portletRequestDataSample);
241 
242         long duration = portletRequestDataSample.getDuration();
243 
244         if (_maxTime < duration) {
245             _maxTime = duration;
246         }
247         else if (_minTime > duration) {
248             _minTime = duration;
249         }
250     }
251 
252     public void reset() {
253         _maxTime = 0;
254         _minTime = 0;
255 
256         for (PortletStatistics portletStatistics :
257                 _portletStatisticsByPortletId.values()) {
258 
259             portletStatistics.reset();
260         }
261     }
262 
263     private long _companyId;
264     private long _maxTime;
265     private long _minTime;
266     private Map<String, PortletStatistics> _portletStatisticsByPortletId =
267         new ConcurrentHashMap<String, PortletStatistics>();
268     private String _webId;
269 
270 }