001
014
015 package com.liferay.portal.monitoring.statistics.portlet;
016
017 import com.liferay.portal.model.Company;
018 import com.liferay.portal.model.CompanyConstants;
019 import com.liferay.portal.monitoring.MonitoringException;
020 import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
021 import com.liferay.portal.monitoring.statistics.RequestStatistics;
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 public void processDataSample(
191 PortletRequestDataSample portletRequestDataSample)
192 throws MonitoringException {
193
194 if (portletRequestDataSample.getCompanyId() != _companyId) {
195 return;
196 }
197
198 String portletId = portletRequestDataSample.getPortletId();
199
200 PortletStatistics portletStatistics = _portletStatisticsByPortletId.get(
201 portletId);
202
203 if (portletStatistics == null) {
204 portletStatistics = new PortletStatistics(
205 portletId, portletRequestDataSample.getName(),
206 portletRequestDataSample.getDisplayName());
207
208 _portletStatisticsByPortletId.put(portletId, portletStatistics);
209 }
210
211 portletStatistics.processDataSample(portletRequestDataSample);
212
213 long duration = portletRequestDataSample.getDuration();
214
215 if (_maxTime < duration) {
216 _maxTime = duration;
217 }
218 else if (_minTime > duration) {
219 _minTime = duration;
220 }
221 }
222
223 public void reset() {
224 _maxTime = 0;
225 _minTime = 0;
226
227 for (PortletStatistics portletStatistics :
228 _portletStatisticsByPortletId.values()) {
229
230 portletStatistics.reset();
231 }
232 }
233
234 private long _companyId;
235 private long _maxTime;
236 private long _minTime;
237 private Map<String, PortletStatistics> _portletStatisticsByPortletId =
238 new ConcurrentHashMap<String, PortletStatistics>();
239 private String _webId;
240
241 }