Only read stats/ files if needed.

This commit is contained in:
Karsten Loesing 2010-01-20 09:56:54 +01:00
parent a7721be511
commit b117b769ca
4 changed files with 66 additions and 16 deletions

View File

@ -11,12 +11,18 @@ public class BridgeStatsFileHandler {
private SortedSet<String> countries;
private SortedSet<String> hashedRelays = new TreeSet<String>();
private SortedMap<String, String> observations;
private boolean initialized;
private boolean modified;
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>();
this.hashedRelayIdentitiesFile = new File(statsDir
+ "/hashed-relay-identities");
}
private void initialize() throws IOException {
if (this.bridgeStatsFile.exists()) {
System.out.print("Reading file " + statsDir + "/bridge-stats... ");
BufferedReader br = new BufferedReader(new FileReader(
@ -42,8 +48,6 @@ public class BridgeStatsFileHandler {
System.out.println("done");
br.close();
}
this.hashedRelayIdentitiesFile = new File(statsDir
+ "/hashed-relay-identities");
if (this.hashedRelayIdentitiesFile.exists()) {
System.out.print("Reading file " + statsDir
+ "/hashed-relay-identities... ");
@ -56,15 +60,28 @@ public class BridgeStatsFileHandler {
br.close();
System.out.println("done");
}
this.initialized = true;
}
public void addHashedRelay(String hashedRelayIdentity) {
public void addHashedRelay(String hashedRelayIdentity)
throws IOException {
if (!this.initialized) {
this.initialize();
}
this.hashedRelays.add(hashedRelayIdentity);
this.modified = true;
}
public boolean isKnownRelay(String hashedBridgeIdentity) {
public boolean isKnownRelay(String hashedBridgeIdentity)
throws IOException {
if (!this.initialized) {
this.initialize();
}
return this.hashedRelays.contains(hashedBridgeIdentity);
}
public void addObs(String hashedIdentity, String date,
String time, Map<String, String> obs) {
String time, Map<String, String> obs) throws IOException {
if (!this.initialized) {
this.initialize();
}
String key = hashedIdentity + "," + date;
StringBuilder sb = new StringBuilder(key + "," + time);
for (String c : countries) {
@ -75,9 +92,13 @@ public class BridgeStatsFileHandler {
|| value.compareTo(this.observations.get(key)) > 0) {
this.observations.put(key, value);
}
this.modified = true;
}
public void writeFile() throws IOException {
if (!this.modified) {
return;
}
if (!this.hashedRelays.isEmpty()) {
System.out.print("Writing file " + this.statsDir
+ "/hashed-relay-identities... ");

View File

@ -11,11 +11,19 @@ public class ConsensusStatsFileHandler {
private File consensusStatsFile;
private SortedMap<String, String> consensusResults;
private SortedMap<String, String> bridgeConsensusResults;
private boolean initialized;
private boolean modified;
public ConsensusStatsFileHandler(String statsDir) throws IOException {
this.statsDir = statsDir;
this.consensusResults = new TreeMap<String, String>();
this.consensusStatsRawFile = new File(statsDir
+ "/consensus-stats-raw");
this.bridgeConsensusResults = new TreeMap<String, String>();
this.bridgeConsensusStatsRawFile = new File(statsDir
+ "/bridge-consensus-stats-raw");
this.consensusStatsFile = new File(statsDir + "/consensus-stats");
}
private void initialize() throws IOException {
if (this.consensusStatsRawFile.exists()) {
System.out.print("Reading file " + statsDir
+ "/consensus-stats-raw... ");
@ -28,9 +36,6 @@ public class ConsensusStatsFileHandler {
System.out.println("done");
br.close();
}
this.bridgeConsensusResults = new TreeMap<String, String>();
this.bridgeConsensusStatsRawFile = new File(statsDir
+ "/bridge-consensus-stats-raw");
if (this.bridgeConsensusStatsRawFile.exists()) {
System.out.print("Reading file " + statsDir
+ "/bridge-consensus-stats-raw... ");
@ -43,17 +48,29 @@ public class ConsensusStatsFileHandler {
System.out.println("done");
br.close();
}
this.consensusStatsFile = new File(statsDir + "/consensus-stats");
this.initialized = true;
}
public void addConsensusResults(String validAfter, int exit, int fast,
int guard, int running, int stable) {
int guard, int running, int stable) throws IOException {
if (!this.initialized) {
this.initialize();
}
consensusResults.put(validAfter, validAfter + "," + exit + "," + fast
+ "," + guard + "," + running + "," + stable);
this.modified = true;
}
public void addBridgeConsensusResults(String published, int running) {
public void addBridgeConsensusResults(String published, int running)
throws IOException {
if (!this.initialized) {
this.initialize();
}
bridgeConsensusResults.put(published, published + "," + running);
this.modified = true;
}
public void writeFile() throws IOException {
if (!this.modified) {
return;
}
SortedMap<String, String> csAggr = new TreeMap<String, String>();
SortedMap<String, String> bcsAggr = new TreeMap<String, String>();
if (!consensusResults.isEmpty()) {
@ -119,7 +136,8 @@ public class ConsensusStatsFileHandler {
if (tempDate != null
&& (next == null || !next.substring(0, 10).equals(tempDate))) {
if (bridgeStatusesDay > 23) {
bcsAggr.put(tempDate, "" + (brunningDay / bridgeStatusesDay) + "\n");
bcsAggr.put(tempDate, "" + (brunningDay / bridgeStatusesDay)
+ "\n");
}
brunningDay = 0;
bridgeStatusesDay = 0;

View File

@ -9,12 +9,16 @@ public class DirreqStatsFileHandler {
private SortedSet<String> countries;
private File dirreqStatsFile;
private SortedMap<String, String> observations;
private boolean initialized;
private boolean modified;
public DirreqStatsFileHandler(String statsDir,
SortedSet<String> countries) throws IOException {
this.statsDir = statsDir;
this.countries = countries;
this.dirreqStatsFile = new File(statsDir + "/dirreq-stats");
this.observations = new TreeMap<String, String>();
}
private void initialize() throws IOException {
if (this.dirreqStatsFile.exists()) {
System.out.print("Reading file " + statsDir + "/dirreq-stats... ");
BufferedReader br = new BufferedReader(new FileReader(
@ -40,10 +44,13 @@ public class DirreqStatsFileHandler {
System.out.println("done");
br.close();
}
this.initialized = true;
}
public void addObs(String dirNickname, String date,
Map<String, String> obs, String share) {
Map<String, String> obs, String share) throws IOException {
if (!this.initialized) {
this.initialize();
}
String obsKey = dirNickname + "," + date;
StringBuilder sb = new StringBuilder(obsKey);
for (String c : this.countries) {
@ -51,8 +58,12 @@ public class DirreqStatsFileHandler {
}
sb.append("," + share);
this.observations.put(obsKey, sb.toString());
this.modified = true;
}
public void writeFile() throws IOException {
if (!this.modified) {
return;
}
if (!this.observations.isEmpty()) {
System.out.print("Writing file " + this.statsDir
+ "/dirreq-stats... ");

View File

@ -55,7 +55,7 @@ public class Main {
directories.put("FFCB46DB1339DA84674C70D7CB586434C4370441",
"moria1");
// Initialize stats file handlers
// Prepare stats file handlers
String statsDirectory = "stats";
ConsensusStatsFileHandler csfh = new ConsensusStatsFileHandler(
statsDirectory);
@ -64,7 +64,7 @@ public class Main {
DirreqStatsFileHandler dsfh = new DirreqStatsFileHandler(
statsDirectory, countries);
// Initialize parsers
// Prepare parsers
RelayDescriptorParser rdp = new RelayDescriptorParser(csfh, bsfh,
dsfh, countries, directories);
BridgeDescriptorParser bdp = new BridgeDescriptorParser(csfh, bsfh,