Write bridge-stats file with bridge users by country.

This commit is contained in:
Karsten Loesing 2010-01-14 17:40:05 +01:00
parent 871e11aa25
commit 27412aa729
3 changed files with 60 additions and 31 deletions

View File

@ -54,19 +54,20 @@ public class BridgeDescriptorParser {
long started = timeFormat.parse(geoipStartTimeLine.
substring("geoip-start-time ".length())).getTime();
long seconds = (published - started) / 1000L;
Map<String, Double> obs = new HashMap<String, Double>();
Map<String, String> obs = new HashMap<String, String>();
String[] parts = line.split(" ")[1].split(",");
for (String p : parts) {
for (String c : countries) {
if (p.startsWith(c)) {
obs.put(c, ((double) Long.parseLong(p.substring(3)) - 4L)
* 86400.0D / ((double) seconds));
obs.put(c, String.format("%.2f",
((double) Long.parseLong(p.substring(3)) - 4L)
* 86400.0D / ((double) seconds)));
}
}
}
String date = publishedLine.split(" ")[1];
String time = publishedLine.split(" ")[2];
bsfh.addStats(date, time, hashedIdentity, obs);
bsfh.addObs(hashedIdentity, date, time, obs);
}
}
}

View File

@ -8,18 +8,37 @@ public class BridgeStatsFileHandler {
private String statsDir;
private File bridgeStatsFile;
private File hashedRelayIdentitiesFile;
private SortedSet<String> countries;
private SortedSet<String> hashedRelays = new TreeSet<String>();
public BridgeStatsFileHandler(String statsDir) throws IOException {
private SortedMap<String, String> observations;
public BridgeStatsFileHandler(String statsDir,
SortedSet<String> countries) throws IOException {
this.statsDir = statsDir;
this.countries = countries;
this.bridgeStatsFile = new File(statsDir + "/bridge-stats");
this.observations = new TreeMap<String, String>();
if (this.bridgeStatsFile.exists()) {
System.out.print("Reading existing file " + statsDir
+ "/bridge-stats... ");
BufferedReader br = new BufferedReader(new FileReader(
this.bridgeStatsFile));
String line = null;
while ((br.readLine()) != null) {
// TODO read bridge-stats
String line = br.readLine();
if (line != null) {
String[] headers = line.split(",");
for (int i = 3; i < headers.length; i++) {
this.countries.add(headers[i]);
}
while ((br.readLine()) != null) {
String[] readData = line.split(",");
String hashedBridgeIdentity = readData[0];
String date = readData[1];
String time = readData[2];
SortedMap<String, String> obs = new TreeMap<String, String>();
for (int i = 3; i < readData.length; i++) {
obs.put(headers[i], readData[i]);
}
this.addObs(hashedBridgeIdentity, date, time, obs);
}
}
System.out.println("done");
br.close();
@ -33,29 +52,30 @@ public class BridgeStatsFileHandler {
this.hashedRelayIdentitiesFile));
String line = null;
while ((line = br.readLine()) != null) {
hashedRelays.add(line);
this.hashedRelays.add(line);
}
br.close();
System.out.println("done");
}
}
public void addHashedRelay(String hashedRelayIdentity) {
hashedRelays.add(hashedRelayIdentity);
this.hashedRelays.add(hashedRelayIdentity);
}
public boolean isKnownRelay(String hashedBridgeIdentity) {
return hashedRelays.contains(hashedBridgeIdentity);
return this.hashedRelays.contains(hashedBridgeIdentity);
}
public void addStats(String date, String time, String hashedIdentity,
Map<String, Double> obs) {
/*
bwBridgeStats.write(publishedLine.split(" ")[2] + ","
+ publishedLine.split(" ")[1] + "," + hashedRelay);
for (String c : countries) {
bwBridgeStats.append(String.format(",%.2f",
obs.containsKey(c) ? obs.get(c) : 0.0D));
}
bwBridgeStats.append("\n");
*/
public void addObs(String hashedIdentity, String date,
String time, Map<String, String> obs) {
String key = hashedIdentity + "," + date;
StringBuilder sb = new StringBuilder(key + "," + time);
for (String c : countries) {
sb.append("," + (obs.containsKey(c) ? obs.get(c) : "0.0"));
}
String value = sb.toString();
if (!this.observations.containsKey(key)
|| value.compareTo(this.observations.get(key)) > 0) {
this.observations.put(key, value);
}
}
public void writeFile() throws IOException {
@ -63,17 +83,25 @@ public class BridgeStatsFileHandler {
+ "/hashed-relay-identities... ");
BufferedWriter bwRelayIdentities = new BufferedWriter(
new FileWriter(this.hashedRelayIdentitiesFile));
for (String hashedRelay : hashedRelays) {
// TODO check if this bridge is a known relay!!!!!
for (String hashedRelay : this.hashedRelays) {
bwRelayIdentities.append(hashedRelay + "\n");
}
bwRelayIdentities.close();
System.out.println("done");
// TODO write bridge-stats, too
// TODO filter out those that are known relays:
/*
if (!hashedRelays.contains(line.split(",")[2])) {
*/
System.out.print("done\nWriting file " + this.statsDir
+ "/bridge-stats...");
BufferedWriter bwBridgeStats = new BufferedWriter(
new FileWriter(this.bridgeStatsFile));
bwBridgeStats.append("bridge,date,time");
for (String c : this.countries) {
bwBridgeStats.append("," + c);
}
bwBridgeStats.append("\n");
for (String observation : this.observations.values()) {
String hashedBridgeIdentity = observation.split(",")[0];
if (!this.hashedRelays.contains(hashedBridgeIdentity)) {
bwBridgeStats.append(observation + "\n");
}
}
}
}

View File

@ -44,7 +44,7 @@ public class Main {
ConsensusStatsFileHandler csfh = new ConsensusStatsFileHandler(
statsDirectory);
BridgeStatsFileHandler bsfh = new BridgeStatsFileHandler(
statsDirectory);
statsDirectory, countries);
DirreqStatsFileHandler dsfh = new DirreqStatsFileHandler(
statsDirectory, countries);