001    package jp.osdl.jbento2.report;
002    
003    import java.io.BufferedWriter;
004    import java.io.File;
005    import java.io.FileFilter;
006    import java.io.FileWriter;
007    import java.util.ArrayList;
008    import java.util.Arrays;
009    import java.util.Collection;
010    import java.util.Collections;
011    import java.util.Comparator;
012    import java.util.Iterator;
013    import java.util.List;
014    import java.util.Map;
015    import java.util.TreeMap;
016    
017    import jp.osdl.jbento2.PathUtils;
018    import jp.osdl.jbento2.RegexNameFileFilter;
019    
020    import org.apache.commons.io.FileUtils;
021    import org.apache.commons.io.IOUtils;
022    import org.apache.commons.io.filefilter.FileFilterUtils;
023    import org.apache.velocity.Template;
024    import org.apache.velocity.VelocityContext;
025    import org.apache.velocity.app.Velocity;
026    
027    public class SampleDetailBuilder extends AbstractReportBuilder {
028        public SampleDetailBuilder(File dataDir, File destDir, File destRootDir) {
029            super(dataDir, destDir, destRootDir);
030        }
031    
032        public void execute() throws Exception {
033            buildSampleDetail();
034        }
035    
036        private void buildSampleDetail() throws Exception {
037    
038            VelocityContext context = new VelocityContext();
039    
040            List tables = new ArrayList();
041    
042            tables
043                    .add(createTableData(getDataDir(), "throughput.txt",
044                            "Throughput"));
045            tables.add(createTableData(getDataDir(), "responseTime.txt",
046                    "Response Time"));
047    
048            context.put("title", "Sample Detail of " + getDataDir().getName());
049            context.put("relativeRoot", getRelativeRoot());
050            context.put("relativePath", getRelativePath());
051    
052            context.put("tables", tables);
053    
054            List cpu = new ArrayList();
055    
056            Collection cpuImages = FileUtils.listFiles(getDataDir(),
057                    new RegexNameFileFilter(".*\\.u\\.all\\.png"), FileFilterUtils
058                            .trueFileFilter());
059            for (Iterator it = cpuImages.iterator(); it.hasNext();) {
060                File f = (File) it.next();
061                Map item = new TreeMap();
062                item.put("machine", PathUtils.basename(f, ".u.all.png"));
063                item.put("img", PathUtils.calculateRelativePath(getDataDir(), f));
064                cpu.add(item);
065            }
066    
067            Collections.sort(cpu, new Comparator() {
068                public int compare(Object arg0, Object arg1) {
069                    return ((String) ((Map) arg0).get("machine"))
070                            .compareTo((String) ((Map) arg1).get("machine"));
071                };
072            });
073            context.put("cpu", cpu);
074    
075            File[] machines = getDataDir().listFiles(new FileFilter() {
076                public boolean accept(File pathname) {
077                    return pathname.isDirectory();
078                }
079            });
080    
081            List machineNames = new ArrayList();
082            for (Iterator it = Arrays.asList(machines).iterator(); it.hasNext();) {
083                File f = (File) it.next();
084                machineNames.add(f.getName());
085            }
086            Collections.sort(machineNames);
087            context.put("machines", machineNames);
088    
089            BufferedWriter writer = null;
090            try {
091                Template template = Velocity
092                        .getTemplate("jp/osdl/jbento2/report/sampleDetail.vm");
093    
094                writer = new BufferedWriter(new FileWriter(new File(getDestDir(),
095                        getDestDir().getName() + ".html")));
096                template.merge(context, writer);
097                writer.flush();
098            } finally {
099                IOUtils.closeQuietly(writer);
100            }
101    
102        }
103    
104        private Map createTableData(File dir, String fileName, String title)
105                throws Exception {
106            File csvFile = new File(dir, fileName);
107            Map result = CsvReader.readData(csvFile);
108            result.put("title", title);
109            return result;
110        }
111    
112    }