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
                 portletBatch = new BatchedPortletStatistics(ds,
 212  
                         this.maxRecordToFlush_Portlet,
 213  
                         this.maxTimeMsToFlush_Portlet, "portletLogBatcher");
 214  0
                 portletBatch.startThread();
 215  
             }
 216  0
             portletBatch.addStatistic(record);
 217  
 
 218  
         }
 219  0
         if (record instanceof PageLogRecord)
 220  
         {
 221  0
             if (pageBatch == null)
 222  
             {
 223  0
                 pageBatch = new BatchedPageStatistics(ds,
 224  
                         this.maxRecordToFlush_Page, class="keyword">this.maxTimeMsToFlush_Page,
 225  
                         "pageLogBatcher");
 226  0
                 pageBatch.startThread();
 227  
             }
 228  0
             pageBatch.addStatistic(record);
 229  
 
 230  
         }
 231  0
         if (record instanceof UserLogRecord)
 232  
         {
 233  0
             if (userBatch == null)
 234  
             {
 235  0
                 userBatch = new BatchedUserStatistics(ds,
 236  
                         this.maxRecordToFlush_User, class="keyword">this.maxTimeMsToFlush_User,
 237  
                         "userLogBatcher");
 238  0
                 userBatch.startThread();
 239  
             }
 240  0
             userBatch.addStatistic(record);
 241  
 
 242  
         }
 243  0
     }
 244  
 
 245  
     protected void saveAccessToCLF(LogRecord record)
 246  
     {
 247  0
         Object[] args =  {""};
 248  0
         String logMessage = "";
 249  0
         if (record instanceof PortletLogRecord)
 250  
         {
 251  0
             PortletLogRecord rec = (PortletLogRecord) record;
 252  0
             Object[] args1 =
 253  
             { rec.getIpAddress(), "-", rec.getUserName(), rec.getTimeStamp(),
 254  
                     rec.getLogType(), formatter.format(rec.getTimeStamp()),
 255  
                     rec.getPortletName(),
 256  
                     new Integer(rec.getStatus()).toString(),
 257  
                     new Long(rec.getMsElapsedTime())};
 258  0
             args = args1;
 259  0
             logMessage = MessageFormat.format(portletLogFormat, args)
 260  
                     .toString();
 261  
         }
 262  0
         if (record instanceof PageLogRecord)
 263  
         {
 264  0
             PageLogRecord rec = (PageLogRecord) record;
 265  0
             Object[] args1 =
 266  
             { rec.getIpAddress(), "-", rec.getUserName(), rec.getTimeStamp(),
 267  
                     rec.getLogType(), formatter.format(rec.getTimeStamp()),
 268  
                     new Integer(rec.getStatus()).toString(),
 269  
                     new Long(rec.getMsElapsedTime())};
 270  0
             args = args1;
 271  0
             logMessage = MessageFormat.format(pageLogFormat, args).toString();
 272  
         }
 273  0
         if (record instanceof UserLogRecord)
 274  
         {
 275  0
             UserLogRecord rec = (UserLogRecord) record;
 276  0
             Object[] args1 =
 277  
             { rec.getIpAddress(), "-", rec.getUserName(), rec.getTimeStamp(),
 278  
                     rec.getLogType(), formatter.format(rec.getTimeStamp()),
 279  
                     new Integer(rec.getStatus()).toString(),
 280  
                     new Long(rec.getMsElapsedTime())};
 281  0
             args = args1;
 282  0
             logMessage = MessageFormat.format(logoutLogFormat, args).toString();
 283  
         }
 284  0
         logger.info(logMessage);
 285  0
     }
 286  
 
 287  
     public void logPageAccess(RequestContext request, String statusCode,
 288  
             long msElapsedTime)
 289  
     {
 290  
         try
 291  
         {
 292  0
             HttpServletRequest req = request.getRequest();
 293  0
             Principal principal = req.getUserPrincipal();
 294  0
             String userName = (principal != null) ? principal.getName()
 295  
                     : "guest";
 296  0
             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
 297  0
             PageLogRecord record = new PageLogRecord();
 298  
 
 299  0
             record.setUserName(userName);
 300  0
             record.setIpAddress(req.getRemoteAddr());
 301  0
             ContentPage cp = request.getPage();
 302  0
             if (cp != null)
 303  
             {
 304  0
                 if (cp.getPath() != null)
 305  
                 {
 306  0
                     record.setPagePath(cp.getPath());
 307  
                 }
 308  
             }
 309  0
             record.setStatus(Integer.parseInt(statusCode));
 310  0
             record.setTimeStamp(timestamp);
 311  0
             record.setMsElapsedTime(msElapsedTime);
 312  
 
 313  0
             if (logToCLF)
 314  
             {
 315  0
                 saveAccessToCLF(record);
 316  
             }
 317  0
             if (logToDatabase)
 318  
             {
 319  0
                 storeAccessToStats(record);
 320  
             }
 321  
 
 322  0
         } catch (Exception e)
 323  
         {
 324  0
             logger.error("Exception", e);
 325  0
         }
 326  0
     }
 327  
 
 328  
     public void logUserLogout(String ipAddress, String userName,
 329  
             long msSessionLength)
 330  
     {
 331  
         try
 332  
         {
 333  
 
 334  0
             if (userName == null)
 335  
             {
 336  0
                 userName = "guest";
 337  
             }
 338  
 
 339  0
             if (!"guest".equals(userName))
 340  
             {
 341  0
                 synchronized (currentUsers)
 342  
                 {
 343  0
                 	UserStats userStats = null;
 344  
                 	
 345  0
                 	Map users = (Map)currentUsers.get(userName);                	
 346  0
                 	if(users != null && users.size() > 0)
 347  
                 	{
 348  0
                 		userStats = (UserStats) users.get(ipAddress);                		
 349  
                 	}                	
 350  
             	
 351  0
                 	if(userStats != null)
 352  
                     {
 353  
                     	// only decrement if user has been logged in
 354  0
                     	currentUserCount = currentUserCount - 1;
 355  
                         
 356  0
                     	userStats.setNumberOfSession(userStats
 357  
                                 .getNumberOfSessions() - 1);                    
 358  0
                         if (userStats.getNumberOfSessions() <= 0)
 359  
                         {
 360  0
                         	users.remove(ipAddress);
 361  0
                             currentUsers.put(userName, users);
 362  
                         }
 363  
                     }
 364  0
                 }
 365  
             }
 366  
 
 367  0
             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
 368  0
             UserLogRecord record = new UserLogRecord();
 369  
 
 370  0
             record.setUserName(userName);
 371  0
             record.setIpAddress(ipAddress);
 372  0
             record.setStatus(STATUS_LOGGED_OUT);
 373  0
             record.setTimeStamp(timestamp);
 374  0
             record.setMsElapsedTime(msSessionLength);
 375  
 
 376  0
             if (logToCLF)
 377  
             {
 378  0
                 saveAccessToCLF(record);
 379  
             }
 380  0
             if (logToDatabase)
 381  
             {
 382  0
                 storeAccessToStats(record);
 383  
             }
 384  
 
 385  0
         } catch (Exception e)
 386  
         {
 387  0
             logger.error("Exception", e);
 388  0
         }
 389  0
     }
 390  
 
 391  
     /*
 392  
      * (non-Javadoc)
 393  
      * 
 394  
      * @see org.apache.jetspeed.statistics.PortalStatistics#logUserLogin(org.apache.jetspeed.request.RequestContext,
 395  
      *      long)
 396  
      */
 397  
     public void logUserLogin(RequestContext request, long msElapsedLoginTime)
 398  
     {
 399  
         try
 400  
         {
 401  0
             HttpServletRequest req = request.getRequest();
 402  0
             Principal principal = req.getUserPrincipal();
 403  0
             String userName = (principal != null) ? principal.getName()
 404  
                     : "guest";
 405  0
             String ipAddress = req.getRemoteAddr();
 406  0
             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
 407  0
             UserLogRecord record = new UserLogRecord();
 408  
 
 409  0
             if (!"guest".equals(userName))
 410  
             {
 411  0
                 currentUserCount = currentUserCount + 1;
 412  
 
 413  0
                 synchronized (currentUsers)
 414  
                 {
 415  
                 	
 416  0
                 	UserStats userStats = null;
 417  
                 	
 418  0
                 	Map users = (Map)currentUsers.get(userName);                	
 419  0
                 	if(users != null && users.size() > 0)
 420  
                 	{
 421  0
                 		userStats = (UserStats) users.get(ipAddress);                		
 422  
                 	}
 423  
                 	else
 424  
                 	{
 425  0
                 		users = new TreeMap();
 426  
                 	}
 427  
                 	
 428  0
                 	if(userStats == null)
 429  
                     {
 430  0
                         userStats = new UserStatsImpl();
 431  0
                         userStats.setNumberOfSession(0);
 432  0
                         userStats.setUsername(userName);
 433  0
                         userStats.setInetAddressFromIp(ipAddress);                        
 434  
                     }
 435  
                     
 436  0
                     userStats.setNumberOfSession(userStats
 437  
                             .getNumberOfSessions() + 1);
 438  0
                     users.put(ipAddress, userStats);
 439  0
             		currentUsers.put(userName, users);
 440  0
                 }
 441  
             }
 442  
 
 443  0
             record.setUserName(userName);
 444  0
             record.setIpAddress(ipAddress);
 445  0
             record.setStatus(STATUS_LOGGED_IN);
 446  0
             record.setTimeStamp(timestamp);
 447  0
             record.setMsElapsedTime(msElapsedLoginTime);
 448  
 
 449  0
             if (logToCLF)
 450  
             {
 451  0
                 saveAccessToCLF(record);
 452  
             }
 453  0
             if (logToDatabase)
 454  
             {
 455  0
                 storeAccessToStats(record);
 456  
             }
 457  
 
 458  0
         } catch (Exception e)
 459  
         {
 460  0
             logger.error("Exception", e);
 461  0
         }
 462  
 
 463  0
     }
 464  
 
 465  
     /**
 466  
      * @see org.springframework.beans.factory.DisposableBean#destroy()
 467  
      */
 468  
     public void springDestroy()
 469  
     {
 470  0
         if (portletBatch != null)
 471  
         {
 472  0
             portletBatch.tellThreadToStop();
 473  0
             synchronized (portletBatch.thread)
 474  
             {
 475  0
                 portletBatch.thread.notify();
 476  0
             }
 477  
 
 478  
         }
 479  0
         if (userBatch != null)
 480  
         {
 481  0
             userBatch.tellThreadToStop();
 482  0
             synchronized (userBatch.thread)
 483  
             {
 484  0
                 userBatch.thread.notify();
 485  0
             }
 486  
         }
 487  0
         if (pageBatch != null)
 488  
         {
 489  0
             pageBatch.tellThreadToStop();
 490  0
             synchronized (pageBatch.thread)
 491  
             {
 492  0
                 pageBatch.thread.notify();
 493  0
             }
 494  
         }
 495  
 
 496  0
         if ((this.currentUserCount != 0) && logger.isDebugEnabled())
 497  
         {
 498  0
             logger.debug("destroying while users are logged in");
 499  
         }
 500  0
         boolean done = false;
 501  0
         while (!done)
 502  
         {
 503  0
             done = true;
 504  0
             if (portletBatch != null)
 505  
             {
 506  0
                 if (!portletBatch.isDone())
 507  
                 {
 508  0
                     done = false;
 509  
                 }
 510  
             }
 511  0
             if (userBatch != null)
 512  
             {
 513  0
                 if (!userBatch.isDone())
 514  
                 {
 515  0
                     done = false;
 516  
                 }
 517  
             }
 518  0
             if (pageBatch != null)
 519  
             {
 520  0
                 if (!pageBatch.isDone())
 521  
                 {
 522  0
                     done = false;
 523  
                 }
 524  
             }
 525  
 
 526  
             try
 527  
             {
 528  0
                 Thread.sleep(2);
 529  0
             } catch (InterruptedException ie)
 530  
             {
 531  0
             }
 532  
         }
 533  
 
 534  0
     }
 535  
 
 536  
     /**
 537  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getNumberOfCurrentUsers()
 538  
      */
 539  
     public int getNumberOfCurrentUsers()
 540  
     {
 541  0
         return currentUserCount;
 542  
     }
 543  
 
 544  
     protected Date getStartDateFromPeriod(String period, Date end)
 545  
     {
 546  0
         GregorianCalendar gcEnd = new GregorianCalendar();
 547  0
         gcEnd.setTime(end);
 548  0
         if (period != null)
 549  
         {
 550  0
             if (period.endsWith("m"))
 551  
             {
 552  
                 // months
 553  0
                 String p = period.substring(0, period.length() - 1);
 554  0
                 int ret = Integer.parseInt(p);
 555  0
                 gcEnd.add(Calendar.MONTH, (ret * -1));
 556  0
             } else if (period.endsWith("d"))
 557  
             {
 558  
                 // days
 559  0
                 String p = period.substring(0, period.length() - 1);
 560  0
                 int ret = Integer.parseInt(p);
 561  0
                 gcEnd.add(Calendar.HOUR, (ret * 24 * -1));
 562  0
             } else if (period.endsWith("h"))
 563  
             {
 564  
                 // hours
 565  0
                 String p = period.substring(0, period.length() - 1);
 566  0
                 int ret = Integer.parseInt(p);
 567  0
                 gcEnd.add(Calendar.HOUR, (ret * -1));
 568  0
             } else if (period.equals("all"))
 569  
             {
 570  0
                 gcEnd = new GregorianCalendar();
 571  0
                 gcEnd.set(1968, 07, 15);
 572  
             } else
 573  
             {
 574  
                 // minutes
 575  0
                 int ret = Integer.parseInt(period);
 576  0
                 gcEnd.add(Calendar.MINUTE, (ret * -1));
 577  0
             }
 578  
         } else
 579  
         {
 580  0
             gcEnd = new GregorianCalendar();
 581  0
             gcEnd.set(1968, 07, 15);
 582  
 
 583  
         }
 584  0
         return gcEnd.getTime();
 585  
     }
 586  
 
 587  
     
 588  
     /* (non-Javadoc)
 589  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getDefaultEmptyStatisticsQueryCriteria()
 590  
      */
 591  
     public StatisticsQueryCriteria createStatisticsQueryCriteria()
 592  
     {
 593  0
         return new StatisticsQueryCriteriaImpl();
 594  
     }
 595  
 
 596  
     /* (non-Javadoc)
 597  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getDefaultEmptyAggregateStatistics()
 598  
      */
 599  
     public AggregateStatistics getDefaultEmptyAggregateStatistics()
 600  
     {
 601  0
         return new AggregateStatisticsImpl();
 602  
     }
 603  
 
 604  
     /**
 605  
      * @see org.apache.jetspeed.statistics.PortalStatistics#queryStatistics(org.apache.jetspeed.statistics.StatisticsQueryCriteria)
 606  
      */
 607  
     public AggregateStatistics queryStatistics(StatisticsQueryCriteria criteria)
 608  
             throws InvalidCriteriaException
 609  
     {
 610  0
         AggregateStatistics as = new AggregateStatisticsImpl();
 611  
         String query;
 612  
         String query2;
 613  
 
 614  
         String tableName;
 615  
         String groupColumn;
 616  
 
 617  0
         Date end = new Date();
 618  0
         Date start = getStartDateFromPeriod(criteria.getTimePeriod(), end);
 619  
 
 620  0
         String queryType = criteria.getQueryType();
 621  
         
 622  
 
 623  0
         if (PortalStatistics.QUERY_TYPE_USER.equals(queryType))
 624  
         {
 625  0
             tableName = "USER_STATISTICS";
 626  0
             groupColumn = "USER_NAME";
 627  0
         } else if (PortalStatistics.QUERY_TYPE_PORTLET.equals(queryType))
 628  
         {
 629  0
             tableName = "PORTLET_STATISTICS";
 630  0
             groupColumn = "PORTLET";
 631  0
         } else if (PortalStatistics.QUERY_TYPE_PAGE.equals(queryType))
 632  
         {
 633  0
             tableName = "PAGE_STATISTICS";
 634  0
             groupColumn = "PAGE";
 635  
         } else
 636  
         {
 637  0
             throw new InvalidCriteriaException(
 638  
                     " invalid queryType passed to queryStatistics");
 639  
         }
 640  0
         String orderColumn = "itemcount";
 641  
 
 642  0
         String ascDesc = "DESC";
 643  
 
 644  0
         if (!PortalStatistics.QUERY_TYPE_USER.equals(queryType))
 645  
         {
 646  0
             query = "select count(*) as itemcount , MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax from "
 647  
                     + tableName + " where time_stamp > ? and time_stamp < ?";
 648  0
             query2 = "select count(*) as itemcount ,"
 649  
                     + groupColumn
 650  
                     + ", MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax "
 651  
                     + "from " + tableName
 652  
                     + " where time_stamp > ? and time_stamp < ? group by "
 653  
                     + groupColumn + "  order by " + orderColumn + " " + ascDesc;
 654  
         } else
 655  
         {
 656  0
             query = "select count(*) as itemcount , MIN(ELAPSED_TIME) as amin,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax from "
 657  
                     + tableName
 658  
                     + " where time_stamp > ? and time_stamp < ? and status = 2";
 659  0
             query2 = "select count(*) as itemcount ,"
 660  
                     + groupColumn
 661  
                     + ", MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax "
 662  
                     + "from "
 663  
                     + tableName
 664  
                     + " where time_stamp > ? and time_stamp < ? and status = 2 group by "
 665  
                     + groupColumn + "  order by " + orderColumn + " " + ascDesc;
 666  
         }
 667  0
         Connection con = null;
 668  
         try
 669  
         {
 670  0
             con = ds.getConnection();
 671  0
             PreparedStatement pstmt = con.prepareStatement(query);
 672  0
             pstmt.setTimestamp(1, new Timestamp(start.getTime()));
 673  0
             pstmt.setTimestamp(2, new Timestamp(end.getTime()));
 674  0
             ResultSet rs = pstmt.executeQuery();
 675  0
             float denominator = 1.0f;
 676  0
             if (PortalStatistics.QUERY_TYPE_USER.equals(queryType))
 677  
             {
 678  0
                 denominator = 1000f * 60f; // this should convert from mS to
 679  
                                            // minutes
 680  
             }
 681  0
             if (rs.next())
 682  
             {
 683  0
                 as.setHitCount(rs.getInt("itemcount"));
 684  
 
 685  0
                 as.setMinProcessingTime(rs.getFloat("amin") / denominator);
 686  0
                 as.setAvgProcessingTime(rs.getFloat("aavg") / denominator);
 687  0
                 as.setMaxProcessingTime(rs.getFloat("amax") / denominator);
 688  
 
 689  
             }
 690  0
             PreparedStatement pstmt2 = con.prepareStatement(query2);
 691  0
             pstmt2.setTimestamp(1, new Timestamp(start.getTime()));
 692  0
             pstmt2.setTimestamp(2, new Timestamp(end.getTime()));
 693  0
             ResultSet rs2 = pstmt2.executeQuery();
 694  
 
 695  0
             int rowCount = 0;
 696  0
             int totalRows = 5;
 697  0
             String listsizeStr = criteria.getListsize();
 698  0
             int temp = -1;
 699  
             try 
 700  
             {
 701  0
                 temp = Integer.parseInt(listsizeStr);
 702  
             } 
 703  0
             catch (NumberFormatException e) 
 704  
             {
 705  0
             }
 706  0
             if(temp != -1) {
 707  0
                 totalRows = temp;
 708  
             }
 709  
             
 710  0
             while ((rs2.next()) && (rowCount < totalRows))
 711  
             {
 712  0
                 Map row = new HashMap();
 713  0
                 row.put("count", "" + rs2.getInt("itemcount"));
 714  0
                 String col = rs2.getString(groupColumn);
 715  0
                 int maxColLen = 35;
 716  0
                 if (col != null)
 717  
                 {
 718  
 
 719  0
                     if (col.length() > maxColLen)
 720  
                     {
 721  0
                         col = col.substring(0, maxColLen);
 722  
                     }
 723  
                 }
 724  
 
 725  0
                 row.put("groupColumn", col);
 726  0
                 row.put("min", ""
 727  
                         + floatFormatter(rs2.getFloat("amin") / denominator));
 728  0
                 row.put("avg", ""
 729  
                         + floatFormatter(rs2.getFloat("aavg") / denominator));
 730  0
                 row.put("max", ""
 731  
                         + floatFormatter(rs2.getFloat("amax") / denominator));
 732  0
                 as.addRow(row);
 733  0
                 rowCount++;
 734  0
             }
 735  
 
 736  
         } 
 737  0
         catch (SQLException e)
 738  
         {
 739  0
             throw new InvalidCriteriaException(e.toString());
 740  
         }
 741  
         finally 
 742  
         {
 743  0
             try 
 744  
             {
 745  0
                 if(con != null) 
 746  
                 {
 747  0
                     con.close();
 748  
                 }
 749  
             } 
 750  0
             catch (SQLException e) 
 751  
             {
 752  0
                 logger.error("error releasing the connection",e);
 753  0
             }
 754  0
         }
 755  
 
 756  0
         return as;
 757  
     }
 758  
 
 759  
     protected String floatFormatter(float f)
 760  
     {
 761  
         // for now we'll just truncate as int
 762  0
         int f2 = new Float(f).class="keyword">intValue();
 763  0
         return Integer.toString(f2);
 764  
     }
 765  
 
 766  
     /*
 767  
      * (non-Javadoc)
 768  
      * 
 769  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getListOfLoggedInUsers()
 770  
      */
 771  
     public List getListOfLoggedInUsers()
 772  
     {
 773  0
         List list = new ArrayList();
 774  
 
 775  0
         synchronized (currentUsers)
 776  
         {
 777  0
             list.addAll(currentUsers.values());
 778  0
         }
 779  0
         return list;
 780  
     }
 781  
 
 782  
     /*
 783  
      * (non-Javadoc)
 784  
      * 
 785  
      * @see org.apache.jetspeed.statistics.PortalStatistics#getNumberOfLoggedInUsers()
 786  
      */
 787  
     public int getNumberOfLoggedInUsers()
 788  
     {
 789  0
         return this.currentUserCount;
 790  
     }
 791  
     
 792  
 
 793  
     /**
 794  
      * @see org.apache.jetspeed.statistics.PortalStatistics#forceFlush()
 795  
      */
 796  
     public void forceFlush()
 797  
     {
 798  0
         if (pageBatch != null)
 799  
         {
 800  0
             this.pageBatch.flush();
 801  
         }
 802  0
         if (portletBatch != null)
 803  
         {
 804  0
             this.portletBatch.flush();
 805  
         }
 806  0
         if (userBatch != null)
 807  
         {
 808  0
             this.userBatch.flush();
 809  
         }
 810  0
     }
 811  
     
 812  
 }

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