001    package jp.osdl.jbento2.analyzer;
002    
003    import java.io.File;
004    import java.util.regex.Matcher;
005    import java.util.regex.Pattern;
006    
007    import org.apache.commons.cli.CommandLine;
008    import org.apache.commons.cli.CommandLineParser;
009    import org.apache.commons.cli.HelpFormatter;
010    import org.apache.commons.cli.OptionBuilder;
011    import org.apache.commons.cli.Options;
012    import org.apache.commons.cli.ParseException;
013    import org.apache.commons.cli.PosixParser;
014    
015    /**
016     * Generate graphs. graph file name and title is same source csv file.
017     * 
018     * 
019     */
020    public class SarAllCli {
021    
022        private static final String FILE_NAME = ".*\\.(.*)\\.sar";
023        private static final String CSV_FILE_NAME = ".*\\.*\\.(csv)";
024        private String srcDirName;
025    
026        private String destDirName;
027    
028        private String skipTime;
029    
030        private String termTime;
031        
032        private long newDateDelta = -1;
033    
034        public static void main(String[] args) throws Exception {
035            Options options = new Options();
036    
037            options.addOption(OptionBuilder.withArgName("source dir").hasArg()
038                    .withDescription("source directory.").withLongOpt("source")
039                    .create("s"));
040    
041            options.addOption(OptionBuilder.withArgName("skip time[s]").hasArg()
042                    .withDescription("skip seconds after starting measurement.")
043                    .withLongOpt("skip").create("k"));
044    
045            options.addOption(OptionBuilder.withArgName("term time[s]").hasArg()
046                    .withDescription("seconds of term for analyzing.").withLongOpt(
047                            "term").create("t"));
048    
049            options.addOption(OptionBuilder.withArgName("dest dir").hasArg()
050                    .withDescription("destination directory.").withLongOpt("dest")
051                    .create("d"));
052            
053            options.addOption(OptionBuilder.withArgName("new date delta").hasArg()
054                    .withDescription("delta[sec] used to determinate new date")
055                    .withLongOpt("delta").create("l"));
056    
057            if (args.length == 0) {
058                HelpFormatter formatter = new HelpFormatter();
059                formatter.printHelp("SarAllCli", options, true);
060                return;
061            }
062    
063            CommandLineParser parser = new PosixParser();
064            CommandLine line = null;
065            try {
066                line = parser.parse(options, args);
067            } catch (ParseException ex) {
068                throw ex;
069            }
070    
071            if (line.hasOption("h")
072                    || (line.getOptions().length == 0 && line.getArgs().length == 0)) {
073                HelpFormatter formatter = new HelpFormatter();
074                formatter.printHelp("SartAllCli", options, true);
075                return;
076            }
077    
078            SarAllCli cli = new SarAllCli();
079            cli.setSrcDirName(line.getOptionValue("s"));
080            cli.setDestDirName(line.getOptionValue("d"));
081            cli.setSkipTime(line.getOptionValue("k"));
082            cli.setTermTime(line.getOptionValue("t"));
083            if (line.hasOption("l")) {
084                cli.setNewDateDelta(line.getOptionValue("l"));
085            }
086            cli.execute();
087        }
088    
089        public void execute() throws Exception {
090            File srcDir = new File(srcDirName);
091            if (!srcDir.isDirectory()) {
092                throw new IllegalArgumentException(srcDir.getName()
093                        + "is not a directory");
094            }
095            traverse(srcDir);
096            
097            File destDir = new File(destDirName);
098            
099            if (!destDir.isDirectory()) {
100                throw new IllegalArgumentException(destDir.getName()
101                        + "is not a directory");
102            }
103            avgTraverse(destDir);
104        }
105    
106        void traverse(File dir) throws Exception {
107            File[] files = dir.listFiles();
108            for (int i = 0; i < files.length; i++) {
109                File file = files[i];
110                if (file.isDirectory()) {
111                    traverse(file);
112                } else {
113                    Pattern pattern = Pattern.compile(FILE_NAME);
114                    Matcher matcher = pattern.matcher(file.getName());
115                    if (matcher.matches()) {
116                        SarAnalyzer analyzer = new SarAnalyzer();
117                        analyzer.setSrc(file.getAbsolutePath());
118                        if (destDirName != null) {
119                            analyzer.setDest(getDest(file.getAbsolutePath()));
120                        }
121                        analyzer.setParser(matcher.group(1));
122                        analyzer.setSkipTime(skipTime);
123                        analyzer.setTermTime(termTime);
124                        if (newDateDelta >= 0) {
125                            analyzer.setNewDateDelta(newDateDelta);
126                        }
127                        analyzer.analyze();
128                    }
129                }
130            }
131            
132        }
133    
134        void avgTraverse(File dir) throws Exception {
135            File[] files = dir.listFiles();
136            for (int i = 0; i < files.length; i++) {
137                File file = files[i];
138                if (file.isDirectory()) {
139                    avgTraverse(file);
140                } else {
141                    Pattern pattern = Pattern.compile(CSV_FILE_NAME);
142                    Matcher matcher = pattern.matcher(file.getName());
143                    if (matcher.matches()) {
144                        SarAnalyzer analyzer = new SarAnalyzer();
145                        analyzer.setSrc(file.getAbsolutePath());
146    //                    if (destDirName != null) {
147    //                        analyzer.setDest(getDest(file.getAbsolutePath()));
148    //                    }
149                        analyzer.setParser(matcher.group(1));
150    //                    analyzer.setSkipTime(skipTime);
151    //                    analyzer.setTermTime(termTime);
152                        analyzer.average();
153                    }
154                }
155            }
156        }
157        
158        private String getDest(String filePath) {
159            // for cygwin
160            String srcDirName = this.srcDirName.replace('/', File.separatorChar);
161            if (srcDirName.charAt(srcDirName.length() - 1) == File.separatorChar) {
162                srcDirName = srcDirName.substring(0, srcDirName.length() - 1);
163            }
164    
165            String postDir = null;
166            
167            try { 
168                int index = filePath.indexOf(srcDirName);
169                postDir = filePath.substring(index + srcDirName.length(),
170                        filePath.lastIndexOf(File.separatorChar));
171            } catch (IndexOutOfBoundsException e) {
172                System.err.println(e.getMessage());
173                System.err.println("filePath=" + filePath);
174                System.err.println("srcDirName=" + srcDirName);
175                System.err.println("File.separator=" + File.separator);
176                throw e;
177            }
178    
179            String dest = destDirName + postDir;
180            File destDir = new File(dest);
181            if (!destDir.isDirectory()) {
182                if (!destDir.mkdirs()) {
183                    throw new IllegalArgumentException("destDir " + destDir
184                            + " cannot be created");
185                }
186            }
187            String destName = dest
188                    + filePath.substring(filePath.lastIndexOf(File.separator),
189                            filePath.lastIndexOf('.')) + ".csv";
190            return destName;
191        }
192    
193        public String getDestDirName() {
194            return destDirName;
195        }
196    
197        public void setDestDirName(String destDirName) {
198            this.destDirName = destDirName;
199        }
200    
201        public String getSkipTime() {
202            return skipTime;
203        }
204    
205        public void setSkipTime(String skipTime) {
206            this.skipTime = skipTime;
207        }
208    
209        public String getSrcDirName() {
210            return srcDirName;
211        }
212    
213        public void setSrcDirName(String srcDirName) {
214            this.srcDirName = srcDirName;
215        }
216    
217        public String getTermTime() {
218            return termTime;
219        }
220    
221        public void setTermTime(String termTime) {
222            this.termTime = termTime;
223        }
224        
225        public String getNewDateDelta() {
226            return Long.toString(newDateDelta / 1000);
227        }
228    
229        public void setNewDateDelta(String newDateDelta) {
230            long value = Long.parseLong(newDateDelta);
231            this.newDateDelta = value * 1000;
232        }
233    }