001 package jp.osdl.jbento2.report; 002 003 import java.io.BufferedWriter; 004 import java.io.File; 005 import java.io.FileWriter; 006 import java.util.ArrayList; 007 import java.util.Collections; 008 import java.util.Comparator; 009 import java.util.HashMap; 010 import java.util.HashSet; 011 import java.util.Iterator; 012 import java.util.List; 013 import java.util.Map; 014 import java.util.Set; 015 import java.util.regex.Matcher; 016 import java.util.regex.Pattern; 017 018 import jp.osdl.jbento2.PathUtils; 019 import jp.osdl.jbento2.RegexNameFileFilter; 020 021 import org.apache.commons.io.FileUtils; 022 import org.apache.commons.io.IOUtils; 023 import org.apache.commons.io.filefilter.FileFilterUtils; 024 import org.apache.velocity.Template; 025 import org.apache.velocity.VelocityContext; 026 import org.apache.velocity.app.Velocity; 027 028 public class MachineDetailBuilder extends AbstractReportBuilder { 029 public MachineDetailBuilder(File dataDir, File destDir, File destRootDir) { 030 super(dataDir, destDir, destRootDir); 031 } 032 033 public void execute() throws Exception { 034 buildSampleDetail(); 035 } 036 037 private void buildSampleDetail() throws Exception { 038 039 List machineNames = getMachineNames(); 040 041 Iterator ite = machineNames.iterator(); 042 043 while (ite.hasNext()) { 044 String machineName = (String) ite.next(); 045 046 VelocityContext context = new VelocityContext(); 047 048 context.put("title", "Resource of " + machineName); 049 context.put("relativeRoot", getRelativeRoot()); 050 context.put("relativePath", getRelativePath()); 051 052 Map resources = new HashMap(); 053 054 resources.put("cpu", getResourceImages(machineName 055 + ".*\\.[uP]\\..*\\.png")); 056 resources.put("mem", 057 getResourceImages(machineName + ".*\\.r\\.png")); 058 resources.put("swap", getResourceImages(machineName 059 + ".*\\.W\\..*\\.png")); 060 resources.put("net", getResourceImagesAsTag(machineName 061 + ".*\\.n(.*)\\.png")); 062 resources.put("gc", getResourceImages(machineName 063 + ".*\\.gc\\..*\\.png")); 064 065 context.put("resources", resources); 066 067 BufferedWriter writer = null; 068 try { 069 Template template = Velocity 070 .getTemplate("jp/osdl/jbento2/report/machineDetail.vm"); 071 072 writer = new BufferedWriter(new FileWriter(new File( 073 getDestDir(), machineName + ".html"))); 074 template.merge(context, writer); 075 writer.flush(); 076 } finally { 077 IOUtils.closeQuietly(writer); 078 } 079 } 080 081 } 082 083 public List getResourceImages(String regex) { 084 List images = new ArrayList(); 085 Iterator ite = FileUtils.iterateFiles(getDataDir(), 086 new RegexNameFileFilter(regex), FileFilterUtils 087 .trueFileFilter()); 088 while (ite.hasNext()) { 089 File f = (File) ite.next(); 090 images.add(PathUtils.calculateRelativePath(getDataDir(), f)); 091 } 092 093 Collections.sort(images); 094 return images; 095 } 096 097 public List getResourceImagesAsTag(String regex) { 098 List images = new ArrayList(); 099 Iterator ite = FileUtils.iterateFiles(getDataDir(), 100 new RegexNameFileFilter(regex), FileFilterUtils 101 .trueFileFilter()); 102 Pattern pattern = Pattern.compile(regex); 103 while (ite.hasNext()) { 104 File f = (File) ite.next(); 105 Matcher matcher = pattern.matcher(f.getName()); 106 if (matcher.matches() && matcher.groupCount() > 0) { 107 Map item = new HashMap(); 108 String tag = matcher.group(1); 109 item.put("tag", tag); 110 item.put("img", PathUtils 111 .calculateRelativePath(getDataDir(), f)); 112 images.add(item); 113 } 114 } 115 116 Collections.sort(images, new Comparator() { 117 public int compare(Object arg0, Object arg1) { 118 return ((String) ((Map) arg0).get("tag")) 119 .compareTo((String) ((Map) arg1).get("tag")); 120 } 121 }); 122 return images; 123 } 124 125 public List getMachineNames() { 126 Set machineNames = new HashSet(); 127 String regex = "([^\\.]*)\\..*\\.png"; 128 String[] files = getDataDir().list(); 129 Pattern pattern = Pattern.compile(regex); 130 for (int i = 0; i < files.length; i++) { 131 Matcher matcher = pattern.matcher(files[i]); 132 if (matcher.matches()) { 133 machineNames.add(matcher.group(1)); 134 } 135 } 136 List result = new ArrayList(); 137 result.addAll(machineNames); 138 Collections.sort(result); 139 return result; 140 } 141 }