Remove service addresses, add graph letters (#15513).
[tor-metrics-tasks.git] / task-2537 / BridgeDBLogConverter.java
blob5a8d69c636e2b88d4c8cfbe719b0aac6d4910110
1 import java.io.*;
2 import java.text.*;
3 import java.util.*;
5 public class BridgeDBLogConverter {
6 public static void main(String[] args) throws Exception {
7 if (args.length != 3) {
8 System.out.println("Usage: java BridgeDBLogConverter logfile year "
9 + "outfile");
10 System.exit(1);
12 File logfile = new File(args[0]), outfile = new File(args[2]);
13 String year = args[1];
14 SimpleDateFormat logFormat = new SimpleDateFormat(
15 "yyyy MMM dd HH:mm:ss");
16 SimpleDateFormat isoFormat = new SimpleDateFormat(
17 "yyyy-MM-dd HH:mm:ss");
18 SimpleDateFormat fileFormat = new SimpleDateFormat(
19 "yyyy/MM/dd/yyyy-MM-dd-HH-mm-ss");
20 logFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
21 isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
22 fileFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
23 BufferedReader br = new BufferedReader(new FileReader(logfile));
24 BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
25 String line;
26 SortedMap<String, String> entries = new TreeMap<String, String>();
27 long lastTimestamp = -1L;
28 String fingerprint = null, lastFingerprint = null, type = null,
29 port = "", flag = "";
30 while ((line = br.readLine()) != null) {
31 long timestamp = logFormat.parse(year + " "
32 + line.substring(0, 15)).getTime();
33 if (timestamp > lastTimestamp + 10L * 60L * 1000L) {
34 if (lastTimestamp > 0L) {
35 bw.write("bridge-pool-assignment "
36 + isoFormat.format(lastTimestamp) + "\n");
37 for (String entry : entries.values()) {
38 bw.write(entry + "\n");
40 entries.clear();
43 lastTimestamp = timestamp;
44 String[] parts = line.split(" ");
45 fingerprint = parts[7];
46 String assignment = line.substring(line.indexOf(parts[7]) + 41);
47 if (!fingerprint.equals(lastFingerprint)) {
48 if (lastFingerprint != null) {
49 entries.put(lastFingerprint, lastFingerprint + " " + type
50 + port + flag);
52 type = null;
53 port = "";
54 flag = "";
56 if (assignment.startsWith("to IP ")) {
57 int ring = -1;
58 if (assignment.startsWith("to IP category ring")) {
59 ring = 4; // TODO This is fragile!
60 } else {
61 ring = Integer.parseInt(assignment.split(" ")[3]) - 1;
63 String newType = "https ring=" + ring;
64 if (type != null && !type.equals(newType)) {
65 System.out.println("type inconsistency in line '" + line + "'");
66 System.exit(1);
68 type = newType;
69 if (assignment.endsWith(" (port-443 subring)")) {
70 String newPort = " port=443";
71 if (port.length() > 0 && !port.equals(newPort)) {
72 System.out.println("port inconsistency in line '" + line
73 + "'");
74 System.exit(1);
76 port = newPort;
77 } else if (assignment.endsWith(" (stable subring)")) {
78 String newFlag = " flag=stable";
79 if (flag.length() > 0 && !flag.equals(newFlag)) {
80 System.out.println("flag inconsistency in line '" + line
81 + "'");
82 System.exit(1);
84 flag = newFlag;
86 } else if (assignment.equals("to email ring")) {
87 String newType = "email";
88 if (type != null && !type.equals(newType)) {
89 System.out.println("type inconsistency in line '" + line + "'");
90 System.exit(1);
92 type = newType;
93 } else if (assignment.startsWith("to Ring ")) {
94 String newType = "email";
95 if (type != null && !type.equals(newType)) {
96 System.out.println("type inconsistency in line '" + line + "'");
97 System.exit(1);
99 type = newType;
100 if (assignment.equals("to Ring (port-443 subring)")) {
101 String newPort = " port=443";
102 if (port.length() > 0 && !port.equals(newPort)) {
103 System.out.println("port inconsistency in line '" + line
104 + "'");
105 System.exit(1);
107 port = newPort;
108 } else if (assignment.equals("to Ring (stable subring)")) {
109 String newFlag = " flag=stable";
110 if (flag.length() > 0 && !flag.equals(newFlag)) {
111 System.out.println("flag inconsistency in line '" + line
112 + "'");
113 System.exit(1);
115 flag = newFlag;
116 } else {
117 System.out.println("type inconsistency in line '" + line
118 + "'");
119 System.exit(1);
121 } else {
122 type = "unallocated";
124 lastFingerprint = fingerprint;
126 if (lastTimestamp > 0L) {
127 bw.write("bridge-pool-assignment "
128 + isoFormat.format(lastTimestamp) + "\n");
129 for (String entry : entries.values()) {
130 bw.write(entry + "\n");
133 bw.close();