logger/uihandlerserver/src/java/org/netbeans/server/uihandler/statistics/ExceptionReports.java
author jsedek@netbeans.org
Fri, 23 Nov 2007 08:15:20 +0000
changeset 2717 91163b406a41
parent 2556 6330ec582bbd
child 2729 df02898aa831
permissions -rw-r--r--
show more components, sort by number of reports
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
     5  *
     6  * The contents of this file are subject to the terms of either the GNU
     7  * General Public License Version 2 only ("GPL") or the Common
     8  * Development and Distribution License("CDDL") (collectively, the
     9  * "License"). You may not use this file except in compliance with the
    10  * License. You can obtain a copy of the License at
    11  * http://www.netbeans.org/cddl-gplv2.html
    12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    13  * specific language governing permissions and limitations under the
    14  * License.  When distributing the software, include this License Header
    15  * Notice in each file and include the License file at
    16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    17  * particular file as subject to the "Classpath" exception as provided
    18  * by Sun in the GPL Version 2 section of the License file that
    19  * accompanied this code. If applicable, add the following below the
    20  * License Header, with the fields enclosed by brackets [] replaced by
    21  * your own identifying information:
    22  * "Portions Copyrighted [year] [name of copyright owner]"
    23  *
    24  * Contributor(s):
    25  *
    26  * The Original Software is NetBeans. The Initial Developer of the Original
    27  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    28  * Microsystems, Inc. All Rights Reserved.
    29  *
    30  * If you wish your version of this file to be governed by only the CDDL
    31  * or only the GPL Version 2, indicate your decision by adding
    32  * "[Contributor] elects to include this software in this distribution
    33  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    34  * single choice of license, a recipient has the option to distribute
    35  * your version of this file under either the CDDL, the GPL Version 2 or
    36  * to extend the choice of license to its licensees as provided above.
    37  * However, if you add GPL Version 2 code and therefore, elected the GPL
    38  * Version 2 license, then the option applies only if the new code is
    39  * made subject to such option by the copyright holder.
    40  */
    41 
    42 package org.netbeans.server.uihandler.statistics;
    43 
    44 import java.util.ArrayList;
    45 import java.util.Collections;
    46 import java.util.Comparator;
    47 import java.util.Iterator;
    48 import java.util.List;
    49 import java.util.Map;
    50 import java.util.TreeMap;
    51 import java.util.logging.Level;
    52 import java.util.logging.LogRecord;
    53 import java.util.logging.Logger;
    54 import java.util.prefs.BackingStoreException;
    55 import java.util.prefs.Preferences;
    56 import javax.servlet.jsp.PageContext;
    57 import org.netbeans.modules.exceptions.entity.Exceptions;
    58 import org.netbeans.modules.exceptions.utils.PersistenceUtils;
    59 import org.netbeans.server.uihandler.Statistics;
    60 import org.netbeans.server.uihandler.Utils;
    61 import org.netbeans.server.uihandler.statistics.ExceptionReports.Data;
    62 /**
    63  *
    64  * @author Jindrich Sedek
    65  */
    66 
    67 public class ExceptionReports extends Statistics<ExceptionReports.Data>{
    68     private static Logger LOG = Logger.getLogger(ExceptionReports.class.getName());
    69     public ExceptionReports() {
    70         super("ExceptionReports");
    71     }
    72     
    73     protected Data newData() {
    74         return null;
    75     }
    76     
    77     protected Data process(LogRecord rec) {
    78         return null;
    79     }
    80     
    81     protected Data join(Data one, Data two) {
    82         return null;
    83     }
    84     
    85     protected Data finishSessionUpload(String userId, int sessionNumber, boolean initialParse, Data data) {
    86         return null;
    87     }
    88     
    89     @Override
    90     protected void registerPageContext(PageContext page, String name, Data data) {
    91         Data dat = new Data();
    92         Map<String, Integer> result = new TreeMap<String, Integer>();
    93         try {
    94             PersistenceUtils pUtils = Utils.getPersistenceUtils();
    95             dat.globalReportsNumber = pUtils.count(Exceptions.class);
    96             List<String> list = pUtils.getDistinct(Exceptions.class, "component");
    97             Iterator<String> iter = list.iterator();
    98             int minimumToShow = dat.globalReportsNumber / 50; // 2%
    99             while (iter.hasNext()){
   100                 //List<String> list =  iter.next();
   101                 String str = iter.next();
   102                 int count = pUtils.count(Exceptions.class, "entity.component = '" + str+"'");
   103                 if ((str != null)&&(count > minimumToShow)) result.put(str, count);
   104             }
   105         } catch (Exception ex) {
   106             // requires database and that is not present
   107             LOG.log(Level.WARNING, ex.getMessage(), ex);
   108         }
   109         dat.components = result;
   110         super.registerPageContext(page, name, dat);
   111     }
   112     
   113     public static final class Data{
   114         int globalReportsNumber;
   115         Map<String, Integer> components;
   116         
   117         public Data(){
   118             globalReportsNumber = 0;
   119         }
   120         
   121         public Data(int global){
   122             globalReportsNumber = global;
   123         }
   124         
   125         public int getGlobalReportsNumber(){
   126             return globalReportsNumber;
   127         }
   128         
   129         public Map<String, Integer> getComponents(){
   130             if (components!= null) return components;
   131             else return Collections.emptyMap();
   132         }
   133         
   134         public List<String> getSortedKeys(){
   135             List<String> list = new ArrayList(components.keySet());
   136             Collections.sort(list, new ReportsComparator());
   137             return list;
   138         }
   139         
   140         private final class ReportsComparator implements Comparator<String>{
   141 
   142             public int compare(String o1, String o2) {
   143                 return components.get(o2).compareTo(components.get(o1));
   144             }
   145             
   146         }
   147     }
   148 
   149     protected void write(Preferences pref, Data d) throws BackingStoreException {
   150         // data are saved in DB for this statistics
   151     }
   152 
   153     protected Data read(Preferences pref) throws BackingStoreException {
   154         return null; // nothing is needed to be done
   155     }
   156 }