1 package jp.osdl.jbento2.analyzer;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.apache.commons.cli.*;
19 import org.apache.commons.io.FileUtils;
20 import org.apache.commons.io.filefilter.TrueFileFilter;
21
22 public class MultiSarResultCollectorAllCli {
23
24 private final String JBENTO_FILE = "jbento.conf";
25 private String delimiter = ",";
26 private Map map = new HashMap();
27 private String targetRow = null;
28 private String targetColumn = null;
29 private List seriesName = new ArrayList();
30 private String xLabelName = null;
31
32 public static void main(String[] args) throws Exception {
33 Options options = new Options();
34
35 options.addOption(
36 OptionBuilder.
37 withArgName("source dir").
38 hasArg().
39 withDescription("source directory.").
40 withLongOpt("source").
41 create("s"));
42
43 options.addOption(
44 OptionBuilder.
45 withArgName("config file").
46 hasArg().
47 withDescription("config file").
48 withLongOpt("conf").
49 create("f"));
50
51 if (args.length == 0) {
52 HelpFormatter formatter = new HelpFormatter();
53 formatter.printHelp("MultiResultCollectorAllCli", options, true);
54 return;
55 }
56
57 CommandLineParser parser = new PosixParser();
58 CommandLine line = null;
59 try {
60 line = parser.parse(options, args);
61 } catch (ParseException ex) {
62 throw ex;
63 }
64
65 if (line.hasOption("h") ||
66 (line.getOptions().length == 0 && line.getArgs().length == 0)) {
67 HelpFormatter formatter = new HelpFormatter();
68 formatter.printHelp("MultiResultCollectorAllCli", options, true);
69 return;
70 }
71
72 String configFile = line.getOptionValue("f");
73 String srcDir = line.getOptionValue("s");
74
75 new MultiSarResultCollectorAllCli().execute(configFile, srcDir);
76 }
77
78 public void execute(String configFileName, String srcDirName)
79 throws IOException, ParseException {
80 File configFile = new File(configFileName);
81 List config = parseConfig(configFile);
82
83 File srcDir = new File(srcDirName);
84
85 if (!srcDir.isDirectory()) {
86 throw new IllegalArgumentException(srcDir.getName()
87 + "is not a directory");
88 }
89
90 Iterator ite = config.iterator();
91 while (ite.hasNext()) {
92 ResultCollectConfig conf = (ResultCollectConfig) ite.next();
93
94 Map constName = new HashMap();
95 List param = conf.getParams();
96
97 for (int j = 0; j < param.size(); j++) {
98 if (param.get(j).equals("--constName")) {
99 constName.put(param.get(j + 1), param.get(j + 2));
100 } else if (param.get(j).equals("--xLabelName")) {
101 xLabelName = (String) param.get(j + 1);
102 } else if (param.get(j).equals("--seriesName")) {
103 seriesName.add(param.get(j + 1));
104
105 } else if (param.get(j).equals("--target")) {
106 targetRow = (String) param.get(j + 1);
107 targetColumn = (String) param.get(j + 2);
108 }
109 }
110
111 traverse(srcDir, conf, constName);
112
113 MultiResultCollector collector = new MultiResultCollector();
114 collector.setTarget(targetRow);
115 collector.setMap(map);
116
117 String destDirName = srcDir.getAbsolutePath() + File.separator
118 + "result_collect";
119 File destDir = new File(destDirName);
120 destDir.mkdirs();
121
122
123 String destFileName = "cpu.csv";
124 collector.setDestName(destDirName + File.separator + destFileName);
125
126 collector.analyze();
127 }
128 }
129
130
131 private void traverse(File src, ResultCollectConfig config, Map constName)
132 throws IOException, ParseException {
133 Map confMap = new HashMap();
134 String regexName = null;
135
136 File[] files = src.listFiles();
137 regexName = config.getRegexName();
138
139 for (int i = 0; i < files.length; i++) {
140 File file = files[i];
141 if (file.isDirectory()) {
142 traverse(file, config, constName);
143 } else {
144
145 if (file.getName().equals(JBENTO_FILE)) {
146 try {
147 boolean isMatch = false;
148
149 confMap = readConfFile(file);
150
151 Set const_keyset = constName.keySet();
152 Iterator ite_const = const_keyset.iterator();
153
154
155 if (const_keyset.size() == 0
156 && confMap.containsKey(xLabelName)) {
157 isMatch = true;
158 }
159 while (ite_const.hasNext()) {
160 String const_key = (String) ite_const.next();
161 if (confMap.get(const_key).equals(
162 constName.get(const_key))) {
163 if (confMap.containsKey(xLabelName)) {
164 isMatch = true;
165 }
166 } else {
167 isMatch = false;
168 break;
169 }
170 }
171
172
173
174 if (isMatch) {
175 findFile(file, (String) confMap.get(xLabelName),
176 regexName, targetRow);
177
178 } else {
179
180
181 }
182
183 } catch (IndexOutOfBoundsException e) {
184 e.printStackTrace();
185 }
186 }
187 }
188 }
189 }
190
191 protected Map readConfFile(File src) throws IOException {
192
193 BufferedReader reader = null;
194 Map confMap = new HashMap();
195 try {
196 reader = new BufferedReader(new FileReader(src));
197
198 while (true) {
199 String line = reader.readLine();
200 if (line == null) {
201 break;
202 }
203 String[] data = line.split(" ");
204 confMap.put(data[0], data[1]);
205 }
206
207 } finally {
208 if (reader != null) {
209 try {
210 reader.close();
211 } catch (IOException ignore) {
212
213 }
214 }
215 }
216 return confMap;
217 }
218
219 private File determineTargetDir(File src) {
220 String path = src.getAbsolutePath();
221 if (path.lastIndexOf("conf" + File.separator) == -1) {
222 return null;
223 }
224 return new File(
225 path.substring(0, path.lastIndexOf("conf" + File.separator)) +
226 "report");
227 }
228
229 private void findFile(File src, String xKey, String regexName,
230 String target) throws IOException {
231
232 File targetDir = determineTargetDir(src);
233 if (targetDir == null) {
234 return;
235 }
236
237 try {
238 Collection c =
239 FileUtils.listFiles(targetDir,
240 TrueFileFilter.INSTANCE,
241 TrueFileFilter.INSTANCE);
242
243 for (Iterator it = c.iterator(); it.hasNext();) {
244 File f = (File) it.next();
245
246 Pattern pattern = Pattern.compile((String) regexName);
247 Matcher matcher = pattern.matcher(f.getName());
248
249 if (matcher.matches()) {
250
251 BufferedReader reader = null;
252 try {
253 reader = new BufferedReader(new FileReader(f));
254
255 String firstLine = reader.readLine();
256 String[] columns = firstLine.split(delimiter);
257 int columns_index = -1;
258
259 for (int i = 0; i < columns.length; i++) {
260
261 columns[i] = columns[i].trim();
262 if (columns[i].equals(target)) {
263 columns_index = i;
264 }
265 }
266
267 List keyList = new ArrayList();
268 keyList.add(matcher.group(1));
269 keyList.add(xKey);
270
271 while (true) {
272 String line = reader.readLine();
273 if (line == null) {
274 break;
275 }
276 String[] data = line.split(delimiter);
277
278 for (int i = 0; i < data.length; i++) {
279 data[i] = data[i].trim();
280 }
281
282 if (!data[0].equals(targetColumn)) {
283 continue;
284 } else {
285 map.put(keyList, data[columns_index]);
286 }
287 }
288 } catch (FileNotFoundException e) {
289
290
291
292 } finally {
293 if (reader != null) {
294 try {
295 reader.close();
296 } catch (IOException ignore) {
297
298 }
299 }
300 }
301 }
302 }
303
304 } catch (IllegalArgumentException e) {
305 return;
306 }
307 }
308
309 private List parseConfig(File confFile) throws IOException {
310 List result = new ArrayList();
311
312 BufferedReader reader = new BufferedReader(new FileReader(confFile));
313
314 String line = null;
315 ResultCollectConfig config = null;
316 while (true) {
317 line = reader.readLine();
318 if (line == null) {
319 break;
320 } else if (line.startsWith("#")) {
321 continue;
322 } else if (line.length() == 0) {
323 continue;
324 } else if (line.startsWith(" ") || line.startsWith("\t")) {
325 if (config == null) {
326 throw new IllegalStateException("parse error around "
327 + line);
328 }
329 List list = config.getParams();
330 line = line.trim();
331 String[] datas = parseLine(line);
332 for (int i = 0; i < datas.length; i++) {
333 String data = datas[i];
334 list.add(data);
335 }
336 } else {
337 config = new ResultCollectConfig(line);
338 result.add(config);
339 }
340
341 }
342 return result;
343 }
344
345 private String[] parseLine(String str) {
346 ArrayList result = new ArrayList();
347 char[] chars = str.toCharArray();
348 StringBuffer buffer = new StringBuffer();
349 boolean isInternalQuotes = false;
350 for (int i = 0; i < chars.length; i++) {
351 char c = chars[i];
352 if (c == ' ') {
353 if (isInternalQuotes) {
354 buffer.append(c);
355 } else {
356 result.add(buffer.toString());
357 buffer = new StringBuffer();
358 }
359 } else if (c == '"') {
360 isInternalQuotes = !isInternalQuotes;
361 } else {
362 buffer.append(c);
363 }
364 }
365 if (buffer.length() > 0) {
366 result.add(buffer.toString());
367 }
368 return (String[]) result.toArray(new String[0]);
369 }
370
371 private class ResultCollectConfig {
372 private String regexName = null;
373
374 private List params = new ArrayList();
375
376 public ResultCollectConfig(String regexName) {
377 this.regexName = regexName;
378 }
379
380 public List getParams() {
381 return params;
382 }
383
384 public String getRegexName() {
385 return regexName;
386 }
387 }
388 }