1
22
23 package com.liferay.portal.monitoring.statistics.portal;
24
25 import com.liferay.portal.model.Company;
26 import com.liferay.portal.monitoring.MonitoringException;
27 import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
28 import com.liferay.portal.service.CompanyLocalService;
29
30 import java.util.HashSet;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.TreeMap;
34
35
42 public class ServerStatistics
43 implements DataSampleProcessor<PortalRequestDataSample> {
44
45 public ServerStatistics(CompanyLocalService companyLocalService) {
46 _companyLocalService = companyLocalService;
47
48 CompanyStatistics systemCompanyStatistics = new CompanyStatistics();
49
50 _companyStatisticsByCompanyId.put(
51 systemCompanyStatistics.getCompanyId(), systemCompanyStatistics);
52 _companyStatisticsByWebId.put(
53 systemCompanyStatistics.getWebId(), systemCompanyStatistics);
54 }
55
56 public Set<Long> getCompanyIds() {
57 return _companyStatisticsByCompanyId.keySet();
58 }
59
60 public CompanyStatistics getCompanyStatistics(long companyId)
61 throws MonitoringException {
62
63 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
64 companyId);
65
66 if (companyStatistics == null) {
67 throw new MonitoringException(
68 "No statistics found for company id " + companyId);
69 }
70
71 return companyStatistics;
72 }
73
74 public CompanyStatistics getCompanyStatistics(String webId)
75 throws MonitoringException {
76
77 CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
78 webId);
79
80 if (companyStatistics == null) {
81 throw new MonitoringException(
82 "No statistics found for web id " + webId);
83 }
84
85 return companyStatistics;
86 }
87
88 public Set<CompanyStatistics> getCompanyStatisticsSet() {
89 return new HashSet<CompanyStatistics>(
90 _companyStatisticsByWebId.values());
91 }
92
93 public Set<String> getWebIds() {
94 return _companyStatisticsByWebId.keySet();
95 }
96
97 public void processDataSample(
98 PortalRequestDataSample portalRequestDataSample) {
99
100 long companyId = portalRequestDataSample.getCompanyId();
101
102 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
103 companyId);
104
105 if (companyStatistics == null) {
106 try {
107 Company company = _companyLocalService.getCompany(companyId);
108
109 companyStatistics = register(company.getWebId());
110 }
111 catch (Exception e) {
112 throw new IllegalStateException(
113 "Unable to get company with company id " + companyId);
114 }
115 }
116
117 companyStatistics.processDataSample(portalRequestDataSample);
118 }
119
120 public synchronized CompanyStatistics register(String webId) {
121 CompanyStatistics companyStatistics = new CompanyStatistics(
122 _companyLocalService, webId);
123
124 _companyStatisticsByCompanyId.put(
125 companyStatistics.getCompanyId(), companyStatistics);
126 _companyStatisticsByWebId.put(webId, companyStatistics);
127
128 return companyStatistics;
129 }
130
131 public void reset() {
132 for (long companyId : _companyStatisticsByCompanyId.keySet()) {
133 reset(companyId);
134 }
135 }
136
137 public void reset(long companyId) {
138 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
139 companyId);
140
141 if (companyStatistics == null) {
142 return;
143 }
144
145 companyStatistics.reset();
146 }
147
148 public void reset(String webId) {
149 CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
150 webId);
151
152 if (companyStatistics == null) {
153 return;
154 }
155
156 companyStatistics.reset();
157 }
158
159 public synchronized void unregister(String webId) {
160 CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
161 webId);
162
163 if (companyStatistics != null) {
164 _companyStatisticsByCompanyId.remove(
165 companyStatistics.getCompanyId());
166 }
167 }
168
169 private CompanyLocalService _companyLocalService;
170 private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
171 new TreeMap<Long, CompanyStatistics>();
172 private Map<String, CompanyStatistics> _companyStatisticsByWebId =
173 new TreeMap<String, CompanyStatistics>();
174
175 }