Coverage report

  %line %branch
org.apache.jetspeed.statistics.impl.PortalStatisticsImpl
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.jetspeed.statistics.impl;
 19  
 
 20  
 import java.security.Principal;
 21  
 import java.sql.Connection;
 22  
 import java.sql.PreparedStatement;
 23  
 import java.sql.ResultSet;
 24  
 import java.sql.SQLException;
 25  
 import java.sql.Timestamp;
 26  
 import java.text.MessageFormat;
 27  
 import java.text.SimpleDateFormat;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Calendar;
 30  
 import java.util.Collections;
 31  
 import java.util.Date;
 32  
 import java.util.GregorianCalendar;
 33  
 import java.util.HashMap;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 import java.util.TreeMap;
 37  
 
 38  
 import javax.naming.NamingException;
 39  
 import javax.servlet.http.HttpServletRequest;
 40  
 import javax.sql.DataSource;
 41  
 
 42  
 import org.apache.commons.logging.Log;
 43  
 import org.apache.commons.logging.LogFactory;
 44  
 import org.apache.jetspeed.om.page.ContentPage;
 45  
 import org.apache.jetspeed.request.RequestContext;
 46  
 import org.apache.jetspeed.statistics.AggregateStatistics;
 47  
 import org.apache.jetspeed.statistics.InvalidCriteriaException;
 48  
 import org.apache.jetspeed.statistics.PortalStatistics;
 49  
 import org.apache.jetspeed.statistics.StatisticsQueryCriteria;
 50  
 import org.apache.jetspeed.statistics.UserStats;
 51  
 import org.springframework.orm.ojb.support.PersistenceBrokerDaoSupport;
 52  
 
 53  
 /**
 54  
  * <p>
 55  
  * PortalStatisticsImpl
 56  
  * </p>
 57  
  * 
 58  
  * @author <a href="mailto:chris@bluesunrise.com">Chris Schaefer </a>
 59  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
 60  
  * @version $Id: TestPortletEntityDAO.java,v 1.3 2005/05/24 14:43:19 ate Exp $
 61  
  */
 62  
 public class PortalStatisticsImpl extends PersistenceBrokerDaoSupport implements
 63  
         PortalStatistics
 64  
 {
 65  
     /* CLF logger */
 66  0
     protected final static Log logger = LogFactory
 67  0
             .getLog(PortalStatisticsImpl.class);
 68  
 
 69  
     /* batch of portlet statistics */
 70  
     protected BatchedStatistics portletBatch;
 71  
 
 72  
     /* batch if page statistics */
 73  
     protected BatchedStatistics pageBatch;
 74  
 
 75  
     /* batch of user statistics */
 76  
     protected BatchedStatistics userBatch;
 77  
 
 78  
     /* format string for a portlet access log entry */
 79  
     protected static final String portletLogFormat = "{0} {1} {2} [{3}] \"{4} {5} {6}\" {7} {8}";
 80  
 
 81  
     /* format string for a page access log entry */
 82  
     protected static final String pageLogFormat = "{0} {1} {2} [{3}] \"{4} {5}\" {6} {7}";
 83  
 
 84  
     /* Format string for a User Logout log entry */
 85  
     protected static final String logoutLogFormat = "{0} {1} {2} [{3}] \"{4}\" {5} {6}";
 86  
 
 87  
     protected static final int STATUS_LOGGED_IN = 1;
 88  
 
 89  
     protected static final int STATUS_LOGGED_OUT = 2;
 90  
 
 91  
     /* the following fields should be settable with Spring injection */
 92  0
     protected boolean logToCLF = true;
 93  
 
 94  0
     protected boolean logToDatabase = true;
 95  
 
 96  0
     protected int maxRecordToFlush_Portlet = 30;
 97  
 
 98  0
     protected int maxRecordToFlush_User = 30;
 99  
 
 100  0
     protected int maxRecordToFlush_Page = 30;
 101  
 
 102  0
     protected long maxTimeMsToFlush_Portlet = 10 * 1000;
 103  
 
 104  0
     protected long maxTimeMsToFlush_User = 10 * 1000;
 105  
 
 106  0
     protected long maxTimeMsToFlush_Page = 10 * 1000;
 107  
 
 108  
     //protected ConnectionRepositoryEntry jetspeedDSEntry;
 109  
     
 110  
     /* after this is NOT for injection */
 111  
 
 112  
     protected DataSource ds;
 113  
 
 114  0
     protected int currentUserCount = 0;
 115  
 
 116  
     protected Map currentUsers;
 117  
 
 118  
     /* date formatter */
 119  0
     protected SimpleDateFormat formatter = null;
 120  
 
 121  
     /**
 122  
      * <p>
 123  
      * Default constructor.
 124  
      * </p>
 125  
      */
 126  
     
 127  
     public PortalStatisticsImpl(boolean logToCLF, class="keyword">boolean logToDatabase,
 128  
             int maxRecordToFlush_Portal, class="keyword">int maxRecordToFlush_User,
 129  
             int maxRecordToFlush_Page, long maxTimeMsToFlush_Portal,
 130  
             long maxTimeMsToFlush_User, class="keyword">long maxTimeMsToFlush_Page,
 131  
             DataSource dataSource)
 132  
             //ConnectionRepositoryEntry jetspeedDSEntry)
 133  0
     {
 134  
 
 135  0
         this.logToCLF = logToCLF;
 136  0
         this.logToDatabase = logToDatabase;
 137  0
         this.maxRecordToFlush_Portlet = maxRecordToFlush_Portal;
 138  0
         this.maxRecordToFlush_User = maxRecordToFlush_User;
 139  0
         this.maxRecordToFlush_Page = maxRecordToFlush_Page;
 140  0
         this.maxTimeMsToFlush_Portlet = maxTimeMsToFlush_Portal;
 141  0
         this.maxTimeMsToFlush_User = maxTimeMsToFlush_User;
 142  0
         this.maxTimeMsToFlush_Page = maxTimeMsToFlush_Page;
 143  
         //this.jetspeedDSEntry = jetspeedDSEntry;
 144  0
         this.ds = dataSource;        
 145  0
         currentUsers = Collections.synchronizedMap(new TreeMap());
 146  0
     }
 147  
 
 148  
     public void springInit() throws NamingException
 149  
     {
 150  0
         formatter = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss z");
 151  0
         currentUserCount = 0;
 152  0
     }
 153  
 
 154  
     public DataSource getDataSource()
 155  
     {
 156  0
         return ds;
 157  
     }
 158  
 
 159  
     public void logPortletAccess(RequestContext request, String portletName,
 160  
             String statusCode, long msElapsedTime)
 161  
     {
 162  
 
 163  
         try
 164  
         {
 165  0
             HttpServletRequest req = request.getRequest();
 166  0
             Principal principal = req.getUserPrincipal();
 167  0
             String userName = (principal != null) ? principal.getName()
 168  
                     : "guest";
 169  0
             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
 170  0
             PortletLogRecord record = new PortletLogRecord();
 171  
 
 172  0
             record.setPortletName(portletName);
 173  0
             record.setUserName(userName);
 174  0
             if (req.getRemoteAddr() != null)
 175  
             {
 176  0
                 record.setIpAddress(req.getRemoteAddr());
 177  
             }
 178  0
             ContentPage cp = request.getPage();
 179  0
             if (cp != null)
 180  
             {
 181  0
                 if (cp.getPath() != null)
 182  
                 {
 183  0
                     record.setPagePath(cp.getPath());
 184  
                 }
 185  
             }
 186  0
             record.setStatus(Integer.parseInt(statusCode));
 187  0
             record.setTimeStamp(timestamp);
 188  0
             record.setMsElapsedTime(msElapsedTime);
 189  
 
 190  0
             if (logToCLF)
 191  
             {
 192  0
                 saveAccessToCLF(record);
 193  
             }
 194  0
             if (logToDatabase)
 195  
             {
 196  0
                 storeAccessToStats(record);
 197  
             }
 198  0
         } catch (Exception e)
 199  
         {
 200  0
             logger.error("Exception", e);
 201  0
         }
 202  0
     }
 203  
 
 204  
     protected void storeAccessToStats(LogRecord record)
 205  
     {
 206  
 
 207  0
         if (record instanceof PortletLogRecord)
 208  
         {
 209  0
             if (portletBatch == null)
 210  
             {
 211  0
                 synchronized (this)
 212  
                 {
 213  0
                     if (portletBatch == null)
 214  
                     {
 215  0
                         portletBatch = new BatchedPortletStatistics(ds,
 216  
                                 this.maxRecordToFlush_Portlet,
 217  
                                 this.maxTimeMsToFlush_Portlet, "portletLogBatcher");
 218  0
                         portletBatch.startThread();
 219  
                     }
 220  0
                 }
 221  
             }
 222  0
             portletBatch.addStatistic(record);
 223  
 
 224  
         }
 225  0
         if (record instanceof PageLogRecord)
 226  
         {
 227  0
             if (pageBatch == null)
 228  
             {
 229  0
                 synchronized (this)
 230  
                 {
 231  0
                     if (pageBatch == null)
 232  
                     {
 233  0
                         pageBatch = new BatchedPageStatistics(ds,
 234  
                                 this.maxRecordToFlush_Page, class="keyword">this.maxTimeMsToFlush_Page,
 235  
                                 "pageLogBatcher");
 236  0
                         pageBatch.startThread();
 237  
                     }
 238  0
                 }
 239  
             }
 240  0
             pageBatch.addStatistic(record);
 241  
 
 242  
         }
 243  0
         if (record instanceof UserLogRecord)
 244  
         {
 245  0
             if (userBatch == null)
 246  
             {
 247  0
                 synchronized (this)
 248  
                 {
 249  0
                     if (userBatch == null)
 250  
                     {
 251  0
                         userBatch = new BatchedUserStatistics(ds,
 252  
                                 this.maxRecordToFlush_User, class="keyword">this.maxTimeMsToFlush_User,
 253  
                                 "userLogBatcher");
 254  0
                         userBatch.startThread();
 255  
                     }
 256  0
                 }
 257  
             }
 258  0
             userBatch.addStatistic(record);
 259  
 
 260  
         }
 261  0
     }
 262  
 
 263  
     protected void saveAccessToCLF(LogRecord record)
 264  
     {
 265  0
         Object[] args =  {""};
 266  0
         String logMessage = "";
 267  0
         if (record instanceof PortletLogRecord)
 268  
         {
 269  0
             PortletLogRecord rec = (PortletLogRecord) record;
 270  0
             Object[] args1 =
 271  
             { rec.getIpAddress(), "-", rec.getUserName(), rec.getTimeStamp(),
 272  
                     rec.getLogType(), formatter.format(rec.getTimeStamp()),
 273  
                     rec.getPortletName(),
 274  
                     new Integer(rec.getStatus()).toString(),
 275  
                     new Long(rec.getMsElapsedTime())};
 276  0
             args = args1;
 277  0
             logMessage = MessageFormat.format(portletLogFormat, args)
 278  
                     .toString();
 279  
         }
 280  0
         if (record instanceof PageLogRecord)
 281  
         {
 282  0
             PageLogRecord rec = (PageLogRecord) record;
 283  0
             Object[] args1 =
 284  
             { rec.getIpAddress(), "-", rec.getUserName(), rec.getTimeStamp(),
 285  
                     rec.getLogType(), formatter.format(rec.getTimeStamp()),
 286  
                     new Integer(rec.getStatus()).toString(),
 287  
                     new Long(rec.getMsElapsedTime())};
 288  0
             args = args1;
 289  0
             logMessage = MessageFormat.format(pageLogFormat, args).toString();
 290  
         }
 291  0
         if (record instanceof UserLogRecord)
 292  
         {
 293  0
             UserLogRecord rec = (UserLogRecord) record;
 294  0
             Object[] args1 =
 295  
             { rec.getIpAddress(), "-", rec.getUserName(), rec.getTimeStamp(),
 296  
                     rec.getLogType(), formatter.format(rec.getTimeStamp()),
 297  
                     new Integer(rec.getStatus()).toString(),
 298  
                     new Long(rec.getMsElapsedTime())};
 299  0
             args = args1;
 300  0
             logMessage = MessageFormat.format(logoutLogFormat, args).toString();
 301  
         }
 302  0
         logger.info(logMessage);
 303  0
     }
 304  
 
 305  
     public void logPageAccess(RequestContext request, String statusCode,
 306  
             long msElapsedTime)
 307  
     {
 308  
         try
 309  
         {
 310  0
             HttpServletRequest req = request.getRequest();
 311  0
             Principal principal = req.getUserPrincipal();
 312  0
             String userName = (principal != null) ? principal.getName()
 313  
                     : "guest";
 314  0
             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
 315  0
             PageLogRecord record = new PageLogRecord();
 316  
 
 317  0
             record.setUserName(userName);
 318  0
             record.setIpAddress(req.getRemoteAddr());
 319  0
             ContentPage cp = request.getPage();
 320  0
             if (cp != null)
 321  
             {
 322  0
                 if (cp.getPath() != null)
 323  
                 {
 324  0
                     record.setPagePath(cp.getPath());
 325  
                 }
 326  
             }
 327  0
             record.setStatus(Integer.parseInt(statusCode));
 328  0
             record.setTimeStamp(timestamp);
 329  0
             record.setMsElapsedTime(msElapsedTime);
 330  
 
 331  0
             if (logToCLF)
 332  
             {
 333  0
                 saveAccessToCLF(record);
 334  
             }
 335  0
             if (logToDatabase)
 336  
             {
 337  0
                 storeAccessToStats(record);
 338  
             }
 339  
 
 340  0
         } catch (Exception e)
 341  
         {
 342  0
             logger.error("Exception", e);
 343  0
         }
 344  0
     }
 345  
 
 346  
     public void logUserLogout(String ipAddress, String userName,
 347  
             long msSessionLength)
 348  
     {
 349  
         try
 350  
         {
 351  
 
 352  0
             if (userName == null)
 353  
             {
 354  0
                 userName = "guest";
 355  
             }
 356  
 
 357  0
             if (!"guest".equals(userName))
 358  
             {
 359  0
                 synchronized (currentUsers)
 360  
                 {
 361  0
                 	UserStats userStats = null;
 362  
                 	
 363  0
                 	Map users = (Map)currentUsers.get(userName);                	
 364  0
                 	if(users != null && users.size() > 0)
 365  
                 	{
 366  0
                 		userStats = (UserStats) users.get(ipAddress);                		
 367  
                 	}                	
 368  
             	
 369  0
                 	if(userStats != null)
 370  
                     {
 371  
                     	// only decrement if user has been logged in
 372  0
                     	currentUserCount = currentUserCount - 1;
 373  
                         
 374  0
                     	userStats.setNumberOfSession(userStats
 375  
                                 .getNumberOfSessions() - 1);                    
 376  0
                         if (userStats.getNumberOfSessions() <= 0)
 377  
                         {
 378  0
                         	users.remove(ipAddress);
 379  0
                             currentUsers.put(userName, users);
 380  
                         }
 381  
                     }
 382  0
                 }
 383  
             }
 384  
 
 385  0
             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
 386  0
             UserLogRecord record = new UserLogRecord();
 387  
 
 388  0
             record.setUserName(userName);
 389  0
             record.setIpAddress(ipAddress);
 390  0
             record.setStatus(STATUS_LOGGED_OUT);
 391  0
             record.setTimeStamp(timestamp);
 392  0
             record.setMsElapsedTime(msSessionLength);
 393  
 
 394  0
             if (logToCLF)
 395  
             {
 396  0
                 saveAccessToCLF(record);
 397  
             }
 398  0
             if (logToDatabase)
 399  
             {
 400  0
                 storeAccessToStats(record);
 401  
             }
 402  
 
 403  0
         } catch (Exception e)
 404  
         {
 405  0
             logger.error("Exception", e);
 406  0
         }
 407  0
     }
 408  
 
 409  
     /*
 410  
      * (non-Javadoc)
 411  
      * 
 412  
      * @see org.apache.jetspeed.statistics.PortalStatistics#logUserLogin(org.apache.jetspeed.request.RequestContext,
 413  
      *      long)
 414  
      */
 415  
     public void logUserLogin(RequestContext request, long msElapsedLoginTime)
 416  
     {
 417  
         try
 418  
         {
 419  0
             HttpServletRequest req = request.getRequest();
 420  0
             Principal principal = req.getUserPrincipal();
 421  0
             String userName = (principal != null) ? principal.getName()
 422  
                     : "guest";
 423  0
             String ipAddress = req.getRemoteAddr();
 424  0
             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
 425  0
             UserLogRecord record = new UserLogRecord();
 426  
 
 427  0
             if (!"guest".equals(userName))
 428  
             {
 429  0
                 currentUserCount = currentUserCount + 1;
 430  
 
 431  0
                 synchronized (currentUsers)
 432  
                 {
 433  
                 	
 434  0
                 	UserStats userStats = null;
 435  
                 	
 436  0
                 	Map users = (Map)currentUsers.get(userName);                	
 437  0
                 	if(users != null && users.size() > 0)
 438  
                 	{
 439  0
                 		userStats = (UserStats) users.get(ipAddress);                		
 440  
                 	}
 441  
                 	else
 442  
                 	{
 443  0
                 		users = new TreeMap();
 444  
                 	}
 445  
                 	
 446  0
                 	if(userStats == null)
 447  
                     {
 448  0
                         userStats = new UserStatsImpl();
 449  0
                         userStats.setNumberOfSession(0);
 450  0
                         userStats.setUsername(userName);
 451  0
                         userStats.setInetAddressFromIp(ipAddress);                        
 452  
                     }
 453  
                     
 454  0
                     userStats.setNumberOfSession(userStats
 455  
                             .getNumberOfSessions() + 1);
 456  0
                     users.put(ipAddress, userStats);
 457  0
             		currentUsers.put(userName, users);
 458  0
                 }
 459  
             }
 460  
 
 461  0
             record.setUserName(userName);
 462  0
             record.setIpAddress(ipAddress);
 463  0
             record.setStatus(STATUS_LOGGED_IN);
 464  0
             record.setTimeStamp(timestamp);
 465  0
             record.setMsElapsedTime(msElapsedLoginTime);
 466  
 
 467  0
             if (logToCLF)
 468  
             {
 469  0
                 saveAccessToCLF(record);
 470  
             }
 471  0
             if (logToDatabase)
 472  
             {
 473  0
                 storeAccessToStats(record);
 474  
             }
 475  
 
 476  0
         } catch (Exception e)
 477  
         {
 478  0
             logger.error("Exception", e);
 479  0
         }
 480  
 
 481  0
     }
 482  
 
 483  
     /**
 484  
      * @see org.springframework.beans.factory.DisposableBean#destroy()
 485  
      */
 486  
     public void springDestroy()
 487  
     {
 488  0
         if (portletBatch != null)
 489  
         {
 490  0
             portletBatch.tellThreadToStop();
 491  0
             synchronized (portletBatch.thread)
 492  
             {
 493  0
                 portletBatch.thread.notify();
 494  0
             }
 495  
 
 496  
         }
 497  0
         if (userBatch != null)
 498  
         {
 499  0
             userBatch.tellThreadToStop();
 500  0
             synchronized (userBatch.thread)
 501  
             {
 502  0
                 userBatch.thread.notify();
 503  0
             }
 504  
         }
 505  0
         if (pageBatch != null)
 506  
         {
 507  0
             pageBatch.tellThreadToStop();
 508  0
             synchronized (pageBatch.thread)
 509  
             {
 510  0
                 pageBatch.thread.notify();
 511  0
             }
 512  
         }
 513  
 
 514  0
         if ((this.currentUserCount != 0) && logger.isDebugEnabled())
 515  
         {
 516  0
             logger.debug("destroying while users are logged in");
 517  
         }
 518  0
         boolean done = false;
 519  0
         while (!done)
 520  
         {
 521  0
             done = true;
 522  0
             if (portletBatch != null)
 523  
             {
 524  0
                 if (!portletBatch.isDone())
 525  
                 {
 526  0
                     done = false;
 527  
                 }
 528  
             }
 529  0
             if (userBatch != null)
 530  
             {
 531  0
                 if (!userBatch.isDone())
 532  
                 {
 533  0
                     done = false;
 534  
                 }
 535  
             }
 536  0
             if (pageBatch != null)
 537  
             {
 538  0
                 if (!pageBatch.isDone())
 539  
                 {
 540  0
                     done = false;
 541  
                 }
 542  
             }
 543  
 
 544  
             try
 545  
             {
 546  0
                 Thread.sleep(2);
 547  0
             } catch (InterruptedException ie)
 548  
             {
 549  0
             }
 550  
         }
 551  
 
 552  0
     }
 553  
 
 554  
     /**
 555  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getNumberOfCurrentUsers()
 556  
      */
 557  
     public int getNumberOfCurrentUsers()
 558  
     {
 559  0
         return currentUserCount;
 560  
     }
 561  
 
 562  
     protected Date getStartDateFromPeriod(String period, Date end)
 563  
     {
 564  0
         GregorianCalendar gcEnd = new GregorianCalendar();
 565  0
         gcEnd.setTime(end);
 566  0
         if (period != null)
 567  
         {
 568  0
             if (period.endsWith("m"))
 569  
             {
 570  
                 // months
 571  0
                 String p = period.substring(0, period.length() - 1);
 572  0
                 int ret = Integer.parseInt(p);
 573  0
                 gcEnd.add(Calendar.MONTH, (ret * -1));
 574  0
             } else if (period.endsWith("d"))
 575  
             {
 576  
                 // days
 577  0
                 String p = period.substring(0, period.length() - 1);
 578  0
                 int ret = Integer.parseInt(p);
 579  0
                 gcEnd.add(Calendar.HOUR, (ret * 24 * -1));
 580  0
             } else if (period.endsWith("h"))
 581  
             {
 582  
                 // hours
 583  0
                 String p = period.substring(0, period.length() - 1);
 584  0
                 int ret = Integer.parseInt(p);
 585  0
                 gcEnd.add(Calendar.HOUR, (ret * -1));
 586  0
             } else if (period.equals("all"))
 587  
             {
 588  0
                 gcEnd = new GregorianCalendar();
 589  0
                 gcEnd.set(1968, 07, 15);
 590  
             } else
 591  
             {
 592  
                 // minutes
 593  0
                 int ret = Integer.parseInt(period);
 594  0
                 gcEnd.add(Calendar.MINUTE, (ret * -1));
 595  0
             }
 596  
         } else
 597  
         {
 598  0
             gcEnd = new GregorianCalendar();
 599  0
             gcEnd.set(1968, 07, 15);
 600  
 
 601  
         }
 602  0
         return gcEnd.getTime();
 603  
     }
 604  
 
 605  
     
 606  
     /* (non-Javadoc)
 607  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getDefaultEmptyStatisticsQueryCriteria()
 608  
      */
 609  
     public StatisticsQueryCriteria createStatisticsQueryCriteria()
 610  
     {
 611  0
         return new StatisticsQueryCriteriaImpl();
 612  
     }
 613  
 
 614  
     /* (non-Javadoc)
 615  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getDefaultEmptyAggregateStatistics()
 616  
      */
 617  
     public AggregateStatistics getDefaultEmptyAggregateStatistics()
 618  
     {
 619  0
         return new AggregateStatisticsImpl();
 620  
     }
 621  
 
 622  
     /**
 623  
      * @see org.apache.jetspeed.statistics.PortalStatistics#queryStatistics(org.apache.jetspeed.statistics.StatisticsQueryCriteria)
 624  
      */
 625  
     public AggregateStatistics queryStatistics(StatisticsQueryCriteria criteria)
 626  
             throws InvalidCriteriaException
 627  
     {
 628  0
         AggregateStatistics as = new AggregateStatisticsImpl();
 629  
         String query;
 630  
         String query2;
 631  
 
 632  
         String tableName;
 633  
         String groupColumn;
 634  
 
 635  0
         Date end = new Date();
 636  0
         Date start = getStartDateFromPeriod(criteria.getTimePeriod(), end);
 637  
 
 638  0
         String queryType = criteria.getQueryType();
 639  
         
 640  
 
 641  0
         if (PortalStatistics.QUERY_TYPE_USER.equals(queryType))
 642  
         {
 643  0
             tableName = "USER_STATISTICS";
 644  0
             groupColumn = "USER_NAME";
 645  0
         } else if (PortalStatistics.QUERY_TYPE_PORTLET.equals(queryType))
 646  
         {
 647  0
             tableName = "PORTLET_STATISTICS";
 648  0
             groupColumn = "PORTLET";
 649  0
         } else if (PortalStatistics.QUERY_TYPE_PAGE.equals(queryType))
 650  
         {
 651  0
             tableName = "PAGE_STATISTICS";
 652  0
             groupColumn = "PAGE";
 653  
         } else
 654  
         {
 655  0
             throw new InvalidCriteriaException(
 656  
                     " invalid queryType passed to queryStatistics");
 657  
         }
 658  0
         String orderColumn = "itemcount";
 659  
 
 660  0
         String ascDesc = "DESC";
 661  
 
 662  0
         if (!PortalStatistics.QUERY_TYPE_USER.equals(queryType))
 663  
         {
 664  0
             query = "select count(*) as itemcount , MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax from "
 665  
                     + tableName + " where time_stamp > ? and time_stamp < ?";
 666  0
             query2 = "select count(*) as itemcount ,"
 667  
                     + groupColumn
 668  
                     + ", MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax "
 669  
                     + "from " + tableName
 670  
                     + " where time_stamp > ? and time_stamp < ? group by "
 671  
                     + groupColumn + "  order by " + orderColumn + " " + ascDesc;
 672  
         } else
 673  
         {
 674  0
             query = "select count(*) as itemcount , MIN(ELAPSED_TIME) as amin,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax from "
 675  
                     + tableName
 676  
                     + " where time_stamp > ? and time_stamp < ? and status = 2";
 677  0
             query2 = "select count(*) as itemcount ,"
 678  
                     + groupColumn
 679  
                     + ", MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax "
 680  
                     + "from "
 681  
                     + tableName
 682  
                     + " where time_stamp > ? and time_stamp < ? and status = 2 group by "
 683  
                     + groupColumn + "  order by " + orderColumn + " " + ascDesc;
 684  
         }
 685  0
         Connection con = null;
 686  
         try
 687  
         {
 688  0
             con = ds.getConnection();
 689  0
             PreparedStatement pstmt = con.prepareStatement(query);
 690  0
             pstmt.setTimestamp(1, new Timestamp(start.getTime()));
 691  0
             pstmt.setTimestamp(2, new Timestamp(end.getTime()));
 692  0
             ResultSet rs = pstmt.executeQuery();
 693  0
             float denominator = 1.0f;
 694  0
             if (PortalStatistics.QUERY_TYPE_USER.equals(queryType))
 695  
             {
 696  0
                 denominator = 1000f * 60f; // this should convert from mS to
 697  
                                            // minutes
 698  
             }
 699  0
             if (rs.next())
 700  
             {
 701  0
                 as.setHitCount(rs.getInt("itemcount"));
 702  
 
 703  0
                 as.setMinProcessingTime(rs.getFloat("amin") / denominator);
 704  0
                 as.setAvgProcessingTime(rs.getFloat("aavg") / denominator);
 705  0
                 as.setMaxProcessingTime(rs.getFloat("amax") / denominator);
 706  
 
 707  
             }
 708  0
             PreparedStatement pstmt2 = con.prepareStatement(query2);
 709  0
             pstmt2.setTimestamp(1, new Timestamp(start.getTime()));
 710  0
             pstmt2.setTimestamp(2, new Timestamp(end.getTime()));
 711  0
             ResultSet rs2 = pstmt2.executeQuery();
 712  
 
 713  0
             int rowCount = 0;
 714  0
             int totalRows = 5;
 715  0
             String listsizeStr = criteria.getListsize();
 716  0
             int temp = -1;
 717  
             try 
 718  
             {
 719  0
                 temp = Integer.parseInt(listsizeStr);
 720  
             } 
 721  0
             catch (NumberFormatException e) 
 722  
             {
 723  0
             }
 724  0
             if(temp != -1) {
 725  0
                 totalRows = temp;
 726  
             }
 727  
             
 728  0
             while ((rs2.next()) && (rowCount < totalRows))
 729  
             {
 730  0
                 Map row = new HashMap();
 731  0
                 row.put("count", "" + rs2.getInt("itemcount"));
 732  0
                 String col = rs2.getString(groupColumn);
 733  0
                 int maxColLen = 35;
 734  0
                 if (col != null)
 735  
                 {
 736  
 
 737  0
                     if (col.length() > maxColLen)
 738  
                     {
 739  0
                         col = col.substring(0, maxColLen);
 740  
                     }
 741  
                 }
 742  
 
 743  0
                 row.put("groupColumn", col);
 744  0
                 row.put("min", ""
 745  
                         + floatFormatter(rs2.getFloat("amin") / denominator));
 746  0
                 row.put("avg", ""
 747  
                         + floatFormatter(rs2.getFloat("aavg") / denominator));
 748  0
                 row.put("max", ""
 749  
                         + floatFormatter(rs2.getFloat("amax") / denominator));
 750  0
                 as.addRow(row);
 751  0
                 rowCount++;
 752  0
             }
 753  
 
 754  
         } 
 755  0
         catch (SQLException e)
 756  
         {
 757  0
             throw new InvalidCriteriaException(e.toString());
 758  
         }
 759  
         finally 
 760  
         {
 761  0
             try 
 762  
             {
 763  0
                 if(con != null) 
 764  
                 {
 765  0
                     con.close();
 766  
                 }
 767  
             } 
 768  0
             catch (SQLException e) 
 769  
             {
 770  0
                 logger.error("error releasing the connection",e);
 771  0
             }
 772  0
         }
 773  
 
 774  0
         return as;
 775  
     }
 776  
 
 777  
     protected String floatFormatter(float f)
 778  
     {
 779  
         // for now we'll just truncate as int
 780  0
         int f2 = new Float(f).class="keyword">intValue();
 781  0
         return Integer.toString(f2);
 782  
     }
 783  
 
 784  
     /*
 785  
      * (non-Javadoc)
 786  
      * 
 787  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getListOfLoggedInUsers()
 788  
      */
 789  
     public List getListOfLoggedInUsers()
 790  
     {
 791  0
         List list = new ArrayList();
 792  
 
 793  0
         synchronized (currentUsers)
 794  
         {
 795  0
             list.addAll(currentUsers.values());
 796  0
         }
 797  0
         return list;
 798  
     }
 799  
 
 800  
     /*
 801  
      * (non-Javadoc)
 802  
      * 
 803  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getNumberOfLoggedInUsers()
 804  
      */
 805  
     public int getNumberOfLoggedInUsers()
 806  
     {
 807  0
         return this.currentUserCount;
 808  
     }
 809  
     
 810  
 
 811  
     /**
 812  
      * @see org.apache.jetspeed.statistics.PortalStatistics#forceFlush()
 813  
      */
 814  
     public void forceFlush()
 815  
     {
 816  0
         if (pageBatch != null)
 817  
         {
 818  0
             this.pageBatch.flush();
 819  
         }
 820  0
         if (portletBatch != null)
 821  
         {
 822  0
             this.portletBatch.flush();
 823  
         }
 824  0
         if (userBatch != null)
 825  
         {
 826  0
             this.userBatch.flush();
 827  
         }
 828  0
     }
 829  
     
 830  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.