001
014
015 package com.liferay.portal.monitoring.statistics.portlet;
016
017 import com.liferay.portal.kernel.monitoring.MonitoringException;
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 import java.util.Collection;
025 import java.util.HashSet;
026 import java.util.Map;
027 import java.util.Set;
028 import java.util.concurrent.ConcurrentHashMap;
029
030
035 public class CompanyStatistics
036 implements DataSampleProcessor<PortletRequestDataSample> {
037
038 public CompanyStatistics() {
039 _companyId = CompanyConstants.SYSTEM;
040 _webId = CompanyConstants.SYSTEM_STRING;
041 }
042
043 public CompanyStatistics(
044 CompanyLocalService companyLocalService, String webId) {
045
046 try {
047 Company company = companyLocalService.getCompanyByWebId(webId);
048
049 _companyId = company.getCompanyId();
050 _webId = webId;
051 }
052 catch (Exception e) {
053 throw new IllegalStateException(
054 "Unable to get company with web id " + webId, e);
055 }
056 }
057
058 public RequestStatistics getActionRequestStatistics(String portletId)
059 throws MonitoringException {
060
061 PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
062 portletId);
063
064 if (portletStatistics == null) {
065 throw new MonitoringException(
066 "No statistics for portlet id " + portletId);
067 }
068
069 return portletStatistics.getActionRequestStatistics();
070 }
071
072 public Set<RequestStatistics> getActionRequestStatisticsSet() {
073 Set<RequestStatistics> actionStatisticsSet =
074 new HashSet<RequestStatistics>();
075
076 for (PortletStatistics portletStatistics :
077 _portletStatisticsByPortletId.values()) {
078
079 actionStatisticsSet.add(
080 portletStatistics.getActionRequestStatistics());
081 }
082
083 return actionStatisticsSet;
084 }
085
086 public long getCompanyId() {
087 return _companyId;
088 }
089
090 public RequestStatistics getEventRequestStatistics(String portletId)
091 throws MonitoringException {
092
093 PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
094 portletId);
095
096 if (portletStatistics == null) {
097 throw new MonitoringException(
098 "No statistics for portlet id " + portletId);
099 }
100
101 return portletStatistics.getEventRequestStatistics();
102 }
103
104 public Set<RequestStatistics> getEventRequestStatisticsSet() {
105 Set<RequestStatistics> eventStatisticsSet =
106 new HashSet<RequestStatistics>();
107
108 for (PortletStatistics portletStatistics :
109 _portletStatisticsByPortletId.values()) {
110
111 eventStatisticsSet.add(
112 portletStatistics.getEventRequestStatistics());
113 }
114
115 return eventStatisticsSet;
116 }
117
118 public long getMaxTime() {
119 return _maxTime;
120 }
121
122 public long getMinTime() {
123 return _minTime;
124 }
125
126 public Collection<String> getPortletIds() {
127 return _portletStatisticsByPortletId.keySet();
128 }
129
130 public RequestStatistics getRenderRequestStatistics(String portletId)
131 throws MonitoringException {
132
133 PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
134 portletId);
135
136 if (portletStatistics == null) {
137 throw new MonitoringException(
138 "No statistics for portlet id " + portletId);
139 }
140
141 return portletStatistics.getRenderRequestStatistics();
142 }
143
144 public Set<RequestStatistics> getRenderRequestStatisticsSet() {
145 Set<RequestStatistics> renderStatisticsSet =
146 new HashSet<RequestStatistics>();
147
148 for (PortletStatistics portletStatistics :
149 _portletStatisticsByPortletId.values()) {
150
151 renderStatisticsSet.add(
152 portletStatistics.getRenderRequestStatistics());
153 }
154
155 return renderStatisticsSet;
156 }
157
158 public RequestStatistics getResourceRequestStatistics(String portletId)
159 throws MonitoringException {
160
161 PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
162 portletId);
163
164 if (portletStatistics == null) {
165 throw new MonitoringException(
166 "No statistics for portlet id " + portletId);
167 }
168
169 return portletStatistics.getResourceRequestStatistics();
170 }
171
172 public Set<RequestStatistics> getResourceRequestStatisticsSet() {
173 Set<RequestStatistics> resourceStatisticsSet =
174 new HashSet<RequestStatistics>();
175
176 for (PortletStatistics portletStatistics :
177 _portletStatisticsByPortletId.values()) {
178
179 resourceStatisticsSet.add(
180 portletStatistics.getResourceRequestStatistics());
181 }
182
183 return resourceStatisticsSet;
184 }
185
186 public String getWebId() {
187 return _webId;
188 }
189
190 @Override
191 public void processDataSample(
192 PortletRequestDataSample portletRequestDataSample)
193 throws MonitoringException {
194
195 if (portletRequestDataSample.getCompanyId() != _companyId) {
196 return;
197 }
198
199 String portletId = portletRequestDataSample.getPortletId();
200
201 PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
202 portletId);
203
204 if (portletStatistics == null) {
205 portletStatistics = new PortletStatistics(
206 portletId, portletRequestDataSample.getName(),
207 portletRequestDataSample.getDisplayName());
208
209 _portletStatisticsByPortletId.put(portletId, portletStatistics);
210 }
211
212 portletStatistics.processDataSample(portletRequestDataSample);
213
214 long duration = portletRequestDataSample.getDuration();
215
216 if (_maxTime < duration) {
217 _maxTime = duration;
218 }
219 else if (_minTime > duration) {
220 _minTime = duration;
221 }
222 }
223
224 public void reset() {
225 _maxTime = 0;
226 _minTime = 0;
227
228 for (PortletStatistics portletStatistics :
229 _portletStatisticsByPortletId.values()) {
230
231 portletStatistics.reset();
232 }
233 }
234
235 private long _companyId;
236 private long _maxTime;
237 private long _minTime;
238 private Map<String, PortletStatistics> _portletStatisticsByPortletId =
239 new ConcurrentHashMap<String, PortletStatistics>();
240 private String _webId;
241
242 }