1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.stats.impl.xwiki; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Date; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.concurrent.ArrayBlockingQueue; |
28 |
|
|
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.slf4j.LoggerFactory; |
31 |
|
import org.xwiki.context.ExecutionContext; |
32 |
|
|
33 |
|
import com.xpn.xwiki.XWikiContext; |
34 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
35 |
|
import com.xpn.xwiki.stats.impl.StatsUtil; |
36 |
|
import com.xpn.xwiki.stats.impl.VisitStats; |
37 |
|
import com.xpn.xwiki.util.AbstractXWikiRunnable; |
38 |
|
import com.xpn.xwiki.web.DownloadAction; |
39 |
|
import com.xpn.xwiki.web.SaveAction; |
40 |
|
import com.xpn.xwiki.web.ViewAction; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
|
|
| 18.3% |
Uncovered Elements: 85 (104) |
Complexity: 28 |
Complexity Density: 0.41 |
|
48 |
|
public class XWikiStatsStoreService extends AbstractXWikiRunnable |
49 |
|
{ |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(XWikiStatsStoreService.class); |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private ArrayBlockingQueue<XWikiStatsStoreItem> queue; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private Thread thread; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private XWikiContext xwikiContext; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@param |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
75 |
60 |
public XWikiStatsStoreService(XWikiContext context)... |
76 |
|
{ |
77 |
60 |
this.xwikiContext = context.clone(); |
78 |
60 |
long queueSize = context.getWiki().ParamAsLong("stats.queue.size", 200); |
79 |
60 |
this.queue = new ArrayBlockingQueue<XWikiStatsStoreItem>((int) queueSize); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
82 |
60 |
@Override... |
83 |
|
protected void declareProperties(ExecutionContext executionContext) |
84 |
|
{ |
85 |
60 |
this.xwikiContext.declareInExecutionContext(executionContext); |
86 |
60 |
this.xwikiContext = null; |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
92 |
60 |
public void start()... |
93 |
|
{ |
94 |
60 |
if (this.thread == null) { |
95 |
60 |
this.thread = new Thread(this, "Statistics storing daemon"); |
96 |
|
|
97 |
60 |
this.thread.setDaemon(true); |
98 |
60 |
this.thread.start(); |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
105 |
0 |
public void stop()... |
106 |
|
{ |
107 |
0 |
this.queue.clear(); |
108 |
0 |
try { |
109 |
0 |
this.queue.put(new StopStatsRegisterObject()); |
110 |
0 |
this.thread.join(); |
111 |
0 |
this.thread = null; |
112 |
|
} catch (InterruptedException e) { |
113 |
0 |
if (LOGGER.isWarnEnabled()) { |
114 |
0 |
LOGGER.warn("Thread join has been interrupted", e); |
115 |
|
} |
116 |
|
} |
117 |
|
} |
118 |
|
|
|
|
| 27.3% |
Uncovered Elements: 8 (11) |
Complexity: 5 |
Complexity Density: 0.71 |
|
119 |
60 |
@Override... |
120 |
|
public void runInternal() |
121 |
|
{ |
122 |
60 |
try { |
123 |
60 |
while (true) { |
124 |
60 |
register(); |
125 |
|
} |
126 |
|
} catch (InterruptedException e) { |
127 |
0 |
if (LOGGER.isWarnEnabled()) { |
128 |
0 |
LOGGER.warn("Statistics storing thread has been interrupted.", e); |
129 |
|
} |
130 |
|
} catch (StopStatsStoreException e) { |
131 |
0 |
if (LOGGER.isInfoEnabled()) { |
132 |
0 |
LOGGER.warn("Statistics storing thread received stop order.", e); |
133 |
|
} |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
@throws |
141 |
|
@throws |
142 |
|
|
|
|
| 4.5% |
Uncovered Elements: 21 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
143 |
60 |
private void register() throws InterruptedException, StopStatsStoreException... |
144 |
|
{ |
145 |
60 |
XWikiStatsStoreItem stat = this.queue.take(); |
146 |
|
|
147 |
0 |
List<List<XWikiStatsStoreItem>> statsList = new ArrayList<List<XWikiStatsStoreItem>>(); |
148 |
0 |
Map<String, List<XWikiStatsStoreItem>> statsMap = new HashMap<String, List<XWikiStatsStoreItem>>(); |
149 |
|
|
150 |
0 |
do { |
151 |
0 |
if (stat instanceof StopStatsRegisterObject) { |
152 |
0 |
throw new StopStatsStoreException(); |
153 |
|
} |
154 |
|
|
155 |
0 |
String statId = stat.getId(); |
156 |
|
|
157 |
0 |
List<XWikiStatsStoreItem> stats = statsMap.get(statId); |
158 |
|
|
159 |
0 |
if (stats == null) { |
160 |
0 |
stats = new ArrayList<XWikiStatsStoreItem>(); |
161 |
|
|
162 |
0 |
statsMap.put(statId, stats); |
163 |
0 |
statsList.add(stats); |
164 |
|
} |
165 |
|
|
166 |
0 |
stats.add(stat); |
167 |
|
|
168 |
0 |
stat = this.queue.poll(); |
169 |
0 |
} while (stat != null); |
170 |
|
|
171 |
0 |
for (List<XWikiStatsStoreItem> stats : statsList) { |
172 |
0 |
stats.get(0).store(stats); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
@param |
184 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
185 |
0 |
public void add(XWikiStatsStoreItem statsRegisterItem)... |
186 |
|
{ |
187 |
0 |
try { |
188 |
0 |
this.queue.put(statsRegisterItem); |
189 |
|
} catch (InterruptedException e) { |
190 |
0 |
LOGGER.error("Statistics storage thread has been interrupted", e); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@param |
198 |
|
@param |
199 |
|
@param |
200 |
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 5 |
Complexity Density: 0.38 |
|
201 |
0 |
public void addStats(XWikiDocument doc, String action, XWikiContext context)... |
202 |
|
{ |
203 |
0 |
VisitStats vobject = StatsUtil.findVisit(context); |
204 |
0 |
synchronized (vobject) { |
205 |
0 |
if (action.equals(ViewAction.VIEW_ACTION)) { |
206 |
|
|
207 |
0 |
vobject.incPageViews(); |
208 |
0 |
} else if (action.equals(SaveAction.ACTION_NAME)) { |
209 |
|
|
210 |
0 |
vobject.incPageSaves(); |
211 |
0 |
} else if (action.equals(DownloadAction.ACTION_NAME)) { |
212 |
|
|
213 |
0 |
vobject.incDownloads(); |
214 |
|
} |
215 |
|
|
216 |
0 |
addVisitStats(vobject, context); |
217 |
|
|
218 |
0 |
boolean isVisit = (vobject.getPageViews() == 1) && (action.equals(ViewAction.VIEW_ACTION)); |
219 |
|
|
220 |
0 |
addDocumentStats(doc, action, isVisit, context); |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
0 |
if (action.equals(ViewAction.VIEW_ACTION)) { |
225 |
0 |
addRefererStats(doc, context); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
@param |
233 |
|
@param |
234 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
235 |
0 |
private void addVisitStats(VisitStats vobject, XWikiContext context)... |
236 |
|
{ |
237 |
0 |
Date currentDate = new Date(); |
238 |
|
|
239 |
0 |
vobject.setEndDate(currentDate); |
240 |
0 |
add(new VisitStatsStoreItem(vobject, context)); |
241 |
0 |
vobject.unrememberOldObject(); |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
@param |
248 |
|
@param |
249 |
|
@param |
250 |
|
@param |
251 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
252 |
0 |
private void addDocumentStats(XWikiDocument doc, String action, boolean isVisit, XWikiContext context)... |
253 |
|
{ |
254 |
0 |
Date currentDate = new Date(); |
255 |
|
|
256 |
0 |
add(new DocumentStatsStoreItem(doc.getFullName(), currentDate, StatsUtil.PeriodType.MONTH, action, isVisit, |
257 |
|
context)); |
258 |
0 |
add(new DocumentStatsStoreItem(doc.getSpace(), currentDate, StatsUtil.PeriodType.MONTH, action, isVisit, |
259 |
|
context)); |
260 |
0 |
add(new DocumentStatsStoreItem("", currentDate, StatsUtil.PeriodType.MONTH, action, false, context)); |
261 |
0 |
add(new DocumentStatsStoreItem(doc.getFullName(), currentDate, StatsUtil.PeriodType.DAY, action, isVisit, |
262 |
|
context)); |
263 |
0 |
add(new DocumentStatsStoreItem(doc.getSpace(), currentDate, StatsUtil.PeriodType.DAY, action, isVisit, |
264 |
|
context)); |
265 |
0 |
add(new DocumentStatsStoreItem("", currentDate, StatsUtil.PeriodType.DAY, action, false, context)); |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
@param |
272 |
|
@param |
273 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
274 |
0 |
private void addRefererStats(XWikiDocument doc, XWikiContext context)... |
275 |
|
{ |
276 |
0 |
String referer = StatsUtil.getReferer(context); |
277 |
0 |
if ((referer != null) && (!referer.equals(""))) { |
278 |
0 |
add(new RefererStatsStoreItem(doc.getFullName(), new Date(), StatsUtil.PeriodType.MONTH, referer, context)); |
279 |
|
} |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
@version |
287 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
288 |
|
class StopStatsRegisterObject implements XWikiStatsStoreItem |
289 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
290 |
0 |
@Override... |
291 |
|
public String getId() |
292 |
|
{ |
293 |
0 |
return null; |
294 |
|
} |
295 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
296 |
0 |
@Override... |
297 |
|
public void store(List<XWikiStatsStoreItem> register) |
298 |
|
{ |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
@version |
306 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
307 |
|
class StopStatsStoreException extends Exception |
308 |
|
{ |
309 |
|
|
310 |
|
} |