1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.stats.impl

File XWikiStatsServiceImpl.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

16
44
15
1
246
160
25
0.57
2.93
15
1.67

Classes

Class Line # Actions
XWikiStatsServiceImpl 58 44 0% 25 28
0.6266666762.7%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.stats.impl;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Date;
25    import java.util.List;
26    import java.util.Map;
27   
28    import org.apache.commons.collections4.queue.CircularFifoQueue;
29    import org.slf4j.Logger;
30    import org.slf4j.LoggerFactory;
31    import org.xwiki.bridge.event.ActionExecutedEvent;
32    import org.xwiki.model.reference.DocumentReferenceResolver;
33    import org.xwiki.observation.EventListener;
34    import org.xwiki.observation.ObservationManager;
35    import org.xwiki.observation.event.Event;
36    import org.xwiki.observation.remote.RemoteObservationManagerContext;
37   
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.XWikiException;
40    import com.xpn.xwiki.criteria.impl.Duration;
41    import com.xpn.xwiki.criteria.impl.Period;
42    import com.xpn.xwiki.criteria.impl.Range;
43    import com.xpn.xwiki.criteria.impl.Scope;
44    import com.xpn.xwiki.doc.XWikiDocument;
45    import com.xpn.xwiki.stats.api.XWikiStatsService;
46    import com.xpn.xwiki.stats.impl.xwiki.XWikiStatsReader;
47    import com.xpn.xwiki.stats.impl.xwiki.XWikiStatsStoreService;
48    import com.xpn.xwiki.web.DownloadAction;
49    import com.xpn.xwiki.web.SaveAction;
50    import com.xpn.xwiki.web.Utils;
51    import com.xpn.xwiki.web.ViewAction;
52   
53    /**
54    * Store and retrieve statistics.
55    *
56    * @version $Id: 2411e81d548b94306930fb29d5ae235e662b2400 $
57    */
 
58    public class XWikiStatsServiceImpl implements XWikiStatsService, EventListener
59    {
60    /**
61    * Logging tools.
62    */
63    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiStatsServiceImpl.class);
64   
65    /**
66    * The name of the listener.
67    */
68    private static final String NAME = "statistics";
69   
70    /**
71    * User actions statistics module saves.
72    */
73    private static final List<Event> EVENTS = new ArrayList<Event>()
74    {
 
75  38 toggle {
76  38 add(new ActionExecutedEvent(ViewAction.VIEW_ACTION));
77  38 add(new ActionExecutedEvent(SaveAction.ACTION_NAME));
78  38 add(new ActionExecutedEvent(DownloadAction.ACTION_NAME));
79    }
80    };
81   
82    /**
83    * Used to resolve reference based on context.
84    */
85    private DocumentReferenceResolver<String> currentDocumentReferenceResolver = Utils.getComponent(
86    DocumentReferenceResolver.TYPE_STRING, "current");
87   
88    /**
89    * The statistics storing thread.
90    */
91    private XWikiStatsStoreService statsRegister;
92   
93    /**
94    * The statistics database reader.
95    */
96    private XWikiStatsReader statsReader = new XWikiStatsReader();
97   
 
98  300 toggle @Override
99    public String getName()
100    {
101  300 return NAME;
102    }
103   
 
104  60 toggle @Override
105    public List<Event> getEvents()
106    {
107  60 return EVENTS;
108    }
109   
 
110  60 toggle @Override
111    public void init(XWikiContext context)
112    {
113  60 if (LOGGER.isInfoEnabled()) {
114  28 LOGGER.info("Start statistics service initialization");
115    }
116   
117  60 if (StatsUtil.isStatsEnabled(context)) {
118    // Start statistics store thread
119  60 this.statsRegister = new XWikiStatsStoreService(context);
120  60 this.statsRegister.start();
121   
122    // Adding the rule which will allow this module to be called on each page view
123  60 Utils.getComponent(ObservationManager.class).addListener(this);
124    }
125    }
126   
 
127  0 toggle @Override
128    public Collection<Object> getRecentActions(String action, int size, XWikiContext context)
129    {
130  0 return this.statsReader.getRecentActions(action, size, context);
131    }
132   
 
133  924 toggle @Override
134    public void onEvent(Event event, Object source, Object data)
135    {
136  924 if (Utils.getComponent(RemoteObservationManagerContext.class).isRemoteState()) {
137    // we do nothing when the event comes from remote instance since the remote instance is supposed to already
138    // take care of this
139  0 return;
140    }
141   
142  923 ActionExecutedEvent actionEvent = (ActionExecutedEvent) event;
143  924 XWikiDocument document = (XWikiDocument) source;
144  925 XWikiContext context = (XWikiContext) data;
145   
146    // If the server is in read-only mode, forget about the statistics (since it's in read only mode we don't write
147    // anything in the database)
148  924 if (context.getWiki().isReadOnly()) {
149  0 return;
150    }
151   
152    // Initialize cookie used as unique identifier of a user visit and put it in the context
153  923 StatsUtil.findCookie(context);
154   
155  924 String action = actionEvent.getActionName();
156   
157    // Let's save in the session the last elements view, saved
158  924 synchronized (this) {
159  925 if (!action.equals(DownloadAction.ACTION_NAME)) {
160  744 Collection actions = StatsUtil.getRecentActionFromSessions(context, action);
161  744 if (actions == null) {
162  108 actions = new CircularFifoQueue(StatsUtil.getRecentVisitSize(context));
163  108 StatsUtil.setRecentActionsFromSession(context, action, actions);
164    }
165   
166  744 String element = document.getPrefixedFullName();
167  744 if (actions.contains(element)) {
168  365 actions.remove(element);
169    }
170  744 actions.add(element);
171    }
172    }
173   
174  925 try {
175  925 if (StatsUtil.isWikiStatsEnabled(context)
176    && !StatsUtil.getStorageFilteredUsers(context).contains(
177    this.currentDocumentReferenceResolver.resolve(context.getUser()))) {
178  0 this.statsRegister.addStats(document, action, context);
179    }
180    } catch (Exception e) {
181  0 LOGGER.error("Faild to get filter users list", e);
182    }
183    }
184   
 
185  0 toggle @Override
186    public Map<?, ?> getActionStatistics(String action, Scope scope, Period period, Duration step,
187    XWikiContext context)
188    {
189  0 return this.statsReader.getActionStatistics(action, scope, period, step, context);
190    }
191   
 
192  0 toggle @Override
193    public List<DocumentStats> getDocumentStatistics(String action, Scope scope, Period period, Range range,
194    XWikiContext context)
195    {
196  0 return this.statsReader.getDocumentStatistics(action, scope, period, range, context);
197    }
198   
 
199  0 toggle @Override
200    public List<DocumentStats> getBackLinkStatistics(String domain, Scope scope, Period period, Range range,
201    XWikiContext context)
202    {
203  0 return this.statsReader.getBackLinkStatistics(domain, scope, period, range, context);
204    }
205   
 
206  0 toggle @Override
207    public List<RefererStats> getRefererStatistics(String domain, Scope scope, Period period, Range range,
208    XWikiContext context)
209    {
210  0 return this.statsReader.getRefererStatistics(domain, scope, period, range, context);
211    }
212   
 
213  0 toggle @Override
214    public List<VisitStats> getVisitStatistics(String action, Period period, Range range, XWikiContext context)
215    {
216  0 return this.statsReader.getVisitStatistics(action, period, range, context);
217    }
218   
219    // ////////////////////////////////////////////////////////////////////////////////////////
220    // Deprecated methods
221    // ////////////////////////////////////////////////////////////////////////////////////////
222   
 
223  0 toggle @Override
224    public DocumentStats getDocTotalStats(String docname, String action, XWikiContext context)
225    {
226  0 return new DocumentStats();
227    }
228   
 
229  0 toggle @Override
230    public DocumentStats getDocMonthStats(String docname, String action, Date month, XWikiContext context)
231    {
232  0 return this.statsReader.getDocMonthStats(docname, action, month, context);
233    }
234   
 
235  0 toggle @Override
236    public DocumentStats getDocDayStats(String docname, String action, Date day, XWikiContext context)
237    {
238  0 return new DocumentStats();
239    }
240   
 
241  0 toggle @Override
242    public List<?> getRefMonthStats(String docName, Date month, XWikiContext context) throws XWikiException
243    {
244  0 return this.statsReader.getRefMonthStats(docName, month, context);
245    }
246    }