1 17 18 package org.netbeans.server.uihandler.statistics; 19 20 import javax.servlet.jsp.PageContext ; 21 import org.netbeans.server.uihandler.*; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Map.Entry; 26 import java.util.Set ; 27 import java.util.TreeMap ; 28 import java.util.logging.LogRecord ; 29 import java.util.logging.Logger ; 30 import org.netbeans.lib.uihandler.ProjectOp; 31 import org.netbeans.server.uihandler.statistics.ProjectTypes.Counts; 32 33 37 public final class ProjectTypes extends Statistics<ProjectTypes.Counts> { 38 static final Logger LOG = Logger.getLogger(ProjectTypes.class.getName()); 39 40 public ProjectTypes() { 41 super("ProjectTypes"); 42 } 43 44 protected Counts newData() { 45 return Counts.EMPTY; 46 } 47 48 protected Counts process(LogRecord rec) { 49 ProjectOp projectOp = ProjectOp.valueOf(rec); 50 if (projectOp != null) { 51 return new Counts(projectOp); 52 } else { 53 return Counts.EMPTY; 54 } 55 } 56 57 protected Counts finishSessionUpload(String userId, int sessionNumber, 58 boolean initialParse, Counts d) { 59 return d; 60 } 61 62 protected Counts join(Counts one, Counts two) { 63 if (one.counts.isEmpty()) { 64 return two; 65 } 66 if (two.counts.isEmpty()) { 67 return one; 68 } 69 Counts join = new Counts(); 70 join.add(one); 71 join.add(two); 72 return join; 73 } 74 75 @Override 76 protected void registerPageContext(PageContext page, String name, Counts data) { 77 int sum = 0; 78 for (int i : data.counts.values()) { 79 sum += i; 80 } 81 82 int others = 0; 83 TreeMap <String ,Object > percentages = new TreeMap <String ,Object >(); 84 for (Map.Entry <String , Integer > entry : data.getUsages()) { 85 if (entry.getValue() < sum / 20) { 86 others += entry.getValue(); 87 } else { 88 int percent = entry.getValue() * 100 / sum; 89 percentages.put(entry.getKey(), percent); 90 } 91 } 92 93 if (others > 0) { 94 int percent = others * 100 / sum; 95 percentages.put("Others", percent); 96 } 97 98 if (!percentages.isEmpty()) { 99 page.setAttribute(name, percentages.entrySet()); 100 } 101 } 102 103 104 105 public static final class Counts { 106 static final Counts EMPTY = new Counts(); 107 108 final Map <String ,Integer > counts = new TreeMap <String , Integer >(); 109 110 Counts() { 111 } 112 113 Counts(ProjectOp projectOp) { 114 String name = projectOp.getProjectDisplayName(); 115 if (name.endsWith("Project")) { 116 name = name.substring(0, name.length() - 7); 117 } 118 counts.put(name, Math.abs(projectOp.getDelta())); 119 } 120 121 public final Set <Map.Entry <String ,Integer >> getUsages() { 122 Set <Entry<String , Integer >> t = counts.entrySet(); 123 return Collections.unmodifiableSet(t); 124 } 125 126 final void add(Counts toAdd) { 127 for (Map.Entry <String ,Integer > entry : toAdd.counts.entrySet()) { 128 Integer prev = counts.get(entry.getKey()); 129 if (prev == null) { 130 counts.put(entry.getKey(), entry.getValue()); 131 } else { 132 counts.put(entry.getKey(), entry.getValue() + prev); 133 } 134 } 135 } 136 } } 138 | Popular Tags |