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.portlet;
16  
17  import com.liferay.portal.model.Company;
18  import com.liferay.portal.model.CompanyConstants;
19  import com.liferay.portal.monitoring.MonitoringException;
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  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  /**
31   * <a href="CompanyStatistics.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Karthik Sudarshan
34   * @author Michael C. Han
35   * @author Brian Wing Shun Chan
36   */
37  public class CompanyStatistics
38      implements DataSampleProcessor<PortletRequestDataSample> {
39  
40      public CompanyStatistics() {
41          _companyId = CompanyConstants.SYSTEM;
42          _webId = CompanyConstants.SYSTEM_STRING;
43      }
44  
45      public CompanyStatistics(
46          CompanyLocalService companyLocalService, String webId) {
47  
48          try {
49              Company company = companyLocalService.getCompanyByWebId(webId);
50  
51              _companyId = company.getCompanyId();
52              _webId = webId;
53          }
54          catch (Exception e) {
55              throw new IllegalStateException(
56                  "Unable to get company with web id " + webId, e);
57          }
58      }
59  
60      public RequestStatistics getActionRequestStatistics(String portletId)
61          throws MonitoringException {
62  
63          PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
64              portletId);
65  
66          if (portletStatistics == null) {
67              throw new MonitoringException(
68                  "No statistics for portlet id " + portletId);
69          }
70  
71          return portletStatistics.getActionRequestStatistics();
72      }
73  
74      public Set<RequestStatistics> getActionRequestStatisticsSet() {
75          Set<RequestStatistics> actionStatisticsSet =
76              new HashSet<RequestStatistics>();
77  
78          for (PortletStatistics portletStatistics :
79                  _portletStatisticsByPortletId.values()) {
80  
81              actionStatisticsSet.add(
82                  portletStatistics.getActionRequestStatistics());
83          }
84  
85          return actionStatisticsSet;
86      }
87  
88      public long getCompanyId() {
89          return _companyId;
90      }
91  
92      public RequestStatistics getEventRequestStatistics(String portletId)
93          throws MonitoringException {
94  
95          PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
96              portletId);
97  
98          if (portletStatistics == null) {
99              throw new MonitoringException(
100                 "No statistics for portlet id " + portletId);
101         }
102 
103         return portletStatistics.getEventRequestStatistics();
104     }
105 
106     public Set<RequestStatistics> getEventRequestStatisticsSet() {
107         Set<RequestStatistics> eventStatisticsSet =
108             new HashSet<RequestStatistics>();
109 
110         for (PortletStatistics portletStatistics :
111                 _portletStatisticsByPortletId.values()) {
112 
113             eventStatisticsSet.add(
114                 portletStatistics.getEventRequestStatistics());
115         }
116 
117         return eventStatisticsSet;
118     }
119 
120     public long getMaxTime() {
121         return _maxTime;
122     }
123 
124     public long getMinTime() {
125         return _minTime;
126     }
127 
128     public Collection<String> getPortletIds() {
129         return _portletStatisticsByPortletId.keySet();
130     }
131 
132     public RequestStatistics getRenderRequestStatistics(String portletId)
133         throws MonitoringException {
134 
135         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
136             portletId);
137 
138         if (portletStatistics == null) {
139             throw new MonitoringException(
140                 "No statistics for portlet id " + portletId);
141         }
142 
143         return portletStatistics.getRenderRequestStatistics();
144     }
145 
146     public Set<RequestStatistics> getRenderRequestStatisticsSet() {
147         Set<RequestStatistics> renderStatisticsSet =
148             new HashSet<RequestStatistics>();
149 
150         for (PortletStatistics portletStatistics :
151                 _portletStatisticsByPortletId.values()) {
152 
153             renderStatisticsSet.add(
154                 portletStatistics.getRenderRequestStatistics());
155         }
156 
157         return renderStatisticsSet;
158     }
159 
160     public RequestStatistics getResourceRequestStatistics(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.getResourceRequestStatistics();
172     }
173 
174     public Set<RequestStatistics> getResourceRequestStatisticsSet() {
175         Set<RequestStatistics> resourceStatisticsSet =
176             new HashSet<RequestStatistics>();
177 
178         for (PortletStatistics portletStatistics :
179                 _portletStatisticsByPortletId.values()) {
180 
181             resourceStatisticsSet.add(
182                 portletStatistics.getResourceRequestStatistics());
183         }
184 
185         return resourceStatisticsSet;
186     }
187 
188     public String getWebId() {
189         return _webId;
190     }
191 
192     public void processDataSample(
193             PortletRequestDataSample portletRequestDataSample)
194         throws MonitoringException {
195 
196         if (portletRequestDataSample.getCompanyId() != _companyId) {
197             return;
198         }
199 
200         String portletId = portletRequestDataSample.getPortletId();
201 
202         PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
203             portletId);
204 
205         if (portletStatistics == null) {
206             portletStatistics = new PortletStatistics(
207                 portletId, portletRequestDataSample.getName(),
208                 portletRequestDataSample.getDisplayName());
209 
210             _portletStatisticsByPortletId.put(portletId, portletStatistics);
211         }
212 
213         portletStatistics.processDataSample(portletRequestDataSample);
214 
215         long duration = portletRequestDataSample.getDuration();
216 
217         if (_maxTime < duration) {
218             _maxTime = duration;
219         }
220         else if (_minTime > duration) {
221             _minTime = duration;
222         }
223     }
224 
225     public void reset() {
226         _maxTime = 0;
227         _minTime = 0;
228 
229         for (PortletStatistics portletStatistics :
230                 _portletStatisticsByPortletId.values()) {
231 
232             portletStatistics.reset();
233         }
234     }
235 
236     private long _companyId;
237     private long _maxTime;
238     private long _minTime;
239     private Map<String, PortletStatistics> _portletStatisticsByPortletId =
240         new ConcurrentHashMap<String, PortletStatistics>();
241     private String _webId;
242 
243 }