Remove service addresses, add graph letters (#15513).
[tor-metrics-tasks.git] / task-2794 / StillRunning.java
blob111c085987f7f3633eb5fb2abb4d14d1eee14240
1 import java.io.*;
2 import java.util.*;
3 public class StillRunning {
4 public static void main(String[] args) throws Exception {
6 /* Parse bridge pool assignments. */
7 Map<String, String> assignments = new HashMap<String, String>();
8 BufferedReader br = new BufferedReader(new FileReader(
9 "assignments.csv"));
10 String line = br.readLine();
11 while ((line = br.readLine()) != null) {
12 String[] parts = line.split(",");
13 String fingerprint = parts[1];
14 String type = parts[2];
15 assignments.put(fingerprint, type);
17 br.close();
19 /* Parse running bridges in first status of the second day in the data
20 * set. */
21 br = new BufferedReader(new FileReader("statuses.csv"));
22 line = br.readLine();
23 if (!line.split(",")[15].equals("running")) {
24 System.out.println("Column 16 should be 'running'");
25 System.exit(1);
27 String dayOne = null, lastStatus = null;
28 List<String> fingerprints = new ArrayList<String>();
29 Map<String, String> addresses = new HashMap<String, String>();
30 while ((line = br.readLine()) != null) {
31 String[] parts = line.split(",");
32 String status = parts[0];
33 if (dayOne == null) {
34 dayOne = status.substring(0, "yyyy-mm-dd".length());
35 } else if (status.startsWith(dayOne)) {
36 continue;
38 String running = parts[15];
39 if (!running.equals("TRUE")) {
40 continue;
42 if (lastStatus != null && !status.equals(lastStatus)) {
43 break;
45 lastStatus = status;
46 String fingerprint = parts[1];
47 fingerprints.add(fingerprint);
48 String address = parts[4];
49 addresses.put(fingerprint, address);
52 /* Parse subsequent statuses and count how often these bridges
53 * occur. */
54 Map<String, Integer>
55 fingerprintAnyCount = new HashMap<String, Integer>(),
56 fingerprintSameCount = new HashMap<String, Integer>();
57 for (String fingerprint : fingerprints) {
58 fingerprintAnyCount.put(fingerprint, 1);
59 fingerprintSameCount.put(fingerprint, 1);
61 do {
62 String[] parts = line.split(",");
63 String status = parts[0];
64 String running = parts[15];
65 if (!running.equals("TRUE")) {
66 continue;
68 String fingerprint = parts[1];
69 if (!fingerprints.contains(fingerprint)) {
70 continue;
72 fingerprintAnyCount.put(fingerprint,
73 fingerprintAnyCount.get(fingerprint) + 1);
74 String address = parts[4];
75 if (addresses.get(fingerprint).equals(address)) {
76 fingerprintSameCount.put(fingerprint,
77 fingerprintSameCount.get(fingerprint) + 1);
79 } while ((line = br.readLine()) != null);
81 /* Create two lists of fingerprints, ordered by the number of
82 * occurrences. */
83 SortedMap<String, String>
84 sortAnyFingerprints = new TreeMap<String, String>(),
85 sortSameFingerprints = new TreeMap<String, String>();
86 for (Map.Entry<String, Integer> e : fingerprintAnyCount.entrySet()) {
87 sortAnyFingerprints.put(String.format("%05d %s", e.getValue(),
88 e.getKey()), e.getKey());
90 List<String> sortedAnyFingerprints = new ArrayList<String>(
91 sortAnyFingerprints.values());
92 for (Map.Entry<String, Integer> e : fingerprintSameCount.entrySet()) {
93 sortSameFingerprints.put(String.format("%05d %s", e.getValue(),
94 e.getKey()), e.getKey());
96 List<String> sortedSameFingerprints = new ArrayList<String>(
97 sortSameFingerprints.values());
99 /* Write bridges of first status to disk. */
100 BufferedWriter bw = new BufferedWriter(new FileWriter(
101 "still-running-bridges.csv"));
102 bw.write("status,anyid,sameid,type,addresschange\n");
103 for (String fingerprint : sortedAnyFingerprints) {
104 bw.write(lastStatus + ","
105 + sortedAnyFingerprints.indexOf(fingerprint) + ","
106 + sortedSameFingerprints.indexOf(fingerprint) + ","
107 + assignments.get(fingerprint) + ",FALSE\n");
110 /* Parse statuses once again and write bridges to disk. */
111 br = new BufferedReader(new FileReader("statuses.csv"));
112 line = br.readLine();
113 while ((line = br.readLine()) != null) {
114 String[] parts = line.split(",");
115 String status = parts[0];
116 if (status.startsWith(dayOne)) {
117 continue;
119 String running = parts[15];
120 if (!running.equals("TRUE")) {
121 continue;
123 String fingerprint = parts[1];
124 if (!fingerprints.contains(fingerprint)) {
125 continue;
127 String address = parts[4];
128 boolean addressChange = !addresses.get(fingerprint).equals(address);
129 bw.write(status + ","
130 + sortedAnyFingerprints.indexOf(fingerprint) + ","
131 + sortedSameFingerprints.indexOf(fingerprint) + ","
132 + assignments.get(fingerprint) + ","
133 + (addressChange ? "TRUE" : "FALSE") + "\n");
135 bw.close();
136 br.close();