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   */
65  public class CompanyStatistics
66      implements DataSampleProcessor<PortletRequestDataSample> {
67  
68      public CompanyStatistics() {
69          _companyId = CompanyConstants.SYSTEM;
70          _webId = CompanyConstants.SYSTEM_STRING;
71      }
72  
73      public CompanyStatistics(
74          CompanyLocalService companyLocalService, String webId) {
75  
76          try {
77              Company company = companyLocalService.getCompanyByWebId(webId);
78  
79              _companyId = company.getCompanyId();
80              _webId = webId;
81          }
82          catch (Exception e) {
83              throw new IllegalStateException(
84                  "Unable to get company with web id " + webId, e);
85          }
86      }
87  
88      public RequestStatistics getActionRequestStatistics(String portletId)
89          throws MonitoringException {
90  
91          PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
92              portletId);
93  
94          if (portletStatistics == null) {
95              throw new MonitoringException(
96                  "No statistics for portlet id " + portletId);
97          }
98  
99          return portletStatistics.getActionRequestStatistics();
100     }
101 
102     public Set<RequestStatistics> getActionRequestStatisticsSet() {
103         Set<RequestStatistics> actionStatisticsSet =
104             new HashSet<RequestStatistics>();
105 
106         for (PortletStatistics portletStatistics :
107                 _portletStatisticsByPortletId.values()) {
108 
109             actionStatisticsSet.add(
110                 portletStatistics.getActionRequestStatistics());
111         }
112 
113         return actionStatisticsSet;
114     }
115 
116     public long getCompanyId() {
117         return _companyId;
118     }
119 
120     public RequestStatistics getEventRequestStatistics(String portletId)
121         throws MonitoringException {
122 
123         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
124             portletId);
125 
126         if (portletStatistics == null) {
127             throw new MonitoringException(
128                 "No statistics for portlet id " + portletId);
129         }
130 
131         return portletStatistics.getEventRequestStatistics();
132     }
133 
134     public Set<RequestStatistics> getEventRequestStatisticsSet() {
135         Set<RequestStatistics> eventStatisticsSet =
136             new HashSet<RequestStatistics>();
137 
138         for (PortletStatistics portletStatistics :
139                 _portletStatisticsByPortletId.values()) {
140 
141             eventStatisticsSet.add(
142                 portletStatistics.getEventRequestStatistics());
143         }
144 
145         return eventStatisticsSet;
146     }
147 
148     public long getMaxTime() {
149         return _maxTime;
150     }
151 
152     public long getMinTime() {
153         return _minTime;
154     }
155 
156     public Collection<String> getPortletIds() {
157         return _portletStatisticsByPortletId.keySet();
158     }
159 
160     public RequestStatistics getRenderRequestStatistics(String portletId)
161         throws MonitoringException {
162 
163         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
164             portletId);
165 
166         if (portletStatistics == null) {
167             throw new MonitoringException(
168                 "No statistics for portlet id " + portletId);
169         }
170 
171         return portletStatistics.getRenderRequestStatistics();
172     }
173 
174     public Set<RequestStatistics> getRenderRequestStatisticsSet() {
175         Set<RequestStatistics> renderStatisticsSet =
176             new HashSet<RequestStatistics>();
177 
178         for (PortletStatistics portletStatistics :
179                 _portletStatisticsByPortletId.values()) {
180 
181             renderStatisticsSet.add(
182                 portletStatistics.getRenderRequestStatistics());
183         }
184 
185         return renderStatisticsSet;
186     }
187 
188     public RequestStatistics getResourceRequestStatistics(String portletId)
189         throws MonitoringException {
190 
191         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
192             portletId);
193 
194         if (portletStatistics == null) {
195             throw new MonitoringException(
196                 "No statistics for portlet id " + portletId);
197         }
198 
199         return portletStatistics.getResourceRequestStatistics();
200     }
201 
202     public Set<RequestStatistics> getResourceRequestStatisticsSet() {
203         Set<RequestStatistics> resourceStatisticsSet =
204             new HashSet<RequestStatistics>();
205 
206         for (PortletStatistics portletStatistics :
207                 _portletStatisticsByPortletId.values()) {
208 
209             resourceStatisticsSet.add(
210                 portletStatistics.getResourceRequestStatistics());
211         }
212 
213         return resourceStatisticsSet;
214     }
215 
216     public String getWebId() {
217         return _webId;
218     }
219 
220     public void processDataSample(
221             PortletRequestDataSample portletRequestDataSample)
222         throws MonitoringException {
223 
224         if (portletRequestDataSample.getCompanyId() != _companyId) {
225             return;
226         }
227 
228         String portletId = portletRequestDataSample.getPortletId();
229 
230         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
231             portletId);
232 
233         if (portletStatistics == null) {
234             portletStatistics = new PortletStatistics(
235                 portletId, portletRequestDataSample.getName(),
236                 portletRequestDataSample.getDisplayName());
237 
238             _portletStatisticsByPortletId.put(portletId, portletStatistics);
239         }
240 
241         portletStatistics.processDataSample(portletRequestDataSample);
242 
243         long duration = portletRequestDataSample.getDuration();
244 
245         if (_maxTime < duration) {
246             _maxTime = duration;
247         }
248         else if (_minTime > duration) {
249             _minTime = duration;
250         }
251     }
252 
253     public void reset() {
254         _maxTime = 0;
255         _minTime = 0;
256 
257         for (PortletStatistics portletStatistics :
258                 _portletStatisticsByPortletId.values()) {
259 
260             portletStatistics.reset();
261         }
262     }
263 
264     private long _companyId;
265     private long _maxTime;
266     private long _minTime;
267     private Map<String, PortletStatistics> _portletStatisticsByPortletId =
268         new ConcurrentHashMap<String, PortletStatistics>();
269     private String _webId;
270 
271 }