Coverage report

  %line %branch
net.sf.infrared.web.util.ViewUtil
0% 
0% 

 1  
 /*
 2  
  *
 3  
  * Copyright 2005 Tavant Technologies and Contributors
 4  
  * 
 5  
  * Licensed under the Apache License, Version 2.0 (the "License")
 6  
  * you may not use this file except in compliance with the License.
 7  
  * 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  
  *
 19  
  * Original Author:  kaushal.kumar (Tavant Technologies)
 20  
  * Contributor(s):   prashant.nair;
 21  
  *
 22  
  */
 23  
 package net.sf.infrared.web.util;
 24  
 
 25  
 import net.sf.infrared.aspects.jdbc.SqlExecuteContext;
 26  
 import net.sf.infrared.aspects.jdbc.SqlPrepareContext;
 27  
 import net.sf.infrared.base.model.AggregateExecutionTime;
 28  
 import net.sf.infrared.base.model.ExecutionContext;
 29  
 import net.sf.infrared.base.model.LayerTime;
 30  
 import net.sf.infrared.base.util.LoggingFactory;
 31  
 import net.sf.infrared.base.util.TreeNode;
 32  
 import net.sf.infrared.web.util.BeanComparator;
 33  
 
 34  
 import org.apache.log4j.Logger;
 35  
 
 36  
 import javax.servlet.http.HttpServletRequest;
 37  
 import javax.servlet.http.HttpSession;
 38  
 
 39  
 import java.util.*;
 40  
 
 41  
 /**
 42  
  * Utility methods used by InfraRED web GUI.
 43  
  * 
 44  
  */
 45  0
 public class ViewUtil {
 46  0
     private static BeanComparator nameComparator = new BeanComparator("getName", true);
 47  
 
 48  0
     private static BeanComparator avgTimeComparator = new BeanComparator("getAverageInclusiveTime", true);
 49  
 
 50  0
     private static BeanComparator totalTimeComparator = new BeanComparator("getTotalInclusiveTime", true);
 51  
 
 52  0
     private static BeanComparator maxTimeComparator = new BeanComparator("getMaxInclusiveTime", true);
 53  
 
 54  0
     private static BeanComparator minTimeComparator = new BeanComparator("getMinInclusiveTime", true);
 55  
 
 56  0
     private static BeanComparator lastExcTimeComparator = 
 57  
                                         new BeanComparator("getInclusiveLastExecutionTime", true);
 58  
 
 59  0
     private static BeanComparator countComparator = new BeanComparator("getExecutionCount", true);
 60  
 
 61  0
     private static BeanComparator firstExcTimeComparator = 
 62  
                                         new BeanComparator("getInclusiveFirstExecutionTime", true);
 63  
 
 64  0
     private static BeanComparator adjAvgTimeComparator = 
 65  
                                         new BeanComparator("getAdjAverageInclusiveTime",true);
 66  
 
 67  0
     private static BeanComparator avgTimeExclusiveComparator = 
 68  
                                         new BeanComparator("getAverageExclusiveTime", true);
 69  
 
 70  0
     private static BeanComparator totalTimeExclusiveComparator = 
 71  
                                         new BeanComparator("getTotalExclusiveTime", true);
 72  
 
 73  0
     private static BeanComparator maxTimeExclusiveComparator = 
 74  
                                         new BeanComparator("getMaxExclusiveTime", true);
 75  
 
 76  0
     private static BeanComparator minTimeExclusiveComparator = 
 77  
                                         new BeanComparator("getMinExclusiveTime", true);
 78  
 
 79  0
     private static BeanComparator lastExcTimeExclusiveComparator = 
 80  
                                         new BeanComparator("getExclusiveLastExecutionTime", true);
 81  
 
 82  0
     private static BeanComparator firstExcTimeExclusiveComparator = 
 83  
                                         new BeanComparator("getExclusiveFirstExecutionTime", true);
 84  
 
 85  0
     private static BeanComparator adjAvgTimeExclusiveComparator = 
 86  
                                         new BeanComparator("getAdjAverageExclusiveTime", true);
 87  
 
 88  0
     private static Logger logger = LoggingFactory.getLogger(ViewUtil.class.getName());
 89  
 
 90  
     /**
 91  
      * Gets the String format of the given double with two places after decimal
 92  
      * point
 93  
      * 
 94  
      * @param time
 95  
      * @return
 96  
      */
 97  
     public static String getFormattedTime(double time) {
 98  0
         return getFormattedTime(time, 2);
 99  
     }
 100  
 
 101  
     /**
 102  
      * Gets the String format of the given double with the given number of
 103  
      * integers after the decimal point.
 104  
      * 
 105  
      * @param time
 106  
      * @param precision
 107  
      *            the number of places after the decimal point.
 108  
      * @return
 109  
      */
 110  
 
 111  
     public static String getFormattedTime(double time, int precision) {
 112  0
         String formattedTime = (new Double(time)).toString();
 113  0
         return getFormattedTime(formattedTime, precision);
 114  
     }
 115  
 
 116  
     public static String getFormattedTime(String time, int precision) {
 117  0
         String formattedTime = (new Double(time)).toString();
 118  0
         if (formattedTime != null && formattedTime.indexOf(".") != -1) {
 119  0
             if (formattedTime.length() > (formattedTime.indexOf(".") + precision + 1)) {
 120  0
                 formattedTime = 
 121  
                     formattedTime.substring(0, formattedTime.indexOf(".") + precision+ 1);
 122  
             } 
 123  
             else {
 124  0
                 int numberOfZeorsToPad = 
 125  
                     (formattedTime.indexOf(".") + precision + 1)- formattedTime.length();
 126  0
                 for (int i = 0; i < numberOfZeorsToPad; i++) {
 127  0
                     formattedTime += "0";
 128  
                 }
 129  
             }
 130  
         }
 131  0
         return formattedTime;
 132  
     }
 133  
 
 134  
     /**
 135  
      * Sorts an array of TimeVo object in-place. The sort criteria is determined
 136  
      * by the sortBy and sortDir request parameters.
 137  
      * 
 138  
      * @param arr
 139  
      * @param request
 140  
      */
 141  
     public static void sort(AggregateExecutionTime[] arr, HttpServletRequest request) {
 142  0
         String sortBy = request.getParameter("sortBy");
 143  0
         String sortDir = request.getParameter("sortDir");
 144  0
         String exclusiveInclusiveMode = 
 145  
             (String) request.getSession().getAttribute("exclusiveInclusiveMode");
 146  
 
 147  0
         if (sortBy == null) {
 148  0
             sortBy = "adjAvg";
 149  0
             if (exclusiveInclusiveMode != null && exclusiveInclusiveMode.equals("exclusive")){
 150  0
                 sortBy = "adjAvgExclusive";
 151  
             }                
 152  
         }
 153  
         
 154  
 
 155  0
         if (sortDir == null) {
 156  0
             sortDir = "desc";
 157  
         }
 158  0
         boolean ascending = true;
 159  0
         if (sortDir.equals("desc")) {
 160  0
             ascending = false;
 161  
         }
 162  0
         sort(sortBy, ascending, arr);
 163  0
     }
 164  
 
 165  
 
 166  
     public static void sort(String sortBy, boolean ascending, AggregateExecutionTime[] arr) {
 167  0
         BeanComparator comparator = getComparator(sortBy);
 168  0
         comparator.setAscending(ascending);
 169  0
         Arrays.sort(arr, comparator);
 170  0
     }
 171  
 
 172  
     public static void sort(LayerTime[] layerTimes, String fieldName, boolean ascending) {
 173  0
         BeanComparator comparator = null;
 174  0
         if ("time".equals(fieldName)) {
 175  0
             comparator = new BeanComparator("getTime", ascending);
 176  
         } 
 177  0
         else if ("name".equals(fieldName)) {
 178  0
             comparator = new BeanComparator("getLayer", ascending);
 179  
         } 
 180  
         else {
 181  0
             comparator = new BeanComparator("getTime", ascending);
 182  
         }
 183  0
         Arrays.sort(layerTimes, comparator);
 184  0
     }
 185  
 
 186  
     public static AggregateExecutionTime[] getSummaryForALayer(PerformanceDataSnapshot perfData,
 187  
                                                                 String layer) {        
 188  0
         return perfData.getStats().getExecutionsInHierarchicalLayer(layer);
 189  
     }
 190  
 
 191  
     public static AggregateExecutionTime[] getSummaryForAbsoluteLayer(PerformanceDataSnapshot perfData,
 192  
                                                                 String layer) {        
 193  0
         return perfData.getStats().getExecutionsInAbsoluteLayer(layer);
 194  
     }
 195  
 
 196  
     public static AggregateExecutionTime[] getSummaryForALayer(HttpServletRequest request,
 197  
                                                                 HttpSession session, 
 198  
                                                                 String apiType, String layerType) {
 199  0
         AggregateExecutionTime[] vos = null;
 200  0
         PerformanceDataSnapshot perfData = 
 201  
                             (PerformanceDataSnapshot) session.getAttribute("perfData");
 202  
         
 203  0
         if(layerType.equals("abs")){
 204  0
             vos = getSummaryForAbsoluteLayer(perfData, apiType);
 205  
         }
 206  0
         else if(layerType.equals("hier")){
 207  0
             vos = getSummaryForALayer(perfData, apiType);
 208  
         }
 209  0
         sort(vos, request);
 210  0
         return vos;
 211  
     }
 212  
 
 213  
     public static AggregateExecutionTime [] getJDBCSummary(TreeNode node) {    	
 214  0
     	Map jdbcMap = new HashMap();
 215  0
     	getJDBCSummary(node, jdbcMap);
 216  
     	
 217  0
     	AggregateExecutionTime [] jdbcExecTime = new AggregateExecutionTime[jdbcMap.size()];    	    	
 218  0
     	jdbcMap.values().toArray(jdbcExecTime);
 219  
     	
 220  0
     	return jdbcExecTime;
 221  
     }
 222  
     
 223  
     private static void getJDBCSummary(TreeNode node, Map jdbcMap){
 224  0
         AggregateExecutionTime originalExecTime = (AggregateExecutionTime) node.getValue();
 225  
         
 226  
         // Needs to make modifications on a copy of this execTime, since the reference to  
 227  
         // the same is stored in the session.        
 228  0
         AggregateExecutionTime execTime = (AggregateExecutionTime) originalExecTime.clone();
 229  
 
 230  0
         if(execTime.getContext().getLayer().endsWith("JDBC")){
 231  0
         	AggregateExecutionTime jdbcTime = (AggregateExecutionTime)
 232  
         												jdbcMap.get(execTime.getContext());
 233  0
         	if(jdbcTime == null){
 234  0
         		jdbcMap.put(execTime.getContext(), execTime);
 235  
         	}
 236  
         	else{
 237  0
         		jdbcTime.merge(execTime);
 238  
         	}        	
 239  
         }
 240  0
         for (Iterator itr = node.getChildren().iterator(); itr.hasNext();) {
 241  0
             getJDBCSummary((TreeNode) itr.next(), jdbcMap);
 242  
         }    	
 243  
         
 244  0
     	return;
 245  
     }
 246  
     
 247  
     public static SqlStatistics [] getSqlStatistics(TreeNode node){
 248  0
     	Map sqlMap = new HashMap();
 249  0
     	getSqlStatistics(node, sqlMap);
 250  0
     	SqlStatistics [] sqlExecTime = new SqlStatistics[sqlMap.size()];
 251  
     	
 252  0
     	sqlMap.values().toArray(sqlExecTime);
 253  0
     	return sqlExecTime;
 254  
     }
 255  
     
 256  
     public static void getSqlStatistics(TreeNode node, Map sqlMap){
 257  0
     	AggregateExecutionTime originalSqlExec = (AggregateExecutionTime) node.getValue();
 258  
     	
 259  
         // Needs to make modifications on a copy of this sqlExec, since the reference to  
 260  
         // the same is stored in the session.
 261  0
     	AggregateExecutionTime sqlExec = (AggregateExecutionTime) originalSqlExec.clone();
 262  
     	    	
 263  0
     	if(sqlExec.getContext().getLayer().equals("SQL")){
 264  0
     		ExecutionContext sqlContext = sqlExec.getContext();
 265  0
     		String sql = sqlContext.getName();
 266  
     		
 267  0
     		SqlStatistics sqlStats = (SqlStatistics)sqlMap.get(sql);
 268  0
     		if(sqlStats == null){
 269  0
     			sqlStats = new SqlStatistics();
 270  0
     			sqlMap.put(sql, sqlStats);
 271  
     		}
 272  0
     		if(sqlContext instanceof SqlPrepareContext){
 273  0
     			sqlStats.mergePrepareTime(sqlExec);
 274  
     		}
 275  0
     		if(sqlContext instanceof SqlExecuteContext){
 276  0
     			sqlStats.mergeExecuteTime(sqlExec);
 277  
     		}    		
 278  
     	}
 279  0
         for (Iterator itr = node.getChildren().iterator(); itr.hasNext();) {
 280  0
         	getSqlStatistics((TreeNode) itr.next(), sqlMap);
 281  
         }    	
 282  
     	
 283  0
 		return;
 284  
     }
 285  
 
 286  
     private static BeanComparator getComparator(String sortBy) {
 287  0
         if (sortBy.equals("name") || sortBy.equals("nameExclusive")) {
 288  0
             return nameComparator;
 289  
         }
 290  0
         if (sortBy.equals("time")) {
 291  0
             return avgTimeComparator;
 292  
         }
 293  0
         if (sortBy.equals("count") || sortBy.equals("countExclusive")) {
 294  0
             return countComparator;
 295  
         }
 296  0
         if (sortBy.equals("totalTime")) {
 297  0
             return totalTimeComparator;
 298  
         }
 299  0
         if (sortBy.equals("maxTime")) {
 300  0
             return maxTimeComparator;
 301  
         }
 302  0
         if (sortBy.equals("minTime")) {
 303  0
             return minTimeComparator;
 304  
         }
 305  0
         if (sortBy.equals("lastExecTime")) {
 306  0
             return lastExcTimeComparator;
 307  
         }
 308  0
         if (sortBy.equals("first")) {
 309  0
             return firstExcTimeComparator;
 310  
         }
 311  
 
 312  0
         if (sortBy.equals("adjAvg")) {
 313  0
             return adjAvgTimeComparator;
 314  
         }
 315  
 
 316  0
         if (sortBy.equals("timeExclusive")) {
 317  0
             return avgTimeExclusiveComparator;
 318  
         }
 319  0
         if (sortBy.equals("totalTimeExclusive")) {
 320  0
             return totalTimeExclusiveComparator;
 321  
         }
 322  0
         if (sortBy.equals("maxTimeExclusive")) {
 323  0
             return maxTimeExclusiveComparator;
 324  
         }
 325  0
         if (sortBy.equals("minTimeExclusive")) {
 326  0
             return minTimeExclusiveComparator;
 327  
         }
 328  0
         if (sortBy.equals("lastExecTimeExclusive")) {
 329  0
             return lastExcTimeExclusiveComparator;
 330  
         }
 331  0
         if (sortBy.equals("firstExclusive")) {
 332  0
             return firstExcTimeExclusiveComparator;
 333  
         }
 334  0
         if (sortBy.equals("adjAvgExclusive")) {
 335  0
             return adjAvgTimeExclusiveComparator;
 336  
         }
 337  0
         return nameComparator;
 338  
     }
 339  
 
 340  
      public static SqlStatistics[] getTopNQueriesByExecutionTime(SqlStatistics[] sqlStatistics, 
 341  
                                                                  int n) {
 342  0
         Arrays.sort(sqlStatistics, new BeanComparator("getAvgExecuteTime", false));
 343  0
         if (n != 0) {
 344  0
             return getTopN(sqlStatistics, n);
 345  
         }
 346  0
         return sqlStatistics;
 347  
     }
 348  
     
 349  
      public static SqlStatistics[] getTopNQueriesByCount(SqlStatistics[] sqlStatistics, int n) {
 350  0
         Arrays.sort(sqlStatistics, new BeanComparator("getNoOfExecutes", false));
 351  0
         if (n != 0) {
 352  0
             return getTopN(sqlStatistics, n);
 353  
         }
 354  0
         return sqlStatistics;
 355  
     }
 356  
 
 357  
      private static SqlStatistics[] getTopN(SqlStatistics[] sqlStatistics, int n) {
 358  0
         SqlStatistics[] result = sqlStatistics;
 359  0
         if (sqlStatistics.length > n) {
 360  0
             SqlStatistics[] temp = new SqlStatistics[n];
 361  0
             System.arraycopy(sqlStatistics, 0, temp, 0, n);
 362  0
             result = temp;
 363  
         }
 364  0
         return result;
 365  
     }
 366  
 
 367  
     public static String extractMethodName(String name) {
 368  0
         int methodNameStart = name.lastIndexOf(".");
 369  0
         int classNameStart = name.lastIndexOf(".", methodNameStart - 1);
 370  0
         if (classNameStart == -1) {
 371  0
             return name;
 372  
         } else {
 373  0
             return name.substring(classNameStart + 1);
 374  
         }
 375  
     }
 376  
 
 377  
     public static String getSortByHrefInLayer(String sortBy, String sortDir, String currentSortBy,
 378  
                                                 String apiType, String exclusiveInclusiveMode) {
 379  0
         String correctedSortBy = sortBy;
 380  0
         if (exclusiveInclusiveMode.equals("exclusive")){
 381  0
             correctedSortBy = correctedSortBy.concat("Exclusive");
 382  
         }
 383  
             
 384  0
         if (sortDir == null && (sortBy.indexOf("adjAvg") != -1)){
 385  0
             return "perfData_layerSummaryAction.do?type=" + apiType + "&sortBy=" 
 386  
                     + correctedSortBy + "&sortDir=asc";            
 387  
         }
 388  
         
 389  0
         if (correctedSortBy.equals(currentSortBy)) {
 390  0
             if (sortDir.equals("desc")){
 391  0
                 return "perfData_layerSummaryAction.do?type=" + apiType + "&sortBy="
 392  
                         + correctedSortBy + "&sortDir=asc";                
 393  
             }
 394  
             else{
 395  0
                 return "perfData_layerSummaryAction.do?type=" + apiType + "&sortBy="
 396  
                         + correctedSortBy + "&sortDir=desc";                
 397  
             }
 398  
         } 
 399  
         else{
 400  0
             return "perfData_layerSummaryAction.do?type=" + apiType + "&sortBy=" 
 401  
                     + correctedSortBy + "&sortDir=desc";            
 402  
         }
 403  
     }
 404  
 
 405  
     public static String getSortByHrefInSummary(String sortBy, String sortDir, 
 406  
                                                 String currentSortBy) {
 407  0
         if (sortDir == null && sortBy.equals("time")){
 408  0
             return "perfData_summaryAction.do?sortBy=" + sortBy + "&sortDir=asc";
 409  
         }
 410  
             
 411  0
         if (sortBy.equals(currentSortBy)) {
 412  0
             if (sortDir.equals("desc")){
 413  0
                 return "perfData_summaryAction.do?sortBy=" + sortBy + "&sortDir=asc";
 414  
             }                
 415  
             else{
 416  0
                 return "perfData_summaryAction.do?sortBy=" + sortBy + "&sortDir=desc";
 417  
             }
 418  
                 
 419  
         } 
 420  
         else
 421  0
             return "perfData_summaryAction.do?sortBy=" + sortBy + "&sortDir=desc";
 422  
     }
 423  
 
 424  
     public static String getSortByHrefInAbsSummary(String sortBy, String sortDir, 
 425  
                                                                         String currentSortBy) {
 426  0
         if (sortDir == null && sortBy.equals("time")) {
 427  0
             return "perfData_summaryAction.do?sortByAbs=" + sortBy + "&sortDirAbs=asc";
 428  
         }
 429  
 
 430  0
         if (sortBy.equals(currentSortBy)) {
 431  0
             if (sortDir.equals("desc")) {
 432  0
                 return "perfData_summaryAction.do?sortByAbs=" + sortBy + "&sortDirAbs=asc";
 433  
             }
 434  
             else {
 435  0
                 return "perfData_summaryAction.do?sortByAbs=" + sortBy + "&sortDirAbs=desc";
 436  
             }
 437  
 
 438  
         }
 439  
         else
 440  0
             return "perfData_summaryAction.do?sortByAbs=" + sortBy + "&sortDirAbs=desc";
 441  
     }
 442  
 
 443  
     public static String getSortByIconInLayer(String sortBy, String sortDir, String currentSortBy,
 444  
                                                 String exclusiveInclusiveMode) {
 445  0
         String correctedSortBy = sortBy;
 446  0
         if (exclusiveInclusiveMode.equals("exclusive")){
 447  0
             correctedSortBy = correctedSortBy.concat("Exclusive");
 448  
         }
 449  
             
 450  0
         if (sortDir == null && (sortBy.indexOf("adjAvg") != -1)){
 451  0
             return "common/graphics/sort_dn.gif";
 452  
         }
 453  
             
 454  0
         if (correctedSortBy.equals(currentSortBy)) {
 455  0
             if (sortDir.equals("asc")){
 456  0
                 return "common/graphics/sort_up.gif";
 457  
             }                
 458  
             else{
 459  0
                 return "common/graphics/sort_dn.gif";
 460  
             }                
 461  
         } 
 462  
         else{
 463  0
             return "common/graphics/notselected.gif";
 464  
         }
 465  
             
 466  
     }
 467  
 
 468  
     public static String getSortByIconInSummary(String sortBy, String sortDir, 
 469  
                                                 String currentSortBy) {
 470  0
         if (sortDir == null && sortBy.equals("time")){
 471  0
             return "common/graphics/sort_dn.gif";
 472  
         }
 473  
             
 474  0
         if (sortBy.equals(currentSortBy)) {
 475  0
             if (sortDir.equals("asc")){
 476  0
                 return "common/graphics/sort_up.gif";
 477  
             }                
 478  
             else{
 479  0
                 return "common/graphics/sort_dn.gif";
 480  
             }               
 481  
         } 
 482  
         else{
 483  0
             return "common/graphics/notselected.gif";
 484  
         }
 485  
             
 486  
     }
 487  
 }

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