Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2009 eXo Platform SAS. This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
 
 package org.exoplatform.groovyscript.text;
 
 import java.util.List;
 import java.util.Map;
 
Created by The eXo Platform SAS Author : tam.nguyen tamndrok@gmail.com Mar 17, 2009
 
 
 @ManagedDescription("Template statistic service")
 @NameTemplate({ @Property(key = "view", value = "portal"), @Property(key = "service", value = "statistic"),
         @Property(key = "type", value = "template") })
 @RESTEndpoint(path = "templatestatistics")
 public class TemplateStatisticService {
 
 
     private final String ASC = "ASC";
 
     private final String DESC = "DESC";
 
     public TemplateStatisticService() {
     }
 
     public TemplateStatistic findTemplateStatistic(String name) {
         if (name == null || name.length() == 0) {
             throw new IllegalArgumentException("Parameter 'templateid' is required.");
         } else {
             TemplateStatistic result = .get(name);
             if (result == null) {
                 /* Try to prevent a potential XSS */
                 String safeName = name.replaceAll("[^a-zA-Z0-9_\\-\\./]+""");
                 throw new IllegalArgumentException("There is no such template with templateid '"safeName +"'.");
             } else {
                 return result;
             }
         }
     }
 
     /*
      * get TemplateStatistic by name, if TemplateStatistic isn't exits, create a new one.
      */
     public TemplateStatistic getTemplateStatistic(String name) {
         TemplateStatistic app = .get(name);
         if (app == null) {
             app = new TemplateStatistic(name);
             .put(nameapp);
         }
         return app;
     }
 
     /*
      * returns a list of templateId sorted alphabetically
      */
     @Managed
     @ManagedDescription("The list of template identifiers sorted alphabetically")
     public String[] getTemplateList() {
         String[] app = new String[list.size()];
         int index = 0;
         for (Iterator<Entry<StringTemplateStatistic>> it = list.iterator(); it.hasNext();) {
             Map.Entry<StringTemplateStatisticentry = it.next();
            app[index] = entry.getKey();
            index++;
        }
        return app;
    }
    /*
     * return max time of an specify template
     */
    @Managed
    @ManagedDescription("The maximum rendering time of a specified template in seconds")
    public double getMaxTime(@ManagedDescription("The template id") @ManagedName("templateId"String name) {
        TemplateStatistic app = findTemplateStatistic(name);
        return toSeconds(app.getMaxTime());
    }
    /*
     * return min time of an specify template
     */
    @Managed
    @ManagedDescription("The minimum rendering time of a specified template in seconds")
    public double getMinTime(@ManagedDescription("The template id") @ManagedName("templateId"String name) {
        TemplateStatistic app = findTemplateStatistic(name);
        return toSeconds(app.getMinTime());
    }
    /*
     * return count of an specify template
     */
    @Managed
    @ManagedDescription("The rendering count of a specified template")
    public long getExecutionCount(@ManagedDescription("The template id") @ManagedName("templateId"String name) {
        TemplateStatistic app = findTemplateStatistic(name);
        return app.executionCount();
    }
    /*
     * return average time of an specify template
     */
    @Managed
    @ManagedDescription("The average rendering time of a specified template in seconds")
    public double getAverageTime(@ManagedDescription("The template id") @ManagedName("templateId"String name) {
        TemplateStatistic app = findTemplateStatistic(name);
        return toSeconds(app.getAverageTime());
    }
    /*
     * returns 10 slowest template
     */
    @Managed
    @ManagedDescription("The list of the 10 slowest templates")
    public String[] getSlowestTemplates() {
        Map<StringDoubleapplication = new HashMap<StringDouble>();
        for (Map.Entry<StringTemplateStatisticentry : .entrySet()) {
            String url = entry.getKey();
            application.put(urlgetAverageTime(url));
        }
        return sort(application);
    }
    /*
     * returns 10 slowest template
     */
    @Managed
    @ManagedDescription("The list of the 10 most executed templates")
    public String[] getMostExecutedTemplates() {
        Map<StringLongapplication = new HashMap<StringLong>();
        for (Map.Entry<StringTemplateStatisticentry : .entrySet()) {
            String url = entry.getKey();
            application.put(urlgetExecutionCount(url));
        }
        return sort(application);
    }
    /*
     * returns 10 fastest template
     */
    @Managed
    @ManagedDescription("The list of the 10 fastest templates")
    public String[] getFastestTemplates() {
        Map<StringDoubleapplication = new HashMap<StringDouble>();
        for (Map.Entry<StringTemplateStatisticentry : .entrySet()) {
            String url = entry.getKey();
            application.put(urlgetAverageTime(url));
        }
        return sort(application);
    }
    private <T extends Comparable<T>> String[] sort(Map<String, T> sourcefinal String order) {
        String[] app = new String[10];
        List<Map.Entry<String, T>> list = new ArrayList<Map.Entry<String, T>>(source.entrySet());
        Collections.sort(listnew Comparator<Map.Entry<String, T>>() {
            public int compare(Map.Entry<String, T> o1Map.Entry<String, T> o2) {
                T value1 = o1.getValue();
                T value2 = o2.getValue();
                if (.equals(order)) {
                    T tmp = value1;
                    value1 = value2;
                    value2 = tmp;
                }
                return value1.compareTo(value2);
            }
        });
        int index = 0;
        for (Map.Entry<String, T> entry : list) {
            app[index] = entry.getKey();
            index++;
            if (index >= app.length) {
                break;
            }
        }
        return app;
    }
    private double toSeconds(double value) {
        return value == -1 ? -1 : value / 1000D;
    }
New to GrepCode? Check out our FAQ X