1
22
23
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
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 }