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

File MonitorData.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

30
89
27
1
277
216
42
0.47
3.3
27
1.56

Classes

Class Line # Actions
MonitorData 33 89 0% 42 146
0.00%
 

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.monitor.api;
21   
22    import java.net.URL;
23    import java.util.ArrayList;
24    import java.util.Date;
25    import java.util.HashMap;
26    import java.util.Iterator;
27    import java.util.List;
28    import java.util.Map;
29   
30    import org.slf4j.Logger;
31    import org.slf4j.LoggerFactory;
32   
 
33    public class MonitorData
34    {
35    private static final Logger LOGGER = LoggerFactory.getLogger(MonitorData.class);
36   
37    private URL url;
38   
39    private String wikiPage;
40   
41    private String action;
42   
43    private String threadName;
44   
45    private Date startTime;
46   
47    private Date endTime;
48   
49    private Map<String, MonitorTimer> timers = new HashMap<>();
50   
51    private Map<String, MonitorTimerSummary> timerSummaries = new HashMap<>();
52   
53    private List<MonitorTimer> timerList = new ArrayList<>();
54   
 
55  0 toggle public MonitorData(String wikiPage, String action, URL url, String threadName)
56    {
57  0 this.setWikiPage(wikiPage);
58  0 this.setURL(url);
59  0 this.setThreadName(threadName);
60  0 this.setStartTime(new Date());
61  0 this.setAction(action);
62    }
63   
 
64  0 toggle public void startRequest(String page, URL url)
65    {
66  0 setWikiPage(page);
67  0 setURL(url);
68  0 setStartTime(new Date());
69    }
70   
 
71  0 toggle public void endRequest()
72    {
73  0 endRequest(true);
74    }
75   
 
76  0 toggle public void endRequest(boolean normal)
77    {
78  0 setEndTime(new Date());
79  0 log();
80    }
81   
 
82  0 toggle public URL getURL()
83    {
84  0 return this.url;
85    }
86   
 
87  0 toggle public void setURL(URL url)
88    {
89  0 this.url = url;
90    }
91   
 
92  0 toggle public String getWikiPage()
93    {
94  0 return this.wikiPage;
95    }
96   
 
97  0 toggle public void setWikiPage(String page)
98    {
99  0 this.wikiPage = page;
100    }
101   
 
102  0 toggle public Date getStartTime()
103    {
104  0 return this.startTime;
105    }
106   
 
107  0 toggle public void setStartTime(Date startTime)
108    {
109  0 this.startTime = startTime;
110    }
111   
 
112  0 toggle public Date getEndTime()
113    {
114  0 return this.endTime;
115    }
116   
 
117  0 toggle public void setEndTime(Date endTime)
118    {
119  0 this.endTime = endTime;
120    }
121   
 
122  0 toggle public long getDuration()
123    {
124  0 return (this.endTime.getTime() - this.startTime.getTime());
125    }
126   
 
127  0 toggle public long getDuration(String timer)
128    {
129  0 MonitorTimerSummary tsummary = this.timerSummaries.get(timer);
130  0 if (tsummary == null) {
131  0 return 0;
132    } else {
133  0 return tsummary.getDuration();
134    }
135    }
136   
 
137  0 toggle public void startTimer(String timername, String details)
138    {
139   
140  0 if (this.startTime == null) {
141  0 return;
142    }
143   
144  0 MonitorTimer timer;
145  0 timer = this.timers.get(timername);
146  0 if (timer != null) {
147  0 if (LOGGER.isDebugEnabled()) {
148  0 LOGGER.debug("MONITOR: error recursive timers for " + timername);
149    }
150    } else {
151  0 timer = new MonitorTimer(timername, details);
152  0 timer.setStartDate();
153  0 this.timers.put(timername, timer);
154    }
155    }
156   
 
157  0 toggle public void startTimer(String timername)
158    {
159  0 startTimer(timername, "");
160    }
161   
 
162  0 toggle public void setTimerDetails(String timername, String details)
163    {
164  0 MonitorTimer timer;
165  0 timer = this.timers.get(timername);
166  0 if (timer == null) {
167  0 if (LOGGER.isDebugEnabled()) {
168  0 LOGGER.debug("MONITOR: could not find timer for " + timername);
169    }
170    } else {
171  0 timer.setDetails(details);
172    }
173    }
174   
 
175  0 toggle public void endTimer(String timername)
176    {
177  0 if (this.startTime == null) {
178  0 return;
179    }
180   
181  0 MonitorTimer timer;
182  0 timer = this.timers.get(timername);
183  0 if (timer == null) {
184  0 if (LOGGER.isDebugEnabled()) {
185  0 LOGGER.debug("MONITOR: could not find timer for " + timername);
186    }
187    } else {
188  0 timer.setEndDate();
189  0 if (timer.getDetails() != null) {
190  0 this.timerList.add(timer);
191    }
192  0 this.timers.remove(timername);
193  0 MonitorTimerSummary tsummary = this.timerSummaries.get(timername);
194  0 if (tsummary == null) {
195  0 tsummary = new MonitorTimerSummary(timername);
196  0 this.timerSummaries.put(timername, tsummary);
197    }
198  0 tsummary.addTimer(timer.getDuration());
199  0 if (LOGGER.isDebugEnabled()) {
200  0 LOGGER.debug("MONITOR " + this.wikiPage + " " + this.action + " " + timer.getName() + ": "
201    + timer.getDuration() + "ms " + timer.getDetails());
202    }
203    }
204    }
205   
 
206  0 toggle public List<MonitorTimer> getTimerList()
207    {
208  0 return this.timerList;
209    }
210   
 
211  0 toggle public Map<String, MonitorTimerSummary> getTimerSummaries()
212    {
213  0 return this.timerSummaries;
214    }
215   
 
216  0 toggle public void log()
217    {
218  0 if (LOGGER.isDebugEnabled()) {
219  0 LOGGER.debug("MONITOR " + this.wikiPage + ": " + getDuration() + "ms");
220  0 Iterator<MonitorTimerSummary> it = this.timerSummaries.values().iterator();
221  0 while (it.hasNext()) {
222  0 MonitorTimerSummary tsummary = it.next();
223  0 LOGGER.debug("MONITOR " + this.wikiPage + " " + this.action + " " + tsummary.getName() + ": "
224    + tsummary.getDuration() + "ms " + tsummary.getNbCalls());
225    }
226    }
227    }
228   
 
229  0 toggle public String getThreadName()
230    {
231  0 return this.threadName;
232    }
233   
 
234  0 toggle public void setThreadName(String threadName)
235    {
236  0 this.threadName = threadName;
237    }
238   
 
239  0 toggle public long getNbCalls(String timer)
240    {
241  0 MonitorTimerSummary tsummary = this.timerSummaries.get(timer);
242  0 if (tsummary == null) {
243  0 return 0;
244    } else {
245  0 return tsummary.getNbCalls();
246    }
247    }
248   
 
249  0 toggle public String getAction()
250    {
251  0 return this.action;
252    }
253   
 
254  0 toggle public void setAction(String action)
255    {
256  0 this.action = action;
257    }
258   
 
259  0 toggle @Override
260    public String toString()
261    {
262  0 StringBuffer str = new StringBuffer();
263  0 str.append("WikiPage: ");
264  0 str.append(this.wikiPage);
265  0 str.append(" Action: ");
266  0 str.append(this.action);
267  0 str.append(" URL: ");
268  0 str.append(this.url.toString());
269  0 str.append(" Thread: ");
270  0 str.append(this.threadName);
271  0 str.append(" StartTime: " + this.startTime);
272  0 str.append(" EndTime: " + this.endTime);
273  0 str.append(getTimerSummaries());
274  0 str.append(getTimerList());
275  0 return str.toString();
276    }
277    }