Add graphing code for #6443.
[tor-metrics-tasks/delber.git] / task-2680 / ProcessSanitizedAssignments.java
blob9289aa17073278bb884225f44431b0195b933f3e
1 import java.io.*;
2 import java.util.*;
4 public class ProcessSanitizedAssignments {
5 public static void main(String[] args) throws IOException {
7 /* Validate command-line arguments. */
8 if (args.length != 1 || !new File(args[0]).exists()) {
9 System.out.println("Usage: java ProcessSanitizedAssignments <dir>");
10 System.exit(1);
13 /* Find all files that we should parse. Somewhat fragile, but should
14 * work. */
15 System.out.println("Creating list of files we should parse.");
16 SortedMap<String, File> assignments = new TreeMap<String, File>();
17 Stack<File> files = new Stack<File>();
18 files.add(new File(args[0]));
19 while (!files.isEmpty()) {
20 File file = files.pop();
21 if (file.isDirectory()) {
22 files.addAll(Arrays.asList(file.listFiles()));
23 } else {
24 assignments.put(file.getName(), file);
27 System.out.println("We found " + assignments.size() + " bridge pool "
28 + "assignment files.");
30 /* Parse assignments. */
31 if (!assignments.isEmpty()) {
32 System.out.println("Parsing bridge pool assignment files.");
33 BufferedWriter bw = new BufferedWriter(new FileWriter(
34 "assignments.csv"));
35 bw.write("assignment,fingerprint,type,ring,port,flag,bucket\n");
36 int parsedAssignments = 0, totalAssignments = assignments.size(),
37 writtenOutputLines = 1;
38 long started = System.currentTimeMillis();
39 for (File file : assignments.values()) {
40 BufferedReader br = new BufferedReader(new FileReader(file));
41 String line, assignmentTime = null;
42 while ((line = br.readLine()) != null) {
43 if (line.startsWith("bridge-pool-assignment ")) {
44 assignmentTime = line.substring("bridge-pool-assignment ".
45 length());
46 } else {
47 String[] parts = line.split(" ");
48 String fingerprint = parts[0];
49 String type = parts[1];
50 String ring = null, port = null, flag = null, bucket = null;
51 for (int i = 2; i < parts.length; i++) {
52 String[] parts2 = parts[i].split("=");
53 String key = parts2[0];
54 String value = parts2[1];
55 if (key.equals("ring")) {
56 } else if (key.equals("ring")) {
57 ring = value;
58 } else if (key.equals("port")) {
59 port = value;
60 } else if (key.equals("flag")) {
61 flag = value;
62 } else if (key.equals("bucket")) {
63 bucket = value;
64 } else {
65 System.out.println("Unknown keyword in line '" + line
66 + "'. Please check. Exiting.");
67 System.exit(1);
70 bw.write(assignmentTime + "," + fingerprint + "," + type + ","
71 + (ring != null ? ring : "NA") + ","
72 + (port != null ? port : "NA") + ","
73 + (flag != null ? flag : "NA") + ","
74 + (bucket != null ? bucket : "NA") + "\n");
75 writtenOutputLines++;
78 br.close();
79 parsedAssignments++;
80 if (parsedAssignments % (totalAssignments / 10) == 0) {
81 double fractionDone = (double) (parsedAssignments) /
82 (double) totalAssignments;
83 double fractionLeft = 1.0D - fractionDone;
84 long now = System.currentTimeMillis();
85 double millisLeft = ((double) (now - started)) * fractionLeft /
86 fractionDone;
87 long secondsLeft = (long) millisLeft / 1000L;
88 System.out.println(" " + (parsedAssignments / (totalAssignments
89 / 10)) + "0% done, " + secondsLeft + " seconds left.");
92 bw.close();
93 System.out.println("Parsed " + parsedAssignments + " bridge pool "
94 + "assignment files and wrote " + writtenOutputLines + " lines "
95 + "to assignments.csv.");
98 /* This is it. */
99 System.out.println("Terminating.");