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
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 }