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