Add serialization methods to StatManager, FrequencyStat and Frequency

for easier collection
This commit is contained in:
zab2
2015-11-04 21:41:33 +00:00
parent b10b8581cc
commit 3f91e448c0
3 changed files with 62 additions and 0 deletions

View File

@ -150,4 +150,16 @@ public class Frequency {
private final static long now() {
return System.currentTimeMillis();
}
/**
* Appends the data of this frequency to the specified StringBuilder
* @param dest to append data to
* @since 0.9.23
*/
synchronized void store(StringBuilder dest) {
dest.append("avgInterval:").append(_avgInterval).append(',');
dest.append("minAverageInterval").append(_minAverageInterval).append(',');
dest.append("lastEvent").append(_lastEvent).append(",");
dest.append("count").append(_count);
}
}

View File

@ -1,5 +1,10 @@
package net.i2p.stat;
import java.io.IOException;
import java.io.OutputStream;
import net.i2p.data.DataHelper;
/** coordinate an event frequency over various periods */
public class FrequencyStat {
/** unique name of the statistic */
@ -92,5 +97,34 @@ public class FrequencyStat {
if ((obj == null) || !(obj instanceof FrequencyStat)) return false;
return _statName.equals(((FrequencyStat)obj)._statName);
}
private final static String NL = System.getProperty("line.separator");
/**
* Serializes this FrequencyStat to the provided OutputStream
* @param out to write to
* @param prefix to prepend to the stat
* @throws IOException if something goes wrong
* @since 0.9.23
*/
public void store(OutputStream out, String prefix) throws IOException {
StringBuilder buf = new StringBuilder(1024);
buf.append(NL);
buf.append("################################################################################").append(NL);
buf.append("# Frequency: ").append(_groupName).append(": ").append(_statName).append(NL);
buf.append("# ").append(_description).append(NL);
buf.append("# ").append(NL).append(NL);
out.write(buf.toString().getBytes("UTF-8"));
buf.setLength(0);
for (Frequency r: _frequencies){
buf.append("#######").append(NL);
buf.append("# Period : ").append(DataHelper.formatDuration(r.getPeriod())).append(" for rate ")
.append(_groupName).append(" - ").append(_statName).append(NL);
buf.append(NL);
r.store(buf);
out.write(buf.toString().getBytes("UTF-8"));
buf.setLength(0);
}
}
}

View File

@ -1,5 +1,7 @@
package net.i2p.stat;
import java.io.IOException;
import java.io.OutputStream;
import java.text.Collator;
import java.util.HashMap;
import java.util.HashSet;
@ -247,4 +249,18 @@ public class StatManager {
public boolean ignoreStat(String statName) {
return _context.isRouterContext() && !_context.getBooleanProperty(PROP_STAT_FULL);
}
/**
* Serializes all Frequencies and Rates to the provided OutputStream
* @param out to write to
* @param prefix to use when serializing
* @throws IOException if something goes wrong
* @since 0.9.23
*/
public void store(OutputStream out, String prefix) throws IOException {
for (FrequencyStat fs : _frequencyStats.values())
fs.store(out, prefix);
for (RateStat rs : _rateStats.values())
rs.store(out,prefix);
}
}